这个 SCAS 和 STOS 的组合是什么?

逆向工程 部件 x86 反编译
2021-07-08 21:57:20

我有一个带有几个指令的简单片段:

01:    mov   edi, [ebp+8]
02:    mov   edx, edi
03:    xor   eax, eax
04:    or    ecx, 0FFFFFFFFh
05:    repne scasb
06:    add   ecx, 2
07:    neg   ecx
08:    mov   al, [ebp+0Ch]
09:    mov   edi, edx
10:    rep   stosb
11:    mov   eax, edx

我应该解释一下:

1. in line in line的类型分别是什么。[ebp+8]01[ebp+C]08

2.这段代码有什么作用?

  1. Line01类似于edi = *(ebp+8),它存储目标地址,不确定。但我无法解释线08

  2. 按照intel手册,SCASB (scan byte string)我假设这段代码确实初始化了字符串的缓冲区,重复写入0字节eax时间,然后分配aledi.

2个回答
  • ebp指向堆栈,这是存储函数参数的地方。ebp+8是你的第一个参数,ebp+0C第二个。
  • 第一个参数是一个指向以 0 结尾的字符串的指针,并被加载到edi.
  • repne scasb指令将递增edi和递减ecx,直到ecx为零或edi指向一个字节,该字节等于al您的情况下 - 0 中的一,因此这将扫描 C 字符串的末尾。
  • 将 2 添加到ecx,然后将其取反,将使其等于字符串长度。用一两个例子在纸上自己尝试一下。
  • eax从 中获取函数的第二个参数ebp+0C
  • 字符串(字符串的地址已保存在edx并返回到edi)被字节 in 覆盖al- 您的第二个函数参数。

所以这个函数的作用是用相同的字节序列(参数 2)覆盖未知长度的以空字符结尾的字符串(参数 1)。

用 powershell 模拟序列

cat scasb.ps1
$edx=$edi=$args[0];  $eax=0; $ecx=-1;
while($edi[$ecx--]){}; $ecx+=2;  $ecx=-$ecx;
"length of string using .net method "+$edi.length;
"length of string using repne scasb "+$ecx;
$edi=$edx.tochararray()
while($ecx){$edi[--$ecx]=$args[1]};  $ofs=""; $edx;[string]$edi

结果

powershell -f scasb.ps1 "abracadabra gili gili choo" r
length of string using .net method 26
length of string using repne scasb 26
abracadabra gili gili choo
rrrrrrrrrrrrrrrrrrrrrrrrrr