pvs-stdio ue4_PVS-Studio静态分析仪作为防止零日漏洞的工具

pvs-stdio ue4

PVS-Studio Static Analyzer as a Tool for Protection against Zero-Day Vulnerabilities

A Zero-day (0-day) vulnerability is a computer-software vulnerability introduced during the development process and not yet discovered by the developers. Zero-day vulnerabilities can be exploited by hackers, thus affecting the company's reputation. Developers should seek to minimize the number of defects leading to such vulnerabilities. PVS-Studio, a static code analyzer for C, C++, C#, and Java code, is one of the tools capable of detecting security issues.

零日(0天)漏洞是在开发过程中引入的,尚未被开发人员发现的计算机软件漏洞。 黑客可以利用零日漏洞,从而影响公司的声誉。 开发人员应设法减少导致此类漏洞的缺陷数量。 PVS-Studio是用于C,C ++,C#和Java代码的静态代码分析器,是能够检测安全问题的工具之一。

零日漏洞 (Zero-day vulnerabilities)

A Zero-day vulnerability (also known as 0-day vulnerability) is a computer-software vulnerability that is unknown to, or unaddressed by, those who should be interested in mitigating the vulnerability (including the vendor of the target software). Until the vulnerability is mitigated, hackers can exploit it to adversely affect computer programs, data, additional computers or a network. The term means the developers don't have a single day to fix the defect because no one knows about it yet. Some of the well-known vendors and software products such as Adobe, Windows, Tor browser, and many others, were affected by zero-day vulnerabilities in the past.

零日漏洞 (也称为0天漏洞)是一种计算机软件漏洞,对于那些有兴趣缓解该漏洞(包括目标软件的供应商)的人来说,这是未知的或未解决的。 在缓解漏洞之前,黑客可以利用它对计算机程序,数据,其他计算机或网络产生不利影响。 该术语表示开发人员没有一天可以修复该缺陷,因为尚无人知道。 过去,某些知名供应商和软件产品(如AdobeWindowsTor浏览器等)受到零日漏洞的影响。

Some were lucky to have a vulnerability found and reported by people who were not going to exploit it. The case of MacOS is one such example. In some other cases, the developers themselves produced a patch with which, while adding new features, they also fixed a zero-day vulnerability without knowing it.

有些人很幸运能够发现漏洞并由不打算利用它的人报告。 MacOS就是这样的一个例子。 在另一些情况下,开发人员自己制作了一个补丁,使用该补丁,在添加新功能的同时,他们还修复了零日漏洞,却不知情。

Others were less lucky though. For instance, not so long ago, Google Chrome had to urgently fix a vulnerability that could be exploited to remotely execute arbitrary code.

其他人则不那么幸运。 例如,不久前,谷歌浏览器不得不紧急修复一个漏洞 ,该漏洞可被用来远程执行任意代码。

The problem is you can't guarantee 100% protection against these vulnerabilities as you can't effectively fight a threat you don't even know of. However, there are ways to make such defects less likely to occur in your program – this will be the topic of this article, but we should take a look at some theory first.

问题是您无法保证100%抵御这些漏洞,因为您无法有效应对甚至不知道的威胁。 但是,有一些方法可以减少此类缺陷在您的程序中的发生-这将是本文的主题,但是我们应该首先了解一些理论。

静态分析 (Static analysis)

Static analysis is a method of checking the source code of a software program using an analyzer without executing the program itself and can be viewed as automated code review. Sometimes static analysis can be much more effective than peer code review but can't completely replace it. I tried to summarize the pros and cons of code review and static analysis relative to each other in the following table:

静态分析是一种使用分析器检查软件程序源代码而无需执行程序本身的方法,可以将其视为自动代码检查。 有时,静态分析可能比对等代码检查更为有效,但不能完全取代它。 我试图在下表中总结代码审查和静态分析相对于彼此的优缺点:

