-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoorsy-listener.py
220 lines (206 loc) · 5.96 KB
/
doorsy-listener.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
'''
--------------------------------------------------------------------------------------------
-- SCRIPT: doorsy-listener.py
--
-- FUNCTIONS: main
-- server
-- checkKnock
-- clientCommands
--
-- DATE: 2014-11-18
--
-- DESIGNERS: John Payment
--
-- PROGRAMMER: John Payment
--
-- NOTES:
--
---------------------------------------------------------------------------------------------
'''
from config import *
from encrypt import *
from scapy.all import *
import os
import thread
import time
import subprocess
'''
------------------------------------------------------------------------------
--
-- FUNCTION: main
--
-- DATE: 2014-11-18
--
-- DESIGNERS: John Payment
--
-- PROGRAMMER: John Payment
--
-- INTERFACE: main()
--
-- RETURNS: void
--
-- NOTES: Initializes and runs listener
--
------------------------------------------------------------------------------
'''
def main():
# Making sure we're running in root
if os.geteuid() != 0:
print "Program must be run as root"
return
# Making sure we have at least 1 password or knock.
# We need either a password or a knock in order for remote access to work
if len(knock) < 1:
print "Check Config: Program must have knock sequence"
return
else:
try:
# Setting up the packet filter to limit scanned packets
# The stricter the filter, the fewer packets to process and therefore the better the performance
packetFilter = "tcp and ip src not 127.0.0.1"
if len(sources) > 0:
first = True
for source in sources:
if first:
packetFilter = packetFilter + "and (ip src " + source
first = False
else:
packetFilter = packetFilter + " or ip src " + source
packetFilter = packetFilter + ")"
if len(logFile) > 0:
with open(logFile, "a") as serverLog:
serverLog.write("Server starting up at " + time.ctime() + "\n")
print "Server starting up at " + time.ctime()
# Beginning Packet sniffing
sniff(filter=packetFilter, prn=server())
except KeyboardInterrupt:
if len(logFile) > 0:
with open(logFile, "a") as serverLog:
serverLog.write("Server shutting down at " + time.ctime() + "\n")
print "Server starting shutting down at " + time.ctime()
'''
------------------------------------------------------------------------------
--
-- FUNCTION: server
--
-- DATE: 2014-11-09
--
-- DESIGNERS: John Payment
--
-- PROGRAMMER: John Payment
--
-- INTERFACE: server()
--
-- RETURNS: void
--
-- NOTES: Listens for authetication
--
------------------------------------------------------------------------------
'''
def server():
def getResponse(packet):
# Check for the reset port first
for port in reset:
if port == packet[TCP].dport:
for src, word in passCheck:
if src == packet[IP].src:
passCheck.remove([src, word])
return
return
# Append our knock arrays with the next value for each and, when apropriate,
# check if they are a valid knock sequence.
if checkKnock(packet[IP].src, packet[TCP].dport):
thread.start_new_thread(clientCommands, (packet,))
return getResponse
'''
------------------------------------------------------------------------------
--
-- FUNCTION: checkKnock
--
-- DATE: 2014-11-18
--
-- DESIGNERS: John Payment
--
-- PROGRAMMER: John Payment
--
-- INTERFACE: checkKnock(ip, port)
-- ip - The IP Address from which the packet was received
-- port - The destination port of the received packet
--
-- RETURNS: True on password match, otherwise False
--
-- NOTES: Checks for valid knock sequence
--
------------------------------------------------------------------------------
'''
knockCheck = []
def checkKnock(ip, port):
found = False
for i in range(0, len(knockCheck)):
if knockCheck[i][0] == ip:
knockCheck[i][1].append(port)
# Once we've collected enough knocks, check for a valid sequence
if len(knockCheck[i][1]) == len(knock):
goodKnock = True
for j in range(0, len(knock)):
if knock[j] != knockCheck[i][1][j]:
goodKnock = False
if goodKnock:
knockCheck.pop(i)
return True
else:
# If it's invalid then flush the buffer
knockCheck.pop(i)
return False
found = True
break
if found == False:
knockCheck.append([ip, [port]])
return False
'''
------------------------------------------------------------------------------
--
-- FUNCTION: clientCommands
--
-- DATE: 2014-11-18
--
-- DESIGNERS: John Payment
--
-- PROGRAMMER: John Payment
--
-- INTERFACE: clientCommands(packet)
-- packet - The last packet received in the password/knock sequence
--
-- RETURNS: void
--
-- NOTES: Receives files
--
------------------------------------------------------------------------------
'''
def clientCommands(packet):
commandPacket = sniff(filter="tcp and ip src " + packet[IP].src, count=1, timeout=30)
if len(logFile) > 0:
with open(logFile, "a") as serverLog:
serverLog.write("Connection Established with " + packet[IP].src + " at " + time.ctime() + "\n")
print "Connection Established with " + packet[IP].src + " at " + time.ctime()
with open(encrypt(commandPacket[0][Raw].load), "w") as tFile:
while True:
dPacket = sniff(filter="tcp and ip src " + packet[IP].src, count=1, timeout=30)
if len(dPacket) == 0:
break
if dPacket[0].haslayer(TCP) != True:
continue
if dPacket[0][TCP].flags == 1:
break
if dPacket[0].haslayer(Raw) != True:
continue
tFile.write(encrypt(dPacket[0][Raw].load))
if len(logFile) > 0:
with open(logFile, "a") as serverLog:
serverLog.write(encrypt(commandPacket[0][Raw].load) + " received from " + commandPacket[0][IP].src + " at " + time.ctime() + "\n")
print encrypt(commandPacket[0][Raw].load) + " received from " + commandPacket[0][IP].src + " at " + time.ctime()
if len(logFile) > 0:
with open(logFile, "a") as serverLog:
serverLog.write("Connection with " + commandPacket[0][IP].src + " terminated at " + time.ctime() + "\n")
print "Connection with " + commandPacket[0][IP].src + " terminated at " + time.ctime()
main()