socat绑定程序到端口

1.使用socat挂载在服务器端口

  1. sudo apt-get install socat
  2. socat tcp-l:端口号,fork exec:程序位置,reuseaddr

nc绑定程序到特定端口

  1. ncat -ve./[prog] -kl [port]

nohup命令(使得关闭终端也能运行题目程序)

  1. First.编写脚本pwn.sh
  2. #!/bin/sh
  3. #name:pwn.sh
  4. socat tcp-l:端口号,fork exec:程序位置,reuseaddr
  5. Next.运行脚本
  6. sudo chmod u+x ./pwn.sh
  7. nohup ./pwn.sh &

docker搭建

查看汇编代码

  1. objdump -d ./

pwntools 检查保护

  1. checksec [prog]

readelf

  1. readelf --program--headers ${binary} 检查nx保护

gdb-peda

生成垃圾数据,计算偏移

  1. pattern create 100
  2. pattern offset AABA

msf

  1. /usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 1200
  2. /usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -q 8Bn9
  3. [*] Exact match at offset 1196

gdb-pwn

生成垃圾数据,计算偏移

  1. cyclic 1000
  2. cyclic -l AABA

gdb 反汇编

hook

  1. 使用钩子,当在断点处停止时会执行gdb 命令
  2. define hook-stop
  3. Type commands for definition of "hook-stop".
  4. End with a line saying just "end".
  5. >info registers
  6. >x/24wx $esp
  7. >x/2i $eip
  8. >end
  9. (gdb) r
  10. 效果
  11. (gdb) del
  12. Delete all breakpoints? (y or n) y
  13. (gdb) break *0x0804840c
  14. Breakpoint 2 at 0x804840c: file stack0/stack0.c, line 11.
  15. (gdb) break *0x8048411
  16. Breakpoint 3 at 0x8048411: file stack0/stack0.c, line 13.
  17. (gdb) define hook-stop
  18. Type commands for definition of "hook-stop".
  19. End with a line saying just "end".
  20. >info registers
  21. >x/24wx $esp
  22. >x/2i $eip
  23. >end
  24. (gdb) r
  25. (gdb) r
  26. The program being debugged has been started already.
  27. Start it from the beginning? (y or n) y
  28. Starting program: /opt/protostar/bin/stack0
  29. eax 0xbffff77c -1073744004
  30. ecx 0x92346446 -1842060218
  31. edx 0x1 1
  32. ebx 0xb7fd7ff4 -1208123404
  33. esp 0xbffff760 0xbffff760
  34. ebp 0xbffff7c8 0xbffff7c8
  35. esi 0x0 0
  36. edi 0x0 0
  37. eip 0x804840c 0x804840c <main+24>
  38. eflags 0x200286 [ PF SF IF ID ]
  39. cs 0x73 115
  40. ss 0x7b 123
  41. ds 0x7b 123
  42. es 0x7b 123
  43. fs 0x0 0
  44. gs 0x33 51
  45. 0xbffff760: 0xbffff77c 0x00000001 0xb7fff8f8 0xb7f0186e
  46. 0xbffff770: 0xb7fd7ff4 0xb7ec6165 0xbffff788 0xb7eada75
  47. 0xbffff780: 0xb7fd7ff4 0x08049620 0xbffff798 0x080482e8
  48. 0xbffff790: 0xb7ff1040 0x08049620 0xbffff7c8 0x08048469
  49. 0xbffff7a0: 0xb7fd8304 0xb7fd7ff4 0x08048450 0xbffff7c8
  50. 0xbffff7b0: 0xb7ec6365 0xb7ff1040 0x0804845b 0x00000000
  51. Error while running hook_stop:
  1. b*main 设置断点
  2. disassembly 反汇编当前函数
  3. disassembly main 反汇编main函数
  4. info proc mappings 查看内存映射情况以及堆栈地址

intel 风格

  1. set disassembly-flavor intel #设置反汇编风格intel或者让att&
  2. set disassembly-flavor att

关闭保护

1.关闭栈保护 -fno-stack-protector
关闭栈可执行(NX,DEP) -zexecstack

  1. gcc -fno-stack-protector -zexecstack bof.c -o bof
  1. gcc -z execstack 开启
  2. 或者安装 execstack :apt install execstack
  3. execstack --set-execstack 开启无需编译
  4. execstack --clear-execstack

2.关掉Stack Protector/Canary(栈保护/金丝雀)

  1. gcc -fno-stack-protector -o level level.c
  2. gcc -o test test.c // 默认情况下,不开启Canary保护
  3. gcc -fno-stack-protector -o test test.c //禁用栈保护
  4. gcc -fstack-protector -o test test.c //启用堆栈保护,不过只为局部变量中含有 char 数组的函数插入保护代码
  5. gcc -fstack-protector-all -o test test.c //启用堆栈保护,为所有函数插入保护代码

3.关掉程序ASLR/PIE(程序随机化保护)

  1. gcc -no-pie level level.c

4.关闭整个linux系统的ASLR保护

  1. sudo -s
  2. echo 0 > /proc/sys/kernel/randomize_va_space
  3. exit

5.打开整个linux系统的ASLR保护

  1. sudo -s
  2. echo 2 > /proc/sys/kernel/randomize_va_space

6.RELRO (Relocation Read Only)

  • default 是Partial ```bash 开启 full gcc -Wl,-z,relro,-z,now 关闭 gcc -Wol,-z,norelro

全部开启RELRO机制Partial-RELRO:在编译时增加 -z now (GOT 不可写) 部分开启RELRO机制Full-RELRO:在编译时增加 -z lazy(默认配置,GOT可写) 关闭RELRO机制: 在编译时增加 -z norelro ```