Code reviewStatic analysis
Helps find not only trivial but also high-level bugs Helps find unfamiliar defects or vulnerabilities
Helps improve the program's architecture and work out a consistent coding styleHelps find bugs not easily noticeable to the human eye (e.g. typos)
ExpensiveCheaper than code review
Takes up a lot of programmers' time. Breaks are necessary as the reviewer's attention tends to weaken quicklyFalse positives are unavoidable; the user has to customize the analyzer
代码审查 静态分析
帮助您不仅发现琐碎的问题,而且还发现高级错误 帮助发现不熟悉的缺陷或漏洞
帮助改善程序的体系结构并制定一致的编码风格 帮助发现人眼不易察觉的错误(例如错别字)
昂贵 比代码审查便宜
占用大量程序员的时间。 休息是必要的,因为审阅者的注意力会Swift减弱 误报是不可避免的。 用户必须自定义分析仪

CVE和CWE (CVE and CWE)

Common Vulnerabilities and Exposures (CVE) is a database of information-security vulnerabilities and exposures. Its initial purpose was to organize known software defects into a coherent list. In the past, most information-security tools were using their own databases and names for such defects, and it was to bring order to that chaos and establish compatibility between different tools that the MITRE Corporation developed CVE in 1999. However, CVE turned out to be insufficient for estimating code security. Some other system was needed, with finer classification and more detailed descriptions. That's how the Common Weakness Enumeration (CWE) came into existence. If a defect is listed in the CWE, it may cause an exploitable vulnerability and get added to the CVE list as well. The Euler diagram below shows the relations between the standards.

常见漏洞和披露(CVE)是一个信息安全漏洞和披露的数据库。 其最初目的是将已知的软件缺陷组织到一个连贯的列表中。 过去,大多数信息安全工具都使用自己的数据库和此类缺陷的名称,这是为了解决混乱问题,并在MITER Corporation在1999年开发CVE的各种工具之间建立兼容性。不足以估计代码安全性。 还需要其他一些系统,具有更好的分类和更详细的描述。 这就是“普通弱点枚举”(CWE)诞生的方式。 如果CWE中列出了缺陷,则可能导致可利用的漏洞,并且也被添加到CVE列表中。 下面的欧拉图显示了标准之间的关系。

CWE, CVE

Some static analyzers can inform you if, for example, your project employs a library containing a vulnerability. Knowing this, you can download a newer version of the library, with the defect fixed, to make your code less susceptible to security threats caused by a mistake in someone else's code.

某些静态分析器可以通知您,例如,您的项目是否使用包含漏洞的库。 知道了这一点,您可以下载已修复缺陷的库的更新版本,以使您的代码不太容易受到他人代码错误引起的安全威胁的影响。

As the CVE and CWE standards were embraced by the developer community, they were also supported by many information-security tools including static analyzers. Analyzers that support these classifications can be viewed as SAST solutions. SAST (Static Application Security Testing) allows developers to detect vulnerabilities in the source code of programs at the earliest stages of the software development life cycle.

随着CVE和CWE标准被开发人员社区所接受,它们也得到了包括静态分析器在内的许多信息安全工具的支持。 支持这些分类的分析仪可以视为SAST解​​决方案。 SAST(静态应用程序安全性测试)允许开发人员在软件开发生命周期的最早阶段检测程序源代码中的漏洞。

SAST is yet another practice to minimize the probability of zero-day vulnerabilities occurring in your project. An analyzer supporting the CWE standard can tell you where a potential vulnerability is lurking so that you could fix it to make your application more reliable and less likely to contain a 0-day threat.

SAST是另一种实践,可以最大程度地减少项目中发生零日漏洞的可能性。 支持CWE标准的分析器可以告诉您潜在漏洞在哪里潜伏,以便您可以对其进行修复,以使您的应用程序更可靠并且更不可能包含0天威胁。

There is a variety of SAST tools. I'll take the PVS-Studio analyzer as an example to show how these tools can help fight vulnerabilities. Warnings of this analyzer are classified as CWE. Some examples are given below.

有各种各样的SAST工具。 我将以PVS-Studio分析仪为例,以说明这些工具如何帮助对抗漏洞。 该分析仪的警告被归类为CWE。 下面给出一些示例。

PVS-Studio diagnostic message: CWE-561: Dead Code (V3021).

