空字符串的 Delphi 字节 ASM 表示是什么?
What's Delphi byte ASM representation of a empty string?
考虑以下字节数组:
const
//Values : Array[0..4] of byte = ($C2,[=11=],[=11=],,);
Values: Array[0..0] of Byte = (???); // '' ?
如何用 asm
表示初始化第二个字节数组(类似于第一个字节数组),但这次是空字符串?
版次:
As it is unclear for what purpose you ask and what you are going to do with that array. – Tom Brunberg
目标是将空字符串写入目标进程的地址:
procedure WriteBytes(hProcess: THandle; Address: pointer; const Buffer: array of byte);
var
Read: THandle;
oldprot, tmp: dword;
begin
if (VirtualProtectEx(hProcess, Address, Length(Buffer), PAGE_EXECUTE_READWRITE, @oldprot)) then
Writeln('1 - VirtualProtectEx() successfully!');
if (WriteProcessMemory(hProcess, Address, @Buffer, Length(Buffer), Read)) then
Writeln('2 - WriteProcessMemory() successfully!');
if (VirtualProtectEx(hProcess, Address, Length(Buffer), oldprot, @tmp)) then
Writeln('3 - VirtualProtectEx() successfully!');
end;
在 C++ 中,以下示例运行良好:
PVOID hmod = debug_event.u.LoadDll.lpBaseOfDll;
ULONG op;
SIZE_T NumberOfBytesWritten;
if (VirtualProtectEx(pi.hProcess, hmod, 1, PAGE_READWRITE, &op))
{
WriteProcessMemory(pi.hProcess, hmod, "", 1, &NumberOfBytesWritten);
}
Delphi怎么会这样?
WriteProcessMemory的声明是:
function WriteProcessMemory(hProcess: THandle; const lpBaseAddress: Pointer;
lpBuffer: Pointer; nSize: SIZE_T; var lpNumberOfBytesWritten: SIZE_T): BOOL; stdcall;
您显示的C代码:
WriteProcessMemory(pi.hProcess, hmod, "", 1, &NumberOfBytesWritten);
使用在 C 中传递给函数的空字符串作为指向内存的指针,该内存包含以 nul 字符结尾的字符串字符数组。所以在你显示的代码中(空字符串),指针指向一个空字节。
你说:
The goal is write a empty string to a address of target process
你的声明几乎是正确的,不同之处在于你的字节数组,相当于 C 代码必须以 nul 终止,如果你对 C nul 字符串的等价物感兴趣,你可以使用:
const
Values: Array[0..0] of Byte = (0);
如果要传递非 nul 字符串,请注意 Delphi 使用的 Unicode(16 位字符),除非您使用 AnsiString(8 位字符)。如果使用强制转换获取字符串的地址,则会得到一个指向以 nul 结尾的字符串的指针。 nul 是 8 位或 16 位,具体取决于字符串类型。 Bu 在 nul 字符串的情况下,指针为 nil。
考虑以下字节数组:
const
//Values : Array[0..4] of byte = ($C2,[=11=],[=11=],,);
Values: Array[0..0] of Byte = (???); // '' ?
如何用 asm
表示初始化第二个字节数组(类似于第一个字节数组),但这次是空字符串?
版次:
As it is unclear for what purpose you ask and what you are going to do with that array. – Tom Brunberg
目标是将空字符串写入目标进程的地址:
procedure WriteBytes(hProcess: THandle; Address: pointer; const Buffer: array of byte);
var
Read: THandle;
oldprot, tmp: dword;
begin
if (VirtualProtectEx(hProcess, Address, Length(Buffer), PAGE_EXECUTE_READWRITE, @oldprot)) then
Writeln('1 - VirtualProtectEx() successfully!');
if (WriteProcessMemory(hProcess, Address, @Buffer, Length(Buffer), Read)) then
Writeln('2 - WriteProcessMemory() successfully!');
if (VirtualProtectEx(hProcess, Address, Length(Buffer), oldprot, @tmp)) then
Writeln('3 - VirtualProtectEx() successfully!');
end;
在 C++ 中,以下示例运行良好:
PVOID hmod = debug_event.u.LoadDll.lpBaseOfDll;
ULONG op;
SIZE_T NumberOfBytesWritten;
if (VirtualProtectEx(pi.hProcess, hmod, 1, PAGE_READWRITE, &op))
{
WriteProcessMemory(pi.hProcess, hmod, "", 1, &NumberOfBytesWritten);
}
Delphi怎么会这样?
WriteProcessMemory的声明是:
function WriteProcessMemory(hProcess: THandle; const lpBaseAddress: Pointer;
lpBuffer: Pointer; nSize: SIZE_T; var lpNumberOfBytesWritten: SIZE_T): BOOL; stdcall;
您显示的C代码:
WriteProcessMemory(pi.hProcess, hmod, "", 1, &NumberOfBytesWritten);
使用在 C 中传递给函数的空字符串作为指向内存的指针,该内存包含以 nul 字符结尾的字符串字符数组。所以在你显示的代码中(空字符串),指针指向一个空字节。
你说:
The goal is write a empty string to a address of target process
你的声明几乎是正确的,不同之处在于你的字节数组,相当于 C 代码必须以 nul 终止,如果你对 C nul 字符串的等价物感兴趣,你可以使用:
const
Values: Array[0..0] of Byte = (0);
如果要传递非 nul 字符串,请注意 Delphi 使用的 Unicode(16 位字符),除非您使用 AnsiString(8 位字符)。如果使用强制转换获取字符串的地址,则会得到一个指向以 nul 结尾的字符串的指针。 nul 是 8 位或 16 位,具体取决于字符串类型。 Bu 在 nul 字符串的情况下,指针为 nil。