forked from dichotomy/scorebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Service.py
executable file
·365 lines (347 loc) · 13.3 KB
/
Service.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
'''
Created on Dec 18, 2011
@author: [email protected]
Service.py is a module in the scorebot program. It's purpose is to provide scoring for a given service.
Copyright (C) 2011 Dichotomy
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
'''
import re
import sys
import ftplib
import socket
import traceback
import globalvars
from Logger import Logger
from Scores import Scores
score_str_re = re.compile("Gold Team scoring file, do not touch")
html1_str_re = re.compile("It works")
html2_str_re = re.compile("IIS")
html3_str_re = re.compile("lamp")
html4_str_re = re.compile("Wordpress")
tcp_80_re = re.compile("80/tcp")
tcp_21_re = re.compile("21/tcp")
tcp_25_re = re.compile("25/tcp")
udp_53_re = re.compile("53/udp")
class Service:
'''
classdocs
'''
def __init__(self, port, protocol, value, logger, uri="index.html", \
content=None, username=None, password=None):
'''
Constructor
'''
self.logger = logger
self.port = port
self.protocol = protocol
self.value = float(value)
self.scores = Scores()
self.uri = "GET /%s\r\n\r\n" % uri
self.up = False
self.functional = False
self.content = content
if self.content:
self.content_re = re.compile(str(self.content))
else:
self.content_re = None
if password:
self.password = password
else:
self.password = "abcd1234"
if username:
self.username = username
else:
self.username = "blueteam"
self.mail_re = re.compile("220")
def check(self, this_round, ipaddress, timeout):
service_name = "%s/%s" % (self.port, self.protocol)
myscore = self.value
if tcp_80_re.match(service_name):
############################################
try:
if globalvars.verbose:
self.logger.err("\t\tChecking for service %s/%s..." \
% (self.port, self.protocol))
addrport = [ipaddress, self.port]
mysock = socket.create_connection(addrport, timeout)
mysock.settimeout(timeout)
if globalvars.verbose:
self.logger.err("connected!\n")
except:
if globalvars.verbose:
self.logger.err("there was a problem...\n")
myscore = self.value * 1
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
############################################
try:
if globalvars.verbose:
self.logger.err("\t\t\tTrying to fetch %s..." % self.uri)
mysock.send(self.uri)
if globalvars.verbose:
self.logger.err("requested...\n")
except:
if globalvars.verbose:
self.logger.err("there was a problem:\n")
myscore = self.value * .75
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
############################################
try:
if globalvars.verbose:
self.logger.err("\t\t\tGetting data...")
data = mysock.recv(4098)
if globalvars.verbose:
self.logger.err("fetched\n")
except:
if globalvars.verbose:
self.logger.err("there was a problem...\n")
myscore = self.value * .50
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
############################################
if globalvars.verbose:
self.logger.err("\t\t\tChecking data...")
if self.content_re:
if self.content_re.search(data):
if globalvars.verbose:
self.logger.err("good\n")
self.set_score(this_round, self.value * 0)
return myscore
else:
if globalvars.verbose:
self.logger.err("bad: %s\n" % data)
self.set_score(this_round, self.value * .25)
return myscore
elif score_str_re.search(data):
if globalvars.verbose:
self.logger.err("good\n")
self.set_score(this_round, self.value * 0)
return myscore
elif html1_str_re.search(data):
if globalvars.verbose:
self.logger.err("good\n")
self.set_score(this_round, self.value * 0)
return myscore
elif html2_str_re.search(data):
if globalvars.verbose:
self.logger.err("good\n")
self.set_score(this_round, self.value * 0)
return myscore
elif html3_str_re.search(data):
if globalvars.verbose:
self.logger.err("good\n")
self.set_score(this_round, self.value * 0)
return myscore
elif html4_str_re.search(data):
if globalvars.verbose:
self.logger.err("good\n")
self.set_score(this_round, self.value * 0)
return myscore
else:
if globalvars.verbose:
self.logger.err("bad: %s\n" % data)
self.set_score(this_round, self.value * .25)
return myscore
elif tcp_21_re.match(service_name):
############################################
try:
if globalvars.verbose:
self.logger.err("\t\tChecking for service %s/%s..." \
% (self.port, self.protocol))
myftp = ftplib.FTP(timeout=timeout)
myftp.connect(ipaddress, self.port)
if globalvars.verbose:
self.logger.err("connected!\n")
except:
if globalvars.verbose:
self.logger.err("there was a problem...\n")
myscore = self.value * 1
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
############################################
try:
if globalvars.verbose:
self.logger.err("\t\t\tTrying to login with %s and %s..." % \
(self.username, self.password))
myftp.login(self.username, self.password)
if globalvars.verbose:
self.logger.err("success!\n")
except:
if globalvars.verbose:
self.logger.err("there was a problem...\n")
myscore = self.value * 0.75
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
############################################
try:
if globalvars.verbose:
self.logger.err("\t\t\tTrying to fetch %s..." % self.uri)
filename = "%s.%s" % (ipaddress, self.port)
file_obj = open(filename, "w")
myftp.set_pasv(False)
myftp.retrbinary("RETR scoring_file.txt", file_obj.write)
if globalvars.verbose:
self.logger.err("requested...\n")
except:
if globalvars.verbose:
self.logger.err("there was a problem...\n")
myscore = self.value * 0.50
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
############################################
try:
if globalvars.verbose:
self.logger.err("\t\t\tChecking data...")
file_obj = open(filename)
for line in file_obj:
if score_str_re.match(line):
myscore = self.value * 0
if globalvars.verbose:
self.logger.err("good\n")
else:
myscore = self.value * 0.25
self.set_score(this_round, myscore)
return myscore
except:
if globalvars.verbose:
self.logger.err("bad: %s\n" % data)
myscore = self.value * 0.333
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
elif tcp_25_re.match(service_name):
#self.set_score(this_round, 0)
#self.logger.err("NEED TO IMPLEMENT 25/TCP SCORE CHECKING!!\n")
############################################
try:
if globalvars.verbose:
self.logger.err("\t\tChecking for service %s/%s..." \
% (self.port, self.protocol))
addrport = [ipaddress, self.port]
mysock = socket.create_connection(addrport, timeout)
mysock.settimeout(timeout)
if globalvars.verbose:
self.logger.err("connected!\n")
except:
if globalvars.verbose:
self.logger.err("there was a problem...\n")
myscore = self.value * 1
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
############################################
try:
if globalvars.verbose:
self.logger.err("\t\tSending HELO..." )
mysock.send("HELO mail")
if globalvars.verbose:
self.logger.err("sent...\n")
except:
if globalvars.verbose:
self.logger.err("there was a problem:\n")
myscore = self.value * .75
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
############################################
try:
if globalvars.verbose:
self.logger.err("\t\t\tGetting data...")
data = mysock.recv(1024)
if globalvars.verbose:
self.logger.err("fetched\n")
except:
if globalvars.verbose:
self.logger.err("there was a problem...\n")
myscore = self.value * .50
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
############################################
if globalvars.verbose:
self.logger.err("\t\t\tChecking data...")
if self.mail_re.search(data):
if globalvars.verbose:
self.logger.err("good\n")
self.set_score(this_round, self.value * 0)
return myscore
else:
if globalvars.verbose:
self.logger.err("bad: %s\n" % data)
self.set_score(this_round, self.value * .25)
return myscore
############################################
else:
try:
if globalvars.verbose:
self.logger.err("\t\tChecking for service %s/%s..." \
% (self.port, self.protocol))
addrport = [ipaddress, self.port]
mysock = socket.create_connection(addrport, timeout)
mysock.settimeout(timeout)
if globalvars.verbose:
self.logger.err("connected!\n")
myscore = self.value * 0
self.set_score(this_round, myscore)
return myscore
except:
if globalvars.verbose:
self.logger.err("there was a problem...\n")
myscore = self.value * 1
traceback.print_exc(file=self.logger)
self.set_score(this_round, myscore)
return myscore
service_name = "%s/%s" % (str(self.port), str(self.protocol))
self.set_score(this_round, myscore)
def get_score(self, this_round):
return self.scores.get_score(this_round)
def set_score(self, this_round, value=None):
if value == None:
value = self.value
service_name = "%s/%s" % (str(self.port), str(self.protocol))
if (value == self.value):
self.up = False
self.functional = False
elif (value > 0):
self.up = True
self.functional = False
else:
self.up = True
self.functional = True
if globalvars.binjitsu:
this_value = self.value - value
else:
this_value = value
self.logger.out("Round %s service %s score %s\n" % \
(this_round, service_name, this_value))
self.logger.err("Round %s service %s score %s\n" % \
(this_round, service_name, this_value))
self.scores.set_score(this_round, this_value)
def get_state(self):
# Returns 2 if up and functional
# Returns 1 if up but not functional
# Returns 0 if down
return self.up + self.functional
def main():
myservice = Service(80, "tcp", "HTTP", 500)
result = myservice.check("10.0.1.28", 3)
print "Got %s " % result
if __name__ == "__main__":
main()