拆解二元炸弹第三阶段逻辑难理解

scy*_*cy8 8 c x86 assembly reverse-engineering att

我有来自二进制炸弹实验室的以下汇编程序。目标是确定在不触发explode_bomb函数的情况下运行二进制文件所需的关键字。我评论了我对这个程序的程序集的分析,但我无法将所有内容拼凑在一起。

我相信我有我需要的所有信息,但我仍然无法看到实际的底层逻辑,因此我被卡住了。我将不胜感激任何帮助!

以下是反汇编的程序本身:

0x08048c3c <+0>:     push   %edi
   0x08048c3d <+1>:     push   %esi
   0x08048c3e <+2>:     sub    $0x14,%esp
   0x08048c41 <+5>:     movl   $0x804a388,(%esp)
   0x08048c48 <+12>:    call   0x80490ab <string_length>
   0x08048c4d <+17>:    add    $0x1,%eax
   0x08048c50 <+20>:    mov    %eax,(%esp)
   0x08048c53 <+23>:    call   0x8048800 <malloc@plt>
   0x08048c58 <+28>:    mov    $0x804a388,%esi
   0x08048c5d <+33>:    mov    $0x13,%ecx
   0x08048c62 <+38>:    mov    %eax,%edi
   0x08048c64 <+40>:    rep movsl %ds:(%esi),%es:(%edi)
   0x08048c66 <+42>:    movzwl (%esi),%edx
   0x08048c69 <+45>:    mov    %dx,(%edi)
   0x08048c6c <+48>:    movzbl 0x11(%eax),%edx
   0x08048c70 <+52>:    mov    %dl,0x10(%eax)
   0x08048c73 <+55>:    mov    %eax,0x4(%esp)
   0x08048c77 <+59>:    mov    0x20(%esp),%eax
   0x08048c7b <+63>:    mov    %eax,(%esp)
   0x08048c7e <+66>:    call   0x80490ca <strings_not_equal>
   0x08048c83 <+71>:    test   %eax,%eax
   0x08048c85 <+73>:    je     0x8048c8c <phase_3+80>
   0x08048c87 <+75>:    call   0x8049363 <explode_bomb>
   0x08048c8c <+80>:    add    $0x14,%esp
   0x08048c8f <+83>:    pop    %esi
   0x08048c90 <+84>:    pop    %edi
   0x08048c91 <+85>:    ret  
Run Code Online (Sandbox Code Playgroud)

以下块包含我的分析

  5 <phase_3>
  6 0x08048c3c <+0>:     push   %edi // push value in edi to stack
  7 0x08048c3d <+1>:     push   %esi // push value of esi to stack
  8 0x08048c3e <+2>:     sub    $0x14,%esp // grow stack by 0x14 (move stack ptr -0x14 bytes)
  9 
 10 0x08048c41 <+5>:     movl   $0x804a388,(%esp) // put 0x804a388 into loc esp points to
 11 
 12 0x08048c48 <+12>:    call   0x80490ab <string_length> // check string length, store in eax
 13 0x08048c4d <+17>:    add    $0x1,%eax // increment val in eax by 0x1 (str len + 1) 
 14 // at this point, eax = str_len + 1  = 77 + 1 = 78
 15 
 16 0x08048c50 <+20>:    mov    %eax,(%esp) // get val in eax and put in loc on stack
 17 //**** at this point, 0x804a388 should have a value of 78? ****
 18 
 19 0x08048c53 <+23>:    call   0x8048800 <malloc@plt> // malloc --> base ptr in eax
 20 
 21 0x08048c58 <+28>:    mov    $0x804a388,%esi // 0x804a388 in esi 
 22 0x08048c5d <+33>:    mov    $0x13,%ecx // put 0x13 in ecx (counter register)
 23 0x08048c62 <+38>:    mov    %eax,%edi // put val in eax into edi
 24 0x08048c64 <+40>:    rep movsl %ds:(%esi),%es:(%edi) // repeat 0x13 (19) times
 25 // **** populate malloced memory with first 19 (edit: 76) chars of string at 0x804a388 (this string is 77 characters long)? ****
 26 
 27 0x08048c66 <+42>:    movzwl (%esi),%edx // put val in loc esi points to into edx
***** // at this point, edx should contain the string at 0x804a388?
 28 
 29 0x08048c69 <+45>:    mov    %dx,(%edi) // put val in dx to loc edi points to
***** // not sure what effect this has or what is in edi at this point
 30 0x08048c6c <+48>:    movzbl 0x11(%eax),%edx // edx = [eax + 0x11]
 31 0x08048c70 <+52>:    mov    %dl,0x10(%eax) // [eax + 0x10] = dl
 32 0x08048c73 <+55>:    mov    %eax,0x4(%esp) // [esp + 0x4] = eax
 33 0x08048c77 <+59>:    mov    0x20(%esp),%eax // eax = [esp + 0x20]
 34 0x08048c7b <+63>:    mov    %eax,(%esp) // put val in eax into loc esp points to
***** // not sure what effect these movs have
 35 
 36 // edi --> first arg
 37 // esi --> second arg
 38 // compare value in esi to edi
 39 0x08048c7e <+66>:    call   0x80490ca <strings_not_equal> // store result in eax
 40 0x08048c83 <+71>:    test   %eax,%eax 
 41 0x08048c85 <+73>:    je     0x8048c8c <phase_3+80>
 42 0x08048c87 <+75>:    call   0x8049363 <explode_bomb>
 43 0x08048c8c <+80>:    add    $0x14,%esp
 44 0x08048c8f <+83>:    pop    %esi
 45 0x08048c90 <+84>:    pop    %edi
 46 0x08048c91 <+85>:    ret 
Run Code Online (Sandbox Code Playgroud)

更新:

在调用 strings_not_equal 之前检查寄存器后,我得到以下信息:

eax            0x804d8aa        134535338
ecx            0x0      0
edx            0x76     118
ebx            0xffffd354       -11436
esp            0xffffd280       0xffffd280
ebp            0xffffd2b8       0xffffd2b8
esi            0x804a3d4        134521812
edi            0x804f744        134543172
eip            0x8048c7b        0x8048c7b <phase_3+63>
eflags         0x282    [ SF IF ]
cs             0x23     35
ss             0x2b     43
ds             0x2b     43
es             0x2b     43
fs             0x0      0
gs             0x63     99
Run Code Online (Sandbox Code Playgroud)

我使用 Hopper 得到以下反汇编的伪代码:

在此处输入图片说明

我什至尝试使用在 eax 中找到的数字和之前看到的字符串作为我的关键字,但它们都不起作用。

Pet*_*des 3

该函数将静态存储中的字符串修改后复制到分配的缓冲区中。


这看起来很奇怪。大小malloc取决于strlen+1,但memcpy大小是编译时常量?您的反编译显然表明地址是一个字符串文字,所以看起来没问题。

可能错过优化的原因是由于自定义string_length()函数可能仅在另一个函数中定义.c(并且炸弹是在没有跨文件内联的链接时优化的情况下编译的)。Sosize_t len = string_length("some string literal");不是编译时常量,编译器发出对它的调用,而不是能够使用已知的字符串常量长度。

但可能他们strcpy在源代码中使用了并且编译器将其内联为rep movs. strcpy由于它显然是从字符串文字复制的,因此长度是编译时常量,并且它可以优化通常必须完成的那部分工作。通常,如果您已经计算了长度,那么最好使用它,memcpy而不是strcpy动态地再次计算它,但在这种情况下,它实际上帮助编译器为该部分编写了更好的代码,而不是将返回值传递string_lengthmemcpy,同样是因为string_length无法内联和优化。


   <+0>:     push   %edi // push value in edi to stack
   <+1>:     push   %esi // push value of esi to stack
   <+2>:     sub    $0x14,%esp // grow stack by 0x14 (move stack ptr -0x14 bytes)
Run Code Online (Sandbox Code Playgroud)

这样的评论是多余的;指令本身已经这么说了。这保存了两个调用保留的寄存器,以便函数可以在内部使用它们并在以后恢复它们。

你的评论sub更好;是的,增长堆栈是这里更高层次的语义。该函数为局部变量保留一些空间(以及用于存储函数参数mov而不是pushed 的空间)。


复制rep movsd0x13 * 4 字节,递增 ESI 和 EDI 以指向复制区域的末尾。因此另一movsd条指令将复制与前一个副本相邻的另外 4 个字节。

该代码实际上复制了另一个 2,但它没有使用movsw,而是使用了movzw字加载和mov存储。 这总共复制了 78 个字节。

  ...
      # at this point EAX = malloc return value which I'll call buf
<+28>:    mov    $0x804a388,%esi            # copy src = a string literal in .rodata?
<+33>:    mov    $0x13,%ecx
<+38>:    mov    %eax,%edi                  # copy dst = buf
<+40>:    rep movsl %ds:(%esi),%es:(%edi)   # memcpy 76 bytes and advance ESI, EDI

<+42>:    movzwl (%esi),%edx
<+45>:    mov    %dx,(%edi)        # copy another 2 bytes (not moving ESI or EDI)
 # final effect: 78-byte memcpy
Run Code Online (Sandbox Code Playgroud)

rep movsb在某些(但不是全部)CPU 上,仅使用or具有适当的计数会很有效rep movsw,但这不是编译器在这种情况下选择的。 movzx又名 AT&Tmovz是一种在没有部分寄存器惩罚的情况下进行窄负载的好方法。这就是编译器这样做的原因,这样他们就可以写入一个完整的寄存器,即使他们只使用存储指令读取该寄存器的低 8 或 16 位。

将字符串文字复制到 buf 后,我们有一个字节加载/存储,它用buf. 请记住此时 EAX 仍然指向buf返回malloc值。 所以它正在制作字符串文字的修改副本。

<+48>:    movzbl 0x11(%eax),%edx
<+52>:    mov    %dl,0x10(%eax)             # buf[16] = buf[17]
Run Code Online (Sandbox Code Playgroud)

也许如果源代码没有击败常量传播,那么具有足够高的优化级别的编译器可能会将最终字符串放入.rodata您可以找到它的位置,从而使这个炸弹阶段变得微不足道。:P

然后它将指针存储为堆栈参数以进行字符串比较。

<+55>:    mov    %eax,0x4(%esp)               # 2nd arg slot = EAX = buf
<+59>:    mov    0x20(%esp),%eax              #  function arg = user input?
<+63>:    mov    %eax,(%esp)                  # first arg slot = our incoming stack arg
<+66>:    call   0x80490ca <strings_not_equal>
Run Code Online (Sandbox Code Playgroud)

如何“作弊”:用GDB查看运行时结果

一些炸弹实验室只允许您在测试服务器上在线运行炸弹,该服务器会记录爆炸。你不能在GDB下运行它,只能使用静态反汇编(如objdump -drwC -Mintel)。因此测试服务器可以记录您尝试失败的次数。例如,我通过 google 找到了cs.virginia.edu 上的 CS 3330,其中满分需要少于 20 次爆炸。

使用 GDB 在函数中检查内存/寄存器比仅进行静态分析要容易得多,事实上,仅在最后检查单个输入,从而简化了该函数。例如,只需看看其他参数被传递给了什么strings_not_equal。(特别是如果您使用 GDBjumpset $pc = ...命令跳过炸弹爆炸检查。)

在调用之前设置断点或单步执行strings_not_equal 用于p (char*)$eax将 EAX 视为 achar*并显示从该地址开始的(以 0 结尾的)C 字符串。此时 EAX 保存缓冲区的地址,正如您从存储到堆栈所看到的那样。

复制/粘贴该字符串结果即可完成。

具有多个数字输入的其他阶段通常不容易用调试器来完成,并且至少需要一些数学知识,但是链表阶段需要您以正确的顺序进行列表遍历的数字序列也变得微不足道,如果您知道如何使用调试器来设置寄存器,以便在访问它们时使比较成功。