forked from sjhilt/GasPot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GasPot.py
399 lines (340 loc) · 16.4 KB
/
GasPot.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
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
#######################################################################
# gaspot.py
#
# This is a 'honeypot' to record any commands that were provided to the
# system. This will record the connection, and if any attempts are made.
# This is a basic attempt, with lots of room for improvement.
#
# Authors: Kyle Wilhoit
# Stephen Hilt
#
########################################################################
import socket
import select
import datetime
import random
import ConfigParser
import ast
# Reading configuration information
config = ConfigParser.ConfigParser()
config.read('config.ini')
# Set vars for connection information
TCP_IP = config.get('host', 'tcp_ip')
TCP_PORT = config.getint('host', 'tcp_port')
BUFFER_SIZE = config.get('host', 'buffer_size')
NOW = datetime.datetime.utcnow()
FILLSTART = NOW - datetime.timedelta(minutes=313)
FILLSTOP = NOW - datetime.timedelta(minutes=303)
# Default Product names, changed based on config.ini file
PRODUCT1 = config.get('products', 'product1').ljust(22)
PRODUCT2 = config.get('products', 'product2').ljust(22)
PRODUCT3 = config.get('products', 'product3').ljust(22)
PRODUCT4 = config.get('products', 'product4').ljust(22)
# Create random Numbers for the volumes
#
# this will crate an initial Volume and then the second value based
# off the orig value.
Vol1 = random.randint(1000, 9050)
vol1tc = random.randint(Vol1, Vol1+200)
Vol2 = random.randint(1000, 9050)
vol2tc = random.randint(Vol2, Vol2+200)
Vol3 = random.randint(1000, 9050)
vol3tc = random.randint(Vol3, Vol3+200)
Vol4 = random.randint(1000, 9050)
vol4tc = random.randint(Vol4, Vol4+200)
# unfilled space ULLAGE
ullage1 = str(random.randint(3000, 9999))
ullage2 = str(random.randint(3000, 9999))
ullage3 = str(random.randint(3000, 9999))
ullage4 = str(random.randint(3000, 9999))
# Height of tank
height1 = str(random.randint(25, 75)) + "." + str(random.randint(10, 99))
height2 = str(random.randint(25, 75)) + "." + str(random.randint(10, 99))
height3 = str(random.randint(25, 75)) + "." + str(random.randint(10, 99))
height4 = str(random.randint(25, 75)) + "." + str(random.randint(10, 99))
# Water in tank, this is a variable that needs to be low
h2o1 = str(random.randint(0, 9)) + "." + str(random.randint(10, 99))
h2o2 = str(random.randint(0, 9)) + "." + str(random.randint(10, 99))
h2o3 = str(random.randint(0, 9)) + "." + str(random.randint(10, 99))
h2o4 = str(random.randint(0, 9)) + "." + str(random.randint(10, 99))
# Temperature of the tank, this will need to be between 50 - 60
temp1 = str(random.randint(50, 60)) + "." + str(random.randint(10, 99))
temp2 = str(random.randint(50, 60)) + "." + str(random.randint(10, 99))
temp3 = str(random.randint(50, 60)) + "." + str(random.randint(10, 99))
temp4 = str(random.randint(50, 60)) + "." + str(random.randint(10, 99))
# List for station name, add more names if you want to have this look less like a honeypot
# this should include a list of gas station names based on the country of demployement
station_name = ast.literal_eval(config.get("stations", "list"))
slength = len(station_name)
station = station_name[random.randint(0, slength-1)]
# This function is to set-up up the message to be sent upon a successful I20100 command being sent
# The final message is sent with a current date/time stamp inside of the main loop.
def I20100():
I20100_1 = '''
I20100
'''
I20100_2 = '''
''' + station + '''
IN-TANK INVENTORY
TANK PRODUCT VOLUME TC VOLUME ULLAGE HEIGHT WATER TEMP
1 '''+ PRODUCT1 + '''''' + str(Vol1) + ''' '''+ str(vol1tc) +''' '''+ ullage1 +''' '''+ height1 +''' '''+ h2o1 +''' '''+ temp1 +'''
2 '''+ PRODUCT2 + '''''' + str(Vol2) + ''' '''+ str(vol2tc) +''' '''+ ullage2 +''' '''+ height2 +''' '''+ h2o2 +''' '''+ temp2 +'''
3 '''+ PRODUCT3 + '''''' + str(Vol3) + ''' '''+ str(vol3tc) +''' '''+ ullage3 +''' '''+ height3 +''' '''+ h2o3 +''' '''+ temp3 +'''
4 '''+ PRODUCT4 + '''''' + str(Vol4) + ''' '''+ str(vol4tc) +''' '''+ ullage4 +''' '''+ height4 +''' '''+ h2o4 +''' '''+ temp4 +'''
'''
return I20100_1 + str(TIME.strftime('%m/%d/%Y %H:%M')) + I20100_2
###########################################################################
#
# Only one Tank is listed currently in the I20200 command
#
###########################################################################
def I20200():
I20200_1 = '''
I20200
'''
I20200_2 = '''
''' + station + '''
DELIVERY REPORT
T 1:'''+ PRODUCT1 +'''
INCREASE DATE / TIME GALLONS TC GALLONS WATER TEMP DEG F HEIGHT
END: '''+ str(FILLSTOP.strftime('%m/%d/%Y %H:%M')) +''' '''+ str(Vol1 + 300) +''' '''+ str(vol1tc + 300) +''' '''+ h2o1 +''' '''+ temp1 +''' '''+ height1 +'''
START: '''+ str(FILLSTART.strftime('%m/%d/%Y %H:%M')) +''' '''+ str(Vol1 - 300) +''' '''+ str(vol1tc - 300) +''' '''+ h2o1 +''' '''+ temp1 +''' '''+ str(float(height1) - 23) +'''
AMOUNT: '''+ str(Vol1)+''' '''+ str(vol1tc) +'''
'''
return I20200_1 + str(TIME.strftime('%m/%d/%Y %H:%M')) + I20200_2
###########################################################################
#
# I20300 In-Tank Leak Detect Report
#
###########################################################################
def I20300():
I20300_1 = '''
I20300
'''
I20300_2 = '''
''' + station + '''
TANK 1 '''+ PRODUCT1 +'''
TEST STATUS: OFF
LEAK DATA NOT AVAILABLE ON THIS TANK
TANK 2 '''+ PRODUCT2 +'''
TEST STATUS: OFF
LEAK DATA NOT AVAILABLE ON THIS TANK
TANK 3 '''+ PRODUCT3 +'''
TEST STATUS: OFF
LEAK DATA NOT AVAILABLE ON THIS TANK
TANK 4 '''+ PRODUCT4 +'''
TEST STATUS: OFF
LEAK DATA NOT AVAILABLE ON THIS TANK
'''
return I20300_1 + str(TIME.strftime('%m/%d/%Y %H:%M')) + I20300_2
###########################################################################
# Shift report command I20400 only one item in report at this time,
# but can always add more if needed
###########################################################################
def I20400():
I20400_1 = '''
I20400
'''
I20400_2 = '''
''' + station + '''
SHIFT REPORT
SHIFT 1 TIME: 12:00 AM
TANK PRODUCT
1 '''+ PRODUCT1 +''' VOLUME TC VOLUME ULLAGE HEIGHT WATER TEMP
SHIFT 1 STARTING VALUES ''' + str(Vol1) +''' '''+ str(vol1tc) +''' '''+ ullage1 +''' '''+ height1 +''' '''+ h2o1 +''' '''+ temp1 +'''
ENDING VALUES ''' + str(Vol1 + 940) +''' '''+ str(vol1tc + 886) +''' '''+ str(int(ullage1) + 345) +''' '''+ str(float(height1) + 53)+''' '''+ h2o1 +''' '''+ temp1 +'''
DELIVERY VALUE 0
TOTALS 940
'''
return I20400_1 + str(TIME.strftime('%m/%d/%Y %H:%M')) + I20400_2
###########################################################################
# I20500 In-Tank Status Report
###########################################################################
def I20500():
I20500_1 = '''
I20500
'''
I20500_2 = '''
''' + station + '''
TANK PRODUCT STATUS
1 '''+ PRODUCT1 +''' NORMAL
2 '''+ PRODUCT2 +''' HIGH WATER ALARM
HIGH WATER WARNING
3 '''+ PRODUCT3 +''' NORMAL
4 '''+ PRODUCT4 +''' NORMAL
'''
return I20500_1 + str(TIME.strftime('%m/%d/%Y %H:%M')) + I20500_2
# create the socket, bind, and start listening for incoming connections
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setblocking(0)
server_socket.bind((TCP_IP, TCP_PORT))
server_socket.listen(10)
#
# Infinite Loop to provide a connection available on port 10001
# This is the default port for the AVG's that were found online
# via R7's research.
#
#
active_sockets = [server_socket]
while True:
readable, writeable, errored = select.select(active_sockets, [], [], 5)
for conn in readable:
if conn is server_socket:
new_con, addr = server_socket.accept()
new_con.settimeout(30.0)
active_sockets.append(new_con)
else:
addr = conn.getpeername()
try:
# get current time in UTC
TIME = datetime.datetime.utcnow()
# open log file for recording messages
target = open("all_attempts.log", 'a')
# write out initial connection
target.write(str(datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M')) + \
"- Connection from : %s\n" % addr[0])
# Get the initial data
response = conn.recv(4096)
# The connection has been closed
if not response:
active_sockets.remove(conn)
conn.close()
continue
while not ('\n' in response or '00' in response):
response += conn.recv(4096)
# if first value is not ^A then do nothing
# thanks John(achillean) for the help
if response[0] != '\x01':
target.write(str(datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M')) + \
" - Non ^A Command Attempt from: %s\n" % addr[0])
conn.close()
active_sockets.remove(conn)
continue
# if response is less than 6, than do nothing
if len(response) < 6:
target.write(str(datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M')) + \
" - Non Valid Command Attempt from: %s\n" % addr[0])
conn.close()
active_sockets.remove(conn)
continue
cmds = {"I20100" : I20100, "I20200" : I20200, "I20300" : I20300 , "I20400" : I20400, "I20500" : I20500}
cmd = response[1:7] # strip ^A and \n out
if cmd in cmds:
target.write(str(datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M')) + \
" - %s Command Attempt from: %s\n" % (cmds[cmd], addr[0]))
conn.send(cmds[cmd]())
elif cmd.startswith("S6020"):
# change the tank name
if cmd.startswith("S60201"):
# split string into two, the command, and the data
TEMP = response.split('S60201')
# if length is less than two, print error
if len(TEMP) < 2:
conn.send("9999FF1B\n")
# Else the command was entered correctly and continue
else:
# Strip off the carrage returns and new lines
TEMP1 = TEMP[1].rstrip("\r\n")
# if Length is less than 22
if len(TEMP1) < 22:
# pad the result to have 22 chars
PRODUCT1 = TEMP1.ljust(22)
elif len(TEMP1) > 22:
# else only print 22 chars if the result was longer
PRODUCT1 = TEMP1[:20] + " "
else:
# else it fits fine (22 chars)
PRODUCT1 = TEMP1
#log result
target.write(str(datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M')) + \
" - S60201: "+ TEMP1 +" Command Attempt from: %s\n" % addr[0])
# Follows format for S60201 for comments
elif cmd.startswith("S60202"):
TEMP = response.split('S60202')
if len(TEMP) < 2:
conn.send("9999FF1B\n")
else:
TEMP1 = TEMP[1].rstrip("\r\n")
if len(TEMP1) < 22:
PRODUCT2 = TEMP1.ljust(22)
elif len(TEMP1) > 22:
PRODUCT2 = TEMP1[:20] + " "
else:
PRODUCT2 = TEMP1
target.write(str(datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M')) + \
" - S60202: "+ TEMP1 +" Command Attempt from: %s\n" % addr[0])
# Follows format for S60201 for comments
elif cmd.startswith("S60203"):
TEMP = response.split('S60203')
if len(TEMP) < 2:
conn.send("9999FF1B\n")
else:
TEMP1 = TEMP[1].rstrip("\r\n")
if len(TEMP1) < 22:
PRODUCT3 = TEMP1.ljust(22)
elif len(TEMP1) > 22:
PRODUCT3 = TEMP1[:20] + " "
else:
PRODUCT3 = TEMP1
target.write(str(datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M')) + \
" - S60203: "+ TEMP1 +" Command Attempt from: %s\n" % addr[0])
# Follows format for S60201 for comments
elif cmd.startswith("S60204"):
TEMP = response.split('S60204')
if len(TEMP) < 2:
conn.send("9999FF1B\n")
else:
TEMP1 = TEMP[1].rstrip("\r\n")
if len(TEMP1) < 22:
PRODUCT4 = TEMP1.ljust(22)
elif len(TEMP1) > 22:
PRODUCT4 = TEMP1[:20] + " "
else:
PRODUCT4 = TEMP1
target.write(str(datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M')) + \
" - S60204: "+ TEMP1 +" Command Attempt from: %s\n" % addr[0])
# Follows format for S60201 for comments
elif cmd.startswith("S60200"):
TEMP = response.split('S60200')
if len(TEMP) < 2:
# 9999 indicates that the command was not understood and
# FF1B is the checksum for the 9999
conn.send("9999FF1B\n")
else:
TEMP1 = TEMP[1].rstrip("\r\n")
if len(TEMP1) < 22:
PRODUCT1 = TEMP1.ljust(22)
PRODUCT2 = TEMP1.ljust(22)
PRODUCT3 = TEMP1.ljust(22)
PRODUCT4 = TEMP1.ljust(22)
elif len(TEMP1) > 22:
PRODUCT1 = TEMP1[:20] + " "
PRODUCT2 = TEMP1[:20] + " "
PRODUCT3 = TEMP1[:20] + " "
PRODUCT4 = TEMP1[:20] + " "
else:
PRODUCT1 = TEMP1
PRODUCT2 = TEMP1
PRODUCT3 = TEMP1
PRODUCT4 = TEMP1
target.write(str(datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M')) + \
" - S60200: "+ TEMP1 +" Command Attempt from: %s\n" % addr[0])
else:
conn.send("9999FF1B\n")
# Else it is a currently unsupported command so print the error message found in the manual
# 9999 indicates that the command was not understood and FF1B is the checksum for the 9999
else:
conn.send("9999FF1B\n")
# log what was entered
target.write(str(datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M')) + \
" - Attempt from: %s\n" % addr[0])
target.write(" Command Entered %s\n" % response)
except Exception, e:
print 'Unknown Error: {}'.format(str(e))
raise
except KeyboardInterrupt:
conn.close()
except select.error:
conn.close()
# close log file
target.close()