深渊之书

notes

本笔记记录二进制相关内容,不限于pwn与逆向,会随着我的学习逐渐添加内容

linxu相关

elf64_asm汇编

生成逻辑与脚本

┌──(wackymaker㉿kali)-[~/tools/code/asm]
└─$ ./asmmake.sh helloword.s
Hello word!
┌──(wackymaker㉿kali)-[~/tools/code/asm]
└─$ cat helloword.s 
global _start

section .data
    message db "Hello word"
    length equ $-message

section .text
_start:
    mov rax, 1
    mov rdi, 1
    mov rsi, message
    mov rdx, length
    syscall

    mov rax, 60
    mov rdi, 0
    syscall

┌──(wackymaker㉿kali)-[~/tools/code/asm]
└─$ cat asmmake.sh 
#!/bin/bash

fileName="${1%%.*}" # remove .s extension

nasm -f elf64 ${fileName}".s"
ld ${fileName}".o" -o ${fileName}
[ "$2" == "-g" ] && gdb -q ${fileName} || ./${fileName}

查找64_linux_syscall

wackymaker@htb[/htb]$ grep exit /usr/include/x86_64-linux-gnu/asm/unistd_64.h

#define __NR_exit 60
#define __NR_exit_group 231

查找其使用参数

wackymaker@htb[/htb]$ man -s 2 exit

...SNIP...
void _exit(int status);

gdb使用

安装gef插件

wget -O ~/.gdbinit-gef.py -q https://gef.blah.cat/py
echo source ~/.gdbinit-gef.py >> ~/.gdbinit

开始反编译

gdb -q ./helloWorld

查找命令相关简要记录(查找)

运行前或运行中

info functions #查找所有函数函数
info variables #查找所有可用变量
info breakpoints #查找所有已经设置的断点
x/s 0x402000 #查找0x402000地址中的值并字符串输出
x/wx 0x401000 #查找0x401000地址中的值并十六进制输出

运行中

registers #查看所有寄存器

disas命令相关简要记录(查函数)

disas $函数名 # 查看函数具体汇编

break命令相关记录(下断点)

b [目标] #在目标地址下断点 比如
b _start #在_start函数起始下断点
b *_start+10 #在_start+10的位置下断点
b *0x401015 #在0x401015处下断点

启动与运行相关指令

启动程序

run

步进

si

步过

step

持续运行至断点或结束

c

pwntools使用

安装

sudo pip3 install pwntools

详细使用文档

https://docs.pwntools.com/en/stable/asm.html

汇编与shellcode转换

汇编转shellcode

pwn asm '$指令'  -c '$架构'
# 例:pwn asm 'push rax'  -c 'amd64'
#  0:    50                       push   eax

shellcode转换汇编

pwn disasm '$shellcode' -c '$架构'
# 例:pwn disasm '50' -c 'amd64'
#  0:    50                       push   eax

应用程序转换shellcode

脚本如下,适应不同架构需要改动

#!/usr/bin/python3

import sys
from pwn import *

context(os="linux", arch="amd64", log_level="error")

file = ELF(sys.argv[1])
shellcode = file.section(".text")
print(shellcode.hex())

使用例如下

python3 shellcoder.py helloworld

48be0020400000000000bf01000000ba12000000b8010000000f05b83c000000bf000000000f05

shellcode_loader

脚本如下,适应不同架构需要改动

#!/usr/bin/python3

import sys
from pwn import *

context(os="linux", arch="amd64", log_level="error")

run_shellcode(unhex(sys.argv[1])).interactive()

使用例如下

python3 loader.py '4831db66bb79215348bb422041636164656d5348bb48656c6c6f204854534889e64831c0b0014831ff40b7014831d2b2120f054831c0043c4030ff0f05'

Hello word!

shellcraft库使用(常用shell载荷)

查找某架构可用载荷

pwn shellcraft -l '$架构.$系统'
# 采取通配使用,所以很好查找,比如查找amd64的linux的sh运行
# pwn shellcraft -l 'amd64.linux.sh'

当查找到后可以显示shellcode

pwn shellcraft amd64.linux.sh

6a6848b82f62696e2f2f2f.......34240101010131f6566a085e4801e6564889e631d26a3b580f05

也可以直接-r运行

pwn shellcraft amd64.linux.sh -r

$ whoami

root

也可以使用python3解释器完全解锁 shellcraft ,并使用带参数的高级系统调用

可以使用dir(shellcraft)列出所有可用系统调用

python3

>>> from pwn import *
>>> context(os="linux", arch="amd64", log_level="error")
>>> dir(shellcraft)

[...SNIP... 'execve', 'exit', 'exit_group', ... SNIP...]

使用系统调用(举例execve)

>>> syscall = shellcraft.execve(path='/bin/sh',argv=['/bin/sh']) # syscall and args
>>> asm(syscall).hex() # print shellcode

'48b801010101010101015048b82e63686f2e726901483104244889e748b801010101010101015048b82e63686f2e7269014831042431f6566a085e4801e6564889e631d26a3b580f05'