pwn笔记

pwntools

http://docs.pwntools.com/en/stable/index.html

Tubes

  • recv(numb=4096, timeout=default)
  • recvline(keepends=True) : keepends为是否保留行尾的\n
  • recvuntil(delims, drop=False) : 一直读到delims的pattern出现为止
  • recvrepeat(timeout=default) : 持续接受直到EOF或timeout
  • recvline_contains(items, keepends=False, timeout=pwnlib.timeout.Timeout.default)
  • recvline_pred(pred, keepends = False)
  • sendlineafter(delim, data, timeout = default)

asm

1
2
3
4
5
>>> asm('nop')
'\x90'
>>> asm('nop', arch='arm')
'\x00\xf0 \xe3'
>>> print disasm('6a0258cd80ebf9'.decode('hex'))

ROP

http://docs.pwntools.com/en/stable/rop.html

gdb

1
2
3
4
5
s = process('./pwnme')
context.terminal = ['gnome-terminal', '-x', 'sh', '-c']
context.terminal = ['tmux', 'splitw', '-h']
gdb.attach(s, execute='b *0x400620\nc\n')
gdb.attach(proc.pidof(s)[0])

DynELF

http://docs.pwntools.com/en/stable/dynelf.html

format

http://docs.pwntools.com/en/stable/fmtstr.html#

构造格式化字符串

fmtstr_payload(offset, writes, numbwritten=0, write_size=’byte’) → str

自动构造格式化的函数

FmtStr(execute_fmt, offset=None, padlen=0, numbwritten=0)

other

log等级

context.log_level = 'debug'

加载指定libc

p = process('./pwn',env= {"LD_PRELOAD":"./libc.so"})

多个:

p = process('./glibc_2.26/tcache_dup',env= {"LD_PRELOAD":'./glibc_versions/ld-2.26.so ./glibc_versions/libc-2.26.so'})

kali - libc

libc = ELF('/lib/x86_64-linux-gnu/libc-2.27.so')

heap

doc

ctf-wiki

https://ctf-wiki.github.io/ctf-wiki/pwn/heap/heap_structure/

Heap Exploitation

https://heap-exploitation.dhavalkapil.com

Glibc Heap简介

http://brieflyx.me/2016/heap/glibc-heap/

how2heap

https://github.com/shellphish/how2heap

bins

Ptmalloc 一共 维护了 128 个 bin,并使用一个数组来存储这些 bin。数组中的第一个为 unsorted bin,数组中从 2 开始编号的前 64 个 bin 称为 small bins,同 一个small bin中的chunk具有相同的大小。两个相邻的small bin中的chunk大小相差8bytes。Small bins后面的bin被称作large bins。large bins中的每一个bin分别包含了一个给定范围 内的 chunk,其中的 chunk 按大小序排列。ptmalloc 使用“smallest-first,best-fit”原则在空闲 large bins 中查找合适的 chunk。除了fast bin是单向链表LIFO外,其余都是双向循环链表且为FIFO。

gcc

  • -m32, 编译成32位
  • -s, 删除符号表
  • -g, 加入调试信息
  • -no-pie
  • -fno-stack-protector
  • -Wno-format-security

gdb

tips

指定libc
LD_PRELOAD=glibc_versions/libc-2.26.so glibc_versions/ld-2.26.so ./glibc_2.26/tcache_dup

gdb预加载libc
gdb -iex 'set exec-wrapper env LD_PRELOAD="./glibc_versions/libc-2.26.so" "./glibc_versions/ld-2.26.so"' ./glibc_2.26/tcache_dup

pwndbg

https://github.com/pwndbg/pwndbg

usage:

https://github.com/pwndbg/pwndbg/blob/dev/FEATURES.md

Docker

查看所有容器
docker ps -a

查看所有容器ID
docker ps -a -q

stop停止所有容器
docker stop $(docker ps -a -q)

remove删除所有容器
docker rm $(docker ps -a -q)

删除所有镜像
docker rmi $(docker images -q)

启动容器
docker run --name="pwn" --privileged -v ~/Downloads/:/home/dir -it d001um3/pwn /bin/zsh

启动shell
docker exec -it pwn /bin/zsh

设置代理,走宿主机ss的http代理
export http_proxy=http://192.1.1.141:1087

更新镜像
docker commit -a 'd001um3' -m 'messages' container_id d001um3/pwn

Dockerfile

1
2
3
4
5
FROM ubuntu:16.04

RUN sed -i "s/http:\/\/archive.ubuntu.com/http:\/\/mirrors.tuna.tsinghua.edu.cn/g" /etc/apt/sources.list
RUN apt-get update && apt-get -y dist-upgrade
RUN apt-get update && apt-get install -y sudo lib32z1 lib32ncurses5 lib32stdc++6 xinetd build-essential m4 wget curl python2.7 python-pip python-dev ipython git libssl-dev libffi-dev gdb vim socat zsh tmux

解决中文乱码

1
2
3
4
apt-get install locales
locale-gen zh_CN.UTF-8
# echo "export LC_ALL=zh_CN.UTF-8" >> ~/.zshrc
export LC_ALL=zh_CN.UTF-8

tools

LibcSearcher

https://github.com/lieanu/LibcSearcher

1
2
3
4
5
6
7
8
from LibcSearcher import *

#第二个参数,为已泄露的实际地址,或最后12位(比如:d90),int类型
obj = LibcSearcher("fgets", 0X7ff39014bd90)

obj.dump("system") #system 偏移
obj.dump("str_bin_sh") #/bin/sh 偏移
obj.dump("__libc_start_main_ret")

ROPgadget

https://github.com/JonathanSalwan/ROPgadget

ROPgadget --binary rop --only 'pop|ret' | grep 'eax'

socat

新建一个 run.sh

socat tcp-l:5002,reuseaddr,fork exec:"python server.py"

后台启动

nohup ./run.sh &

tmux

https://www.cnblogs.com/kevingrace/p/6496899.html

  • ctrl+b " 模向分隔窗口
  • ctrl+b % 纵向分隔窗口
  • ctrl+b 方向键 移动光标以选择面板
  • ctrl+b o 在当前窗口中选择下一面板
  • ctrl+b d 脱离当前会话
  • tmux attach 能够重新进入之前的会话
  • ctrl+b fn+↑ 向上翻页