PVS-Studio诊断消息: CWE-561无效代码( V3021 )。

public string EncodeImage(....)
{
  if (string.IsNullOrWhiteSpace(inputPath))
  {
    throw new ArgumentNullException("inputPath");
  }
  if (string.IsNullOrWhiteSpace(inputPath))
  {
    throw new ArgumentNullException("outputPath");
  }
  ....
}

This code contains a typo: the conditions of both if statements check the same variable. The message accompanying the exception suggests that the second condition should check the outputPath variable instead. This mistake has made some part of the code unreachable.

这段代码包含一个错字:两个if语句的条件都检查同一个变量。 出现异常的消息表明,第二种情况应改为检查outputPath变量。 此错误使代码的某些部分无法访问。

Bugs like that might seem harmless, but this impression is wrong. Let's take a look at another trivial and seemingly harmless bug that has to do with a duplicate goto statement.

这样的错误似乎无害,但是这种印象是错误的。 让我们看一下另一个与重复的goto语句有关的琐碎且看似无害的错误。

This bug once caused a vulnerability in iOS.

此错误曾经导致iOS中的漏洞。

The CVE-2014-1266 vulnerability: The SSLVerifySignedServerKeyExchange function in libsecurity_ssl/lib/sslKeyExchange.c in the Secure Transport feature in the Data Security component in Apple iOS 6.x before 6.1.6 and 7.x before 7.0.6, Apple TV 6.x before 6.0.2, and Apple OS X 10.9.x before 10.9.2 does not check the signature in a TLS Server Key Exchange message, which allows man-in-the-middle attackers to spoof SSL servers by using an arbitrary private key for the signing step or omitting the signing step.

CVE-2014-1266漏洞:Apple TV 6.x之前的6.1.6版和7.x之前的7.x版,Apple TV的数据安全组件中的安全传输功能中的libsecurity_ssl / lib / sslKeyExchange.c中的libsecurity_ssl / lib / sslKeyExchange.c中的SSLVerifySignedServerKeyExchange函数6.0.2之前的6.x和10.9.2之前的Apple OS X 10.9.x不会检查TLS服务器密钥交换消息中的签名,该消息允许中间人攻击者通过使用任意内容欺骗SSL服务器签名步骤或省略签名步骤的私钥。

static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx, 
                                 bool isRsa, 
                                 SSLBuffer signedParams,
                                 uint8_t *signature, 
                                 UInt16 signatureLen)
{
  OSStatus err;
  ....

  if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
    goto fail;
  if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    goto fail;
    goto fail;
  if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
    goto fail;
  ....

fail:
  SSLFreeBuffer(&signedHashes);
  SSLFreeBuffer(&hashCtx);
  return err;
}

Like in the first example, the duplicate goto here led to unreachable code: whatever the conditions of the if statements, the second goto statement would be executed anyway. As a result, the signature wouldn't be checked, the function would return 0, meaning the signature was OK, and the program would receive a key from the server even if the signature check failed. This key is used to encrypt the data being transmitted.

像在第一个示例中一样,此处重复的goto导致无法访问的代码:无论if语句的条件如何,无论如何都将执行第二个goto语句。 结果,将不检查签名,该函数将返回0,这意味着签名正常,并且即使签名检查失败,程序也将从服务器接收密钥。 该密钥用于加密正在传输的数据。

This trivial bug had drastic implications. The incident illustrates why there's no point speculating if this or that CWE defect is dangerous or not – you just have to fix it for the sake of your code's safety.

这个琐碎的错误产生了深远的影响。 该事件说明了为什么没有理由推测这种或那个CWE缺陷是否危险–您仅出于安全代码的考虑而必须对其进行修复。

By the way, PVS-Studio could have easily found this bug, reporting it with two CWE warnings at once:

顺便说一句,PVS-Studio可以很容易地发现此错误,并立即通过两个CWE警告报告此错误:

Here's another example. Long ago, in 2012, a security issue was discovered in MySQL, which could be exploited by an attacker to enter the MySQL database. Below you will see the flawed code fragment, where the vulnerability occurred.

这是另一个例子。 很久以前,在2012年,MySQL中发现了一个安全问题,攻击者可以利用它来进入MySQL数据库。 在下面,您将看到存在漏洞的代码片段。

The CVE-2012-2122 vulnerability: sql/password.c in Oracle MySQL 5.1.x before 5.1.63, 5.5.x before 5.5.24, and 5.6.x before 5.6.6, and MariaDB 5.1.x before 5.1.62, 5.2.x before 5.2.12, 5.3.x before 5.3.6, and 5.5.x before 5.5.23, when running in certain environments with certain implementations of the memcmp function, allows remote attackers to bypass authentication by repeatedly authenticating with the same incorrect password, which eventually causes a token comparison to succeed due to an improperly-checked return value.

CVE-2012-2122漏洞:Oracle MySQL 5.1.x在5.1.63之前的sql / password.c,5.5.24在5.5.24之前的5.5.x,5.6.6之前的5.6.x和5.1.62之前的MariaDB 5.1.x ,5.2.12之前的5.2.x,5.3.6之前的5.3.x和5.5.23之前的5.5.x,在具有memcmp功能某些实现的某些环境中运行时,允许远程攻击者通过使用相同的错误密码,由于返回值检查不正确,最终导致令牌比较成功。

typedef char my_bool;
my_bool
check_scramble(const char *scramble_arg, const char *message,
                             const uint8 *hash_stage2)
{
  ....
  return memcmp(hash_stage2, hash_stage2_reassured, SHA1_HASH_SIZE);
}

The memcmp function returns a value of type int, while the check_scramble function returns a value of type my_bool, which is in fact char. The int value gets implicitly cast to char, with the most significant bits truncated. This caused about 1 out of 256 attempts to log in with an arbitrary password for a known username to succeed.

memcmp函数返回int类型的值,而check_scramble函数返回my_bool类型的值,实际上是charint值隐式转换为char ,最高有效位被截断。 这导致大约256次尝试中使用任意密码登录的尝试中有1次成功使已知用户名成功。

Again, this CWE defect could have been neutralized and prevented from becoming a CVE vulnerability much earlier, at the coding stage. For example, PVS-Studio reports it as CWE-197 (V642): Numeric Truncation Error.

同样,此CWE缺陷在编码阶段可以更早地被消除并防止成为CVE漏洞。 例如,PVS-Studio将其报告为CWE-197 ( V642 ):数值截断错误。

See the article "How Can PVS-Studio Help in the Detection of Vulnerabilities?" for further reading on the topic.

有关该主题的更多信息,请参阅文章“ PVS-Studio如何帮助检测漏洞? ”。

结论 (Conclusion)

You can't be 100% sure your program is safe from 0-day vulnerabilities. But you can still make them much less likely to occur. This is done by using specialized SAST tools such as PVS-Studio. If your project is found to contain defects classified as CWE issues, make sure to fix them. Even though few of CWE defects end up on the CVE list, fixing them helps to secure your program from many potential threats.

您不能100%地确定您的程序不受0天漏洞的影响。 但是您仍然可以使它们发生的可能性大大降低。 这可以通过使用专用的SAST工具(例如PVS-Studio)来完成。 如果发现您的项目包含归类为CWE问题的缺陷,请确保对其进行修复。 尽管很少有CWE缺陷最终出现在CVE列表中,但修复它们有助于保护您的程序免受许多潜在威胁的侵害。

参考文献 (References)

  1. Download and evaluate PVS-Studio

    下载并评估PVS-Studio

  2. Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

    PVS-Studio代码分析器中用于查找错误和潜在漏洞的技术

  3. Classification of PVS-Studio warnings according to the Common Weakness Enumeration (CWE)

    根据常见弱点枚举(CWE)对PVS-Studio警告进行分类

  4. Classification of PVS-Studio warnings according to the SEI CERT Coding Standard

    根据SEI CERT编码标准对PVS-Studio警告进行分类

翻译自: https://habr.com/en/company/pvs-studio/blog/477838/

pvs-stdio ue4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值