简体   繁体   English

Delphi中的缓冲区溢出漏洞

[英]Buffer overflow vulnerability in Delphi

I'm interested to know, is Delphi vulnerable to Buffer overflow attack?我很想知道,Delphi 是否容易受到缓冲区溢出攻击? I read some pages which mentioned Delphi is secure to that vuln because "Delphi can use Pascal strings as well as generic windows strings (PChar). When interfacing with Win API there is no other option except using Pchar".我读了一些页面,其中提到 Delphi 对那个 vuln 是安全的,因为“Delphi 可以使用 Pascal 字符串以及通用 windows 字符串 (PChar)。当与 Win ZDB974238714CA8DE6434A7CE1D083A 连接时,除了使用 Pchar1 之外没有其他选项。” is that true?真的吗? thanks谢谢

is Delphi vulnerable to Buffer overflow attack? Delphi 是否容易受到缓冲区溢出攻击?

MOST languages are susceptible to buffer overflow attacks.大多数语言都容易受到缓冲区溢出攻击。 A buffer overflow is a coding bug, not a language defect.缓冲区溢出是编码错误,而不是语言缺陷。 For example, in Delphi:例如,在 Delphi 中:

var
  buf: array[0..0] of Byte;
  i: Integer;
begin
  Move(buf, i, sizeof(i)); // buffer overflow!
  PInteger(@buf)^ := i;    // buffer overflow!
end;

MOST languages will let you shoot yourself in the foot, if you are not careful.如果您不小心,大多数语言会让您在脚下开枪。 There is only so much hand-holding a compiler can do.编译器能做的只有这么多。 Not everything can be avoided at compile-time.并非所有事情都可以在编译时避免。 Programming is not just about writing code that compiles, but also about writing code that acts correctly and responsibly at runtime.编程不仅仅是编写可编译的代码,还涉及编写在运行时正确且负责任地运行的代码。

SOME languages may wrap buffers in such a way that bounds checking is performed at runtime, mitigating the risk of buffer overflows.某些语言可能会以在运行时执行边界检查的方式包装缓冲区,从而降低缓冲区溢出的风险。 Delphi is not one of those languages, since it allows you to operate directly on raw memory, so you can pretty much do whatever you want (well, whatever the underlying OS lets you do, anyway). Delphi 不是这些语言之一,因为它允许您直接在原始 memory 上进行操作,因此您几乎可以做任何您想做的事情(好吧,不管底层操作系统让您做什么,无论如何)。 And this is certainly true for Pascal strings.这对于 Pascal 字符串来说当然是正确的。

I read some pages which mentioned Delphi is secure to that vuln because "Delphi can use Pascal strings as well as generic windows strings (PChar).我读了一些页面,其中提到 Delphi 对那个 vuln 是安全的,因为“Delphi 可以使用 Pascal 字符串以及通用 windows 字符串 (PChar)。

Delphi has no features to avoid all possible kinds of buffer overflows. Delphi 没有避免所有可能的缓冲区溢出的功能。 But, if you write your code to use buffers correctly and sanely, overflows are not likely to happen.但是,如果您编写代码以正确且合理地使用缓冲区,则不太可能发生溢出。 This is not limited to just strings, either.这也不仅限于字符串。

When interfacing with Win API there is no other option except using Pchar". is that true?与 Win API 连接时,除了使用 Pchar 没有其他选择。是这样吗?

It depends on the particular API.这取决于特定的 API。 Most use simple null-terminated PChar strings, yes.大多数使用简单的以 null 结尾的PChar字符串,是的。 But some use UNICODE_STRING records instead, which use WideChar buffers that are not guaranteed to be null-terminated.但有些使用UNICODE_STRING记录,这些记录使用不保证以空值终止的WideChar缓冲区。 Some use ActiveX/COM BSTR (Delphi WideString ) strings instead.一些使用 ActiveX/COM BSTR (Delphi WideString ) 字符串代替。

Delphi as IDE? Delphi 为 IDE? Maybe.也许。 As language?作为语言? Sure.当然。 Judge for yourself:自己判断:

var
  s: String;
  i: Integer;
begin
  s:= 'four';  // Length of string: 4 characters
  for i:= 1 to 1138 do begin  // This loop goes WAY beyond the String's buffer
    write( s[i] );  // What will it access after i=4?
  end;
end;

PChar is needed because the WinAPI is not constructed for Pascal, but Pascal needs to bend for APIs.需要PChar是因为 WinAPI 不是为 Pascal 构建的,但 Pascal 需要为 API 弯曲。 Buffer overflows are a problem, but it's not like PChar is radioactive and String is Jesus - it's up to the programmer to not be overly stupid.缓冲区溢出是一个问题,但它不像PChar是放射性的,而String是耶稣——这取决于程序员不要过于愚蠢。

Buffer overflow attack is not related to any specific language.缓冲区溢出攻击与任何特定语言无关。 Those attacks are only possible when the developer wrongly coded his application.只有当开发人员错误地编码了他的应用程序时,这些攻击才有可能发生。

To make it short, it is your responsibility as the developer to write all tests when memory is being written with data from the outside.简而言之,当使用来自外部的数据写入 memory 时,您作为开发人员有责任编写所有测试。 You have to ALWAYS check if the data length is correct to fit when you write it.您必须始终检查数据长度是否正确以适合您编写它。

For Delphi, there are tools that helps detect buffer overflow (or underflow and many other bugs).对于 Delphi,有一些工具可以帮助检测缓冲区溢出(或下溢和许多其他错误)。 For example madExcept .例如madExcept This tool won't prevent buffer overflow, it will immediately if your program overflow a dynamically allocated buffer.此工具不会防止缓冲区溢出,如果您的程序溢出动态分配的缓冲区,它将立即。 This is a test tool that should not be delivered in released version.这是一个测试工具,不应在发布版本中提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM