csapp bomblab

Posted N4tural

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csapp bomblab相关的知识,希望对你有一定的参考价值。

实验概述

实验模拟了一个二进制炸弹,有六个“雷管”,输入正确的字符串就可以拆卸。要进行反汇编和gdb调试

   /* Hmm...  Six phases must be more secure than one phase! */
    input = read_line();             /* Get input                   */
    phase_1(input);                  /* Run the phase               */
    phase_defused();                 /* Drat!  They figured it out!
				      * Let me know how they did it. */
    printf("Phase 1 defused. How about the next one?\\n");

    /* The second phase is harder.  No one will ever figure out
     * how to defuse this... */
    input = read_line();
    phase_2(input);
    phase_defused();
    printf("That's number 2.  Keep going!\\n");

    /* I guess this is too easy so far.  Some more complex code will
     * confuse people. */
    input = read_line();
    phase_3(input);
    phase_defused();
    printf("Halfway there!\\n");

    /* Oh yeah?  Well, how good is your math?  Try on this saucy problem! */
    input = read_line();
    phase_4(input);
    phase_defused();
    printf("So you got that one.  Try this one.\\n");
    
    /* Round and 'round in memory we go, where we stop, the bomb blows! */
    input = read_line();
    phase_5(input);
    phase_defused();
    printf("Good work!  On to the next...\\n");

    /* This phase will never be used, since no one will get past the
     * earlier ones.  But just in case, make this one extra hard. */
    input = read_line();
    phase_6(input);
    phase_defused();

    /* Wow, they got it!  But isn't something... missing?  Perhaps
     * something they overlooked?  Mua ha ha ha ha! */
    

用命令objdump -d bomb>bomb.txt导出汇编到文件bomb.txt

phase1

分析输入时的调用链

rdi:the address of the string that we input  
400e3a:	e8 a1 00 00 00       	callq  400ee0 <phase_1>
  400ee0:	48 83 ec 08          	sub    $0x8,%rsp
  400ee4:	be 00 24 40 00       	mov    $0x402400,%esi
  400ee9:	e8 4a 04 00 00       	callq  401338 <strings_not_equal>

调用下面这个函数,后面也会用到

0000000000401338 <strings_not_equal>:
  401338:	41 54                	push   %r12
  40133a:	55                   	push   %rbp
  40133b:	53                   	push   %rbx
  40133c:	48 89 fb             	mov    %rdi,%rbx
  40133f:	48 89 f5             	mov    %rsi,%rbp
  401342:	e8 d4 ff ff ff       	callq  40131b <string_length>
  401347:	41 89 c4             	mov    %eax,%r12d
  40134a:	48 89 ef             	mov    %rbp,%rdi
  40134d:	e8 c9 ff ff ff       	callq  40131b <string_length>
  401352:	ba 01 00 00 00       	mov    $0x1,%edx
  401357:	41 39 c4             	cmp    %eax,%r12d
  40135a:	75 3f                	jne    40139b <strings_not_equal+0x63>
  40135c:	0f b6 03             	movzbl (%rbx),%eax
  40135f:	84 c0                	test   %al,%al
  401361:	74 25                	je     401388 <strings_not_equal+0x50>
  401363:	3a 45 00             	cmp    0x0(%rbp),%al
  401366:	74 0a                	je     401372 <strings_not_equal+0x3a>
  401368:	eb 25                	jmp    40138f <strings_not_equal+0x57>
  40136a:	3a 45 00             	cmp    0x0(%rbp),%al
  40136d:	0f 1f 00             	nopl   (%rax)
  401370:	75 24                	jne    401396 <strings_not_equal+0x5e>
  401372:	48 83 c3 01          	add    $0x1,%rbx
  401376:	48 83 c5 01          	add    $0x1,%rbp
  40137a:	0f b6 03             	movzbl (%rbx),%eax
  40137d:	84 c0                	test   %al,%al
  40137f:	75 e9                	jne    40136a <strings_not_equal+0x32>
  401381:	ba 00 00 00 00       	mov    $0x0,%edx
  401386:	eb 13                	jmp    40139b <strings_not_equal+0x63>
  401388:	ba 00 00 00 00       	mov    $0x0,%edx
  40138d:	eb 0c                	jmp    40139b <strings_not_equal+0x63>
  40138f:	ba 01 00 00 00       	mov    $0x1,%edx
  401394:	eb 05                	jmp    40139b <strings_not_equal+0x63>
  401396:	ba 01 00 00 00       	mov    $0x1,%edx
  40139b:	89 d0                	mov    %edx,%eax
  40139d:	5b                   	pop    %rbx
  40139e:	5d                   	pop    %rbp
  40139f:	41 5c                	pop    %r12
  4013a1:	c3                   	retq   
000000000040131b <string_length>:
  40131b:	80 3f 00             	cmpb   $0x0,(%rdi) //防止rdi输入为空
  40131e:	74 12                	je     401332 <string_length+0x17>
  401320:	48 89 fa             	mov    %rdi,%rdx
  401323:	48 83 c2 01          	add    $0x1,%rdx
  401327:	89 d0                	mov    %edx,%eax
  401329:	29 f8                	sub    %edi,%eax
  40132b:	80 3a 00             	cmpb   $0x0,(%rdx)
  40132e:	75 f3                	jne    401323 <string_length+0x8>
  401330:	f3 c3                	repz retq 
  401332:	b8 00 00 00 00       	mov    $0x0,%eax
  401337:	c3                   	retq  

其实这里rsi存的就是正确答案了
这是个测试字符串长度的程序,地址依次加1进行比较,当最后一个为null时,跳出函数执行,返回字符串长度到eax
然后回到上面第一次调用检查我们输入字符串的长度,放到r12,第二次放入eax后直接和r12比较,如果相等,再进行输入地址是否为空的判断,并且401363这里也进行了一次判断输入和目的地址的第一个字节是否相同的判断,这里40133c指令把输入字符串地址给过rbx了。0x40136a~0x40137f这一段就是借用eax为一个字节的temp不停逐字节的循环判断两个字符串是否相等。为什么要用temp就是源操作数和目的操作数不能同时为内存引用。
后面的话返回值就是0,如果当中任何一个检查没有过返回值就是1了。

400eee:	85 c0                	test   %eax,%eax
400ef0:	74 05                	je     400ef7 <phase_1+0x17>
400ef2:	e8 43 05 00 00       	callq  40143a <explode_bomb>
400ef7:	48 83 c4 08          	add    $0x8,%rsp
400efb:	c3                   	retq   
000000000040143a <explode_bomb>:
  40143a:	48 83 ec 08          	sub    $0x8,%rsp
  40143e:	bf a3 25 40 00       	mov    $0x4025a3,%edi
  401443:	e8 c8 f6 ff ff       	callq  400b10 <puts@plt>
  401448:	bf ac 25 40 00       	mov    $0x4025ac,%edi
  40144d:	e8 be f6 ff ff       	callq  400b10 <puts@plt>
  401452:	bf 08 00 00 00       	mov    $0x8,%edi
  401457:	e8 c4 f7 ff ff       	callq  400c20 <exit@plt>

直接跳到400ef7,如果eax不为0的话,就会调用引爆炸弹这个函数,这个函数打印字符串后直接exit了,直接退出
那这里的话还会返回到main函数,从400ee0里面return后,下一条指令又是调用4015c4 <phase_defused>函数

00000000004015c4 <phase_defused>:
  4015c4:	48 83 ec 78          	sub    $0x78,%rsp
  4015c8:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
  4015cf:	00 00 
  4015d1:	48 89 44 24 68       	mov    %rax,0x68(%rsp)
  4015d6:	31 c0                	xor    %eax,%eax
  4015d8:	83 3d 81 21 20 00 06 	cmpl   $0x6,0x202181(%rip)        # 603760 <num_input_strings>
  4015df:	75 5e                	jne    40163f <phase_defused+0x7b>
  4015e1:	4c 8d 44 24 10       	lea    0x10(%rsp),%r8
  4015e6:	48 8d 4c 24 0c       	lea    0xc(%rsp),%rcx
  4015eb:	48 8d 54 24 08       	lea    0x8(%rsp),%rdx
  4015f0:	be 19 26 40 00       	mov    $0x402619,%esi
  4015f5:	bf 70 38 60 00       	mov    $0x603870,%edi
  4015fa:	e8 f1 f5 ff ff       	callq  400bf0 <__isoc99_sscanf@plt>
  4015ff:	83 f8 03             	cmp    $0x3,%eax
  401602:	75 31                	jne    401635 <phase_defused+0x71>
  401604:	be 22 26 40 00       	mov    $0x402622,%esi
  401609:	48 8d 7c 24 10       	lea    0x10(%rsp),%rdi
  40160e:	e8 25 fd ff ff       	callq  401338 <strings_not_equal>
  401613:	85 c0                	test   %eax,%eax
  401615:	75 1e                	jne    401635 <phase_defused+0x71>
  401617:	bf f8 24 40 00       	mov    $0x4024f8,%edi
  40161c:	e8 ef f4 ff ff       	callq  400b10 <puts@plt>
  401621:	bf 20 25 40 00       	mov    $0x402520,%edi
  401626:	e8 e5 f4 ff ff       	callq  400b10 <puts@plt>
  40162b:	b8 00 00 00 00       	mov    $0x0,%eax
  401630:	e8 0d fc ff ff       	callq  401242 <secret_phase>
  401635:	bf 58 25 40 00       	mov    $0x402558,%edi
  40163a:	e8 d1 f4 ff ff       	callq  400b10 <puts@plt>
  40163f:	48 8b 44 24 68       	mov    0x68(%rsp),%rax
  401644:	64 48 33 04 25 28 00 	xor    %fs:0x28,%rax
  40164b:	00 00 
  40164d:	74 05                	je     401654 <phase_defused+0x90>
  40164f:	e8 dc f4 ff ff       	callq  400b30 <__stack_chk_fail@plt>
  401654:	48 83 c4 78          	add    $0x78,%rsp
  401658:	c3                   	retq   
  401659:	90                   	nop
  40165a:	90                   	nop
  40165b:	90                   	nop
  40165c:	90                   	nop
  40165d:	90                   	nop
  40165e:	90                   	nop
  40165f:	90                   	nop

这里我写的时候忘记保存了,根据印象应该就是一个恢复环境的,后面也会调用这个函数
Border relations with Canada have never been better.

phase_2

直接看400efc

400efc:	55                   	push   %rbp
400efd:	53                   	push   %rbx
400efe:	48 83 ec 28          	sub    $0x28,%rsp
400f02:	48 89 e6             	mov    %rsp,%rsi //rsi = rsp
400f05:	e8 52 05 00 00       	callq  40145c <read_six_numbers>
400f0a:	83 3c 24 01          	cmpl   $0x1,(%rsp)

000000000040145c <read_six_numbers>:
  40145c:	48 83 ec 18          	sub    $0x18,%rsp
  401460:	48 89 f2             	mov    %rsi,%rdx
  401463:	48 8d 4e 04          	lea    0x4(%rsi),%rcx //rsp+4
  401467:	48 8d 46 14          	lea    0x14(%rsi),%rax //rsp+0x14
  40146b:	48 89 44 24 08       	mov    %rax,0x8(%rsp) //rsp + 0x8 = rsp +0x14
  401470:	48 8d 46 10          	lea    0x10(%rsi),%rax //rsp + 0x10 = rsp +0x14
  401474:	48 89 04 24          	mov    %rax,(%rsp) //(rsp) = rax
  401478:	4c 8d 4e 0c          	lea    0xc(%rsi),%r9 //r9 = rsi +0xc
  40147c:	4c 8d 46 08          	lea    0x8(%rsi),%r8//r8 = rsi +0x8
  401480:	be c3 25 40 00       	mov    $0x4025c3,%esi //esi = 0x40253
  401485:	b8 00 00 00 00       	mov    $0x0,%eax //eax=0
  40148a:	e8 61 f7 ff ff       	callq  400bf0 <__isoc99_sscanf@plt>
  40148f:	83 f8 05             	cmp    $0x5,%eax
  401492:	7f 05                	jg     401499 <read_six_numbers+0x3d>
  401494:	e8 a1 ff ff ff       	callq  40143a <explode_bomb>
  401499:	48 83 c4 18          	add    $0x18,%rsp
  40149d:	c3                   	retq   
  |
  400f0a:	83 3c 24 01          	cmpl   $0x1,(%rsp)
  400f0e:	74 20                	je     400f30 <phase_2+0x34>
  |
  400f30:	48 8d 5c 24 04       	lea    0x4(%rsp),%rbx //ebx = &(rsp+4)
  400f35:	48 8d 6c 24 18       	lea    0x18(%rsp),%rbp //ebp = rsp+0x18
  400f3a:	eb db                	jmp    400f17 <phase_2+0x1b>
  |
  400f17:	8b 43 fc             	mov    -0x4(%rbx),%eax //eax = *rsp temp = a[i]
  400f1a:	01 c0                	add    %eax,%eax //temp= 2a[i]
  400f1c:	39 03                	cmp    %eax,(%rbx)//(rsp) //if (temp == a[i+1])
  400f1e:	74 05                	je     400f25 <phase_2+0x29>
  400f20:	e8 15 05 00 00       	callq  40143a <explode_bomb>
  400f25:	48 83 c3 04          	add    $0x4,%rbx  //判断下一个 i++
  400f29:	48 39 eb             	cmp    %rbp,%rbx   //判断循环是否终止  if(a[i] == a[5])
  400f2c:	75 e9                	jne    400f17 <phase_2+0x1b>
  400f2e:	eb 0c                	jmp    400f3c <phase_2+0x40>   
  ……………………
  400f3c:	48 83 c4 28          	add    $0x28,%rsp
  400f40:	5b                   	pop    %rbx
  400f41:	5d                   	pop    %rbp
  400f42:	c3                   	retq   

  

调用sscanf,其中rdi是我们的输入。rsi是格式化字符串%d %d %d %d %d %d,根据scanf给eax值就是对输入进行解析,超过5,也就是6可以跳过401494。这个时候发现输入是每四字节存储到栈里,所以输入的第一个数只能是1,然后跳到400f30
设我们输入的数组是a[6],所以偏移就是0x4 *6 = 0x18,根据上面的注释,输入是一个首项为1,公比为2的等比数列的前六项。
1 2 4 8 16 32

phase_3

  400e6f:	48 89 c7             	mov    %rax,%rdi
  400e72:	e8 cc 00 00 00       	callq  400f43 <phase_3>
  400e77:	e8 48 07 00 00       	callq  4015c4 <phase_defused>
  |
  400f43:	48 83 ec 18          	sub    $0x18,%rsp
  400f47:	48 8d 4c 24 0c       	lea    0xc(%rsp),%rcx
  400f4c:	48 8d 54 24 08       	lea    0x8(%rsp),%rdx
  400f51:	be cf 25 40 00       	mov    $0x4025cf,%esi
  400f56:	b8 00 00 00 00       	mov    $0x0,%eax
  400f5b:	e8 90 fc ff ff       	callq  400bf0 <__isoc99_sscanf@plt>
  400f60:	83 f8 01             	cmp    $0x1,%eax
  400f63:	7f 05                	jg     400f6a <phase_3+0x27>
  |
  400f6a:	83 7c 24 08 07       	cmpl   $0x7,0x8(%rsp)
  400f6f:	77 3c                	ja     400fad <phase_3+0x6a>//jump to bomb
  400f71:	8b CSAPP第二個實驗bomblab

CSAPP BombLab Solution (基于IDA pro) (差456+隐藏)

CMU Bomblab 答案

CSAPP Bomb Lab记录

CSAPP实验二:二进制炸弹(Bomb Lab)

CSAPP实验二:二进制炸弹(Bomb Lab)