[27] | 1 | |
---|
[36] | 2 | |
---|
[27] | 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 | gtkdir = 'C:/Program Files/Fichiers communs/GTK/' |
---|
| 29 | |
---|
| 30 | import os |
---|
| 31 | import binascii |
---|
| 32 | import struct |
---|
| 33 | import time |
---|
| 34 | os.environ['PATH'] += ";%s/lib;%s/bin" % (gtkdir, gtkdir) |
---|
| 35 | |
---|
| 36 | import threading |
---|
| 37 | import string |
---|
| 38 | import re |
---|
| 39 | import socket |
---|
| 40 | import sys |
---|
| 41 | import gtk |
---|
| 42 | import traceback |
---|
| 43 | import pango |
---|
| 44 | import commands |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | IPversion = 4 |
---|
| 48 | localAddr = '' |
---|
| 49 | localAddr6 = '2001:660:3001:4012:30ec:9207:2a75:1583' |
---|
| 50 | localPort = 2222 |
---|
| 51 | collectorAddr = "193.51.183.225" |
---|
| 52 | collectorAddr6 = "2001:660:3000:1011:10::" |
---|
| 53 | collectorPort = 52571 |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | myInputControlThread = None |
---|
| 58 | printValue = [ 0,1,1,0,1,0,0,1,1,0, |
---|
| 59 | 0,1,1,0,0,0,0,0,0,0, |
---|
| 60 | 1,0,0,0,0,0,0,1,1,0, |
---|
| 61 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 62 | 0,0,0,0,0,0,1,1,0,0, |
---|
| 63 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 64 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 65 | 1,1,1,1,1,1,1,1,1,1, |
---|
| 66 | 0,0,0,0,0,0,0,0,0,0 |
---|
| 67 | ] |
---|
| 68 | firstParseValue = [ 0,0,0,0,0,0,0,0,0,0, |
---|
| 69 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 70 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 71 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 72 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 73 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 74 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 75 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 76 | 0,0,0,0,0,0,0,0,0,0 |
---|
| 77 | ] |
---|
| 78 | checkUpValues = [ 0,0,0,0,0,0,0,0,0,0, |
---|
| 79 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 80 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 81 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 82 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 83 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 84 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 85 | 0,0,0,0,0,0,0,0,0,0, |
---|
| 86 | 0,0,0,0,0,0,0,0,0,0 |
---|
| 87 | ] |
---|
| 88 | fieldsName = [ "","IN_BYT","IN_PKTS","","PROT","TOS","TCP_FL","S_PORT","IPv4@S","S_MASK", |
---|
| 89 | "InSNMP","D_PORT","IPv4@D","D_MASK","OutSNMP","NextHop","S_AS","D_AS","","", |
---|
| 90 | "","Time","","","","","","IPv6@S","IPv6@D","", |
---|
| 91 | "","","","","","","","","","", |
---|
| 92 | "","","","","","","M_T_L_T","M_T_L_@","","", |
---|
| 93 | "","","","","","","","","","", |
---|
| 94 | "","","","","","","","","","ROUTER", |
---|
| 95 | "M_L_1","M_L_2","M_L_3","M_L_4","M_L_5","M_L_6","M_L_7","M_L_8","M_L_9","M_L_10", |
---|
| 96 | "","","","","","","","","","" |
---|
| 97 | ] |
---|
| 98 | flowCpt = 0 |
---|
| 99 | mask = [ 0,0,0 ] |
---|
| 100 | spaceSep = " " |
---|
| 101 | tabSep = "\t" |
---|
| 102 | freeze = 0 |
---|
| 103 | record = 0 |
---|
| 104 | record_file_name = "" |
---|
| 105 | record_file = None |
---|
| 106 | is_already_see = 0 |
---|
| 107 | collectorRule="" |
---|
| 108 | coll_window = gtk.Window() |
---|
| 109 | router=0 |
---|
| 110 | tpl_def = {} |
---|
| 111 | os_type = 0 |
---|
| 112 | oldTpl = 0 |
---|
| 113 | fromRouter = "" |
---|
| 114 | |
---|
| 115 | class InputControl(threading.Thread): |
---|
| 116 | def kill(self, timeout): |
---|
| 117 | self.imRunning = 0 |
---|
| 118 | time.sleep(1) |
---|
| 119 | print "\n.\n..\n..." |
---|
| 120 | time.sleep(1) |
---|
| 121 | print "....\n.....\n......" |
---|
| 122 | time.sleep(1) |
---|
| 123 | if self.myFlowInput: |
---|
| 124 | self.myFlowInput.socket_close() |
---|
| 125 | self.join(timeout) |
---|
| 126 | |
---|
| 127 | def __init__(self, address, port): |
---|
| 128 | self.imRunning = 1 |
---|
| 129 | self.currentflow = None |
---|
| 130 | threading.Thread.__init__(self) |
---|
| 131 | self.myFlowInput = FlowInput(address, port) |
---|
| 132 | |
---|
| 133 | def run(self): |
---|
| 134 | global freeze, record |
---|
| 135 | while self.imRunning: |
---|
| 136 | if self.myFlowInput: |
---|
| 137 | tmp = self.myFlowInput.get_flow() |
---|
| 138 | if (tmp[0]!=None): |
---|
| 139 | self.currentflow = Flow(tmp) |
---|
| 140 | if (freeze==0) & self.currentflow.enable(): |
---|
| 141 | self.currentflow.print_flow2() |
---|
| 142 | if (record==1) & self.currentflow.enable(): |
---|
| 143 | self.currentflow.write_flow2() |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | class Flow: |
---|
| 148 | def __init__(self, data): |
---|
| 149 | self.flow = [ data[0], data[1] ] |
---|
| 150 | |
---|
| 151 | def enable(self): |
---|
| 152 | global firstParseValue, checkUpValues, tpl_def |
---|
| 153 | res = 1 |
---|
| 154 | for i in range (0, len(tpl_def[self.flow[0]])): |
---|
| 155 | f = tpl_def[self.flow[0]][i][0] |
---|
| 156 | if ( f==8 or f==12 or f==15 or f==18 or f==47) and ( checkUpValues[f]==1 ): |
---|
| 157 | res = res & ( ((struct.unpack('>L',(socket.inet_aton(self.flow[1][i])))[0] & mask[1-1]) == firstParseValue[f])| ((struct.unpack('>L',(socket.inet_aton(self.flow[1][i])))[0] & mask[1-1]) == firstParseValue[f]) ) |
---|
| 158 | elif ( i==27 or i==28 ): |
---|
| 159 | pass |
---|
| 160 | elif (checkUpValues[f]==1): |
---|
| 161 | res = res & (self.flow[1][i] == firstParseValue[f]) |
---|
| 162 | return res |
---|
| 163 | |
---|
| 164 | def print_flow2(self): |
---|
| 165 | global printValue, tpl_def, flowCpt, fieldsName, oldTpl, os_type, fromRouter |
---|
| 166 | myliste = [] |
---|
| 167 | myTpl = [] |
---|
| 168 | strFlow = "" |
---|
| 169 | strField = "" |
---|
| 170 | underscore_line = "" |
---|
| 171 | flowCpt+=1 |
---|
| 172 | if (flowCpt%20 == 0 or oldTpl != self.flow[0]): |
---|
| 173 | flowCpt = 0 |
---|
| 174 | for i in range (0, len(tpl_def[self.flow[0]])): |
---|
| 175 | f = tpl_def[self.flow[0]][i][0] |
---|
| 176 | if printValue[f]: |
---|
| 177 | if ( f==27 or f==28 or f==62 or f==63 ): |
---|
| 178 | strField += str(fieldsName[f]) |
---|
| 179 | l = len(str(fieldsName[f])) |
---|
| 180 | if ((40-l)%8 == 0): |
---|
| 181 | tabNb = int((40-l)/8) |
---|
| 182 | else: |
---|
| 183 | tabNb = int((40-l)/8) + 1 |
---|
| 184 | for j in range (0, tabNb): |
---|
| 185 | strField += "\t" |
---|
| 186 | for k in range (0, 40): |
---|
| 187 | underscore_line += "-" |
---|
| 188 | elif ( f==8 or f==15 or f==12 or f==18 or f==47 ): |
---|
| 189 | strField += str(fieldsName[f]) |
---|
| 190 | l = len(str(fieldsName[f])) |
---|
| 191 | if ((16-l)%8 == 0): |
---|
| 192 | tabNb = int((16-l)/8) |
---|
| 193 | else: |
---|
| 194 | tabNb = int((16-l)/8) + 1 |
---|
| 195 | for j in range (0, tabNb): |
---|
| 196 | strField += "\t" |
---|
| 197 | for k in range (0, 16): |
---|
| 198 | underscore_line += "-" |
---|
| 199 | else: |
---|
| 200 | strField += str(fieldsName[f]) |
---|
| 201 | strField += "\t" |
---|
| 202 | for k in range (0, 8): |
---|
| 203 | underscore_line += "-" |
---|
| 204 | if (oldTpl != self.flow[0]): |
---|
| 205 | print " " |
---|
| 206 | if (os_type == 0): |
---|
| 207 | esc = '\x1b[' |
---|
| 208 | sep = ';' |
---|
| 209 | end = 'm' |
---|
| 210 | if (printValue[69]): |
---|
| 211 | rt = str(fieldsName[69]) |
---|
| 212 | toprint = esc+"37"+sep+"44"+end+strField+rt+esc+"0"+end |
---|
| 213 | else: |
---|
| 214 | toprint = esc+"37"+sep+"44"+end+strField+esc+"0"+end |
---|
| 215 | print toprint |
---|
| 216 | else: |
---|
| 217 | if (printValue[69]): |
---|
| 218 | strField += str(fieldsName[69]) |
---|
| 219 | print strField |
---|
| 220 | print underscore_line |
---|
| 221 | for i in range (0, len(tpl_def[self.flow[0]])): |
---|
| 222 | f = tpl_def[self.flow[0]][i][0] |
---|
| 223 | if printValue[f]: |
---|
| 224 | if ( f==27 or f==28 or f==62 or f==63 ): |
---|
| 225 | strFlow += str(self.flow[1][i]) |
---|
| 226 | l = len(str(self.flow[1][i])) |
---|
| 227 | if ((40-l)%8 == 0): |
---|
| 228 | tabNb = int((40-l)/8) |
---|
| 229 | else: |
---|
| 230 | tabNb = int((40-l)/8) + 1 |
---|
| 231 | for j in range (0, tabNb): |
---|
| 232 | strFlow += "\t" |
---|
| 233 | elif ( f==8 or f==12 or f==15 or f==18 or f==47 ): |
---|
| 234 | strFlow += str(self.flow[1][i]) |
---|
| 235 | l = len(str(self.flow[1][i])) |
---|
| 236 | if ((16-l)%8 == 0): |
---|
| 237 | tabNb = int((16-l)/8) |
---|
| 238 | else: |
---|
| 239 | tabNb = int((16-l)/8) + 1 |
---|
| 240 | for j in range (0, tabNb): |
---|
| 241 | strFlow += "\t" |
---|
| 242 | elif (f==21): |
---|
| 243 | myliste += [str(self.flow[1][i]-self.flow[1][i+1])] |
---|
| 244 | strFlow += str(self.flow[1][i]-self.flow[1][i+1]) |
---|
| 245 | strFlow += "\t" |
---|
| 246 | elif (f==22): |
---|
| 247 | pass |
---|
| 248 | else: |
---|
| 249 | myliste += [str(self.flow[1][i])] |
---|
| 250 | strFlow += str(self.flow[1][i]) |
---|
| 251 | for k in range (len(str(self.flow[1][i])), 7): |
---|
| 252 | strFlow += " " |
---|
| 253 | strFlow += "\t" |
---|
| 254 | if (printValue[69]): |
---|
| 255 | strFlow += fromRouter |
---|
| 256 | print strFlow |
---|
| 257 | oldTpl = self.flow[0] |
---|
| 258 | |
---|
| 259 | def write_flow2(self): |
---|
| 260 | global printValue, record_file, tpl_def |
---|
| 261 | myliste = [] |
---|
| 262 | for i in range (0, len(tpl_def[self.flow[0]])): |
---|
| 263 | f = tpl_def[self.flow[0]][i][0] |
---|
| 264 | if printValue[f]: |
---|
| 265 | if (f==21): |
---|
| 266 | myliste += [str(self.flow[1][i]-self.flow[1][i+1])] |
---|
| 267 | elif (f==22): |
---|
| 268 | pass |
---|
| 269 | else: |
---|
| 270 | myliste += [str(self.flow[1][i])] |
---|
| 271 | theflow = string.join(myliste, "\t")+"\n" |
---|
| 272 | record_file.write(theflow) |
---|
| 273 | |
---|
| 274 | class FlowInput: |
---|
| 275 | def __init__(self, h, p): |
---|
| 276 | self.HOST = h |
---|
| 277 | self.PORT = p |
---|
| 278 | if (IPversion == 4): |
---|
| 279 | self.socketnumber = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
| 280 | elif (IPversion == 6): |
---|
| 281 | self.socketnumber = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) |
---|
| 282 | else: |
---|
| 283 | print "IP protocol ", IPversion ," not supported" |
---|
| 284 | exit(0) |
---|
| 285 | self.socketnumber.bind((self.HOST, self.PORT)) |
---|
| 286 | |
---|
| 287 | def get_flow(self): |
---|
| 288 | global collectorRule, tpl_def, os_type, fromRouter |
---|
| 289 | if (self.socketnumber!=None): |
---|
| 290 | data = self.socketnumber.recvfrom(1024) |
---|
| 291 | flow = data[0] |
---|
| 292 | code = struct.unpack('<H', flow[0:2]) |
---|
| 293 | if (code[0] == 1): |
---|
| 294 | routerSrc = struct.unpack('<L', flow[2:6]) |
---|
| 295 | sourceId = struct.unpack('<L', flow[6:10]) |
---|
| 296 | tplId = struct.unpack('<H', flow[10:12]) |
---|
| 297 | mykeylst = [] |
---|
| 298 | mykeylst += [str(routerSrc[0])] |
---|
| 299 | mykeylst += [str(sourceId[0])] |
---|
| 300 | mykeylst += [str(tplId[0])] |
---|
| 301 | mykeystr = string.join(mykeylst,""); |
---|
| 302 | fields = [] |
---|
| 303 | fieldNb = struct.unpack('<H', flow[12:14]) |
---|
| 304 | for i in range(0, fieldNb[0]): |
---|
| 305 | fd = struct.unpack('<H',flow[14+(i*4):16+(i*4)]) |
---|
| 306 | tp = struct.unpack('<H',flow[14+(2+i*4):16+(2+i*4)]) |
---|
| 307 | fields.insert(i,(fd[0],tp[0])) |
---|
| 308 | tpl_def[mykeystr] = fields |
---|
| 309 | return ( None, None) |
---|
| 310 | if (code[0]==11): |
---|
| 311 | routerSrc = struct.unpack('<L', flow[4:8]) |
---|
| 312 | tmp = struct.unpack('<L', flow[4:8]) |
---|
| 313 | fromRouter = socket.inet_ntoa(struct.pack('>L', tmp[0])) |
---|
| 314 | sourceId = struct.unpack('<L', flow[8:12]) |
---|
| 315 | tplId = struct.unpack('<H', flow[12:14]) |
---|
| 316 | mykeylst = [] |
---|
| 317 | mykeylst += [str(routerSrc[0])] |
---|
| 318 | mykeylst += [str(sourceId[0])] |
---|
| 319 | mykeylst += [str(tplId[0])] |
---|
| 320 | mykeystr = string.join(mykeylst,"") |
---|
| 321 | if (tpl_def.has_key(mykeystr)== True): |
---|
| 322 | field_list = [] |
---|
| 323 | index = 0 |
---|
| 324 | for i in range (0, len(tpl_def[mykeystr])): |
---|
| 325 | if (tpl_def[mykeystr][i][1]==1): |
---|
| 326 | value = struct.unpack('<B',flow[14+index:15+index]) |
---|
| 327 | field_list.insert(i,value[0]) |
---|
| 328 | if (tpl_def[mykeystr][i][1]==2): |
---|
| 329 | value = struct.unpack('>H',flow[14+index:16+index]) |
---|
| 330 | field_list.insert(i,value[0]) |
---|
| 331 | if (tpl_def[mykeystr][i][1]==3): |
---|
| 332 | value = struct.unpack('<BBB',flow[14+index:17+index]) |
---|
| 333 | valueTmp = struct.pack('<BBBB',value[2],value[1],value[0],0) |
---|
| 334 | valueFinal = struct.unpack('<L', valueTmp) |
---|
| 335 | valueFinal2 = (valueFinal[0])>>4 |
---|
| 336 | field_list.insert(i,valueFinal2) |
---|
| 337 | if (tpl_def[mykeystr][i][1]==4): |
---|
| 338 | if (tpl_def[mykeystr][i][0]==8 or tpl_def[mykeystr][i][0]==12 or tpl_def[mykeystr][i][0]==15 or tpl_def[mykeystr][i][0]==18 or tpl_def[mykeystr][i][0]==47): |
---|
| 339 | value = socket.inet_ntoa(flow[14+index:18+index]) |
---|
| 340 | field_list.insert(i,value) |
---|
| 341 | else: |
---|
| 342 | value = struct.unpack('<L', flow[14+index:18+index]) |
---|
| 343 | value = struct.unpack('>L', flow[14+index:18+index]) |
---|
| 344 | field_list.insert(i,value[0]) |
---|
| 345 | if (tpl_def[mykeystr][i][1]==16): |
---|
| 346 | if (os_type==1): |
---|
| 347 | field_list.insert(i,nt_inet_ntop(flow[14+index:30+index])) |
---|
| 348 | else: |
---|
| 349 | tmp = socket.inet_ntop(socket.AF_INET6,flow[14+index:30+index]) |
---|
| 350 | field_list.insert(i,tmp) |
---|
| 351 | index += tpl_def[mykeystr][i][1] |
---|
| 352 | return mykeystr, field_list |
---|
| 353 | |
---|
| 354 | |
---|
| 355 | |
---|
| 356 | return ( None, None) |
---|
| 357 | return ( None, None) |
---|
| 358 | |
---|
| 359 | def socket_close(self): |
---|
| 360 | if self.socketnumber: |
---|
| 361 | try : |
---|
| 362 | self.socketnumber.shutdown(2) |
---|
| 363 | self.socketnumber = None |
---|
| 364 | except socket.error: |
---|
| 365 | pass |
---|
| 366 | |
---|
| 367 | class FileSel: |
---|
| 368 | def file_ok_sel(self, w): |
---|
| 369 | global record, record_file, record_file_name |
---|
| 370 | record = 1 |
---|
| 371 | record_file_name = self.fileSel.get_filename() |
---|
| 372 | record_file = file(record_file_name, 'w') |
---|
| 373 | print "%s" % self.fileSel.get_filename() |
---|
| 374 | self.fileSel.destroy() |
---|
| 375 | |
---|
| 376 | def destroy(self, widget): |
---|
| 377 | self.fileSel.destroy() |
---|
| 378 | |
---|
| 379 | def __init__(self): |
---|
| 380 | self.fileSel = gtk.FileSelection("File Selection") |
---|
| 381 | self.fileSel.connect("destroy", self.destroy) |
---|
| 382 | self.fileSel.ok_button.connect("clicked", self.file_ok_sel) |
---|
| 383 | self.fileSel.cancel_button.connect("clicked", |
---|
| 384 | lambda w: self.fileSel.destroy()) |
---|
| 385 | self.fileSel.set_filename("flow_record.txt") |
---|
| 386 | self.fileSel.show() |
---|
| 387 | |
---|
| 388 | def print_hello(widget, event): |
---|
| 389 | print "Hello, World!" |
---|
| 390 | |
---|
| 391 | def nt_inet_ntop(packed_ip): |
---|
| 392 | cpt = 0 |
---|
| 393 | double = 0 |
---|
| 394 | string_ip = "" |
---|
| 395 | tmp = binascii.hexlify(packed_ip[0:2]) |
---|
| 396 | if ( int(tmp,16)!=0 ): |
---|
| 397 | string_ip += tmp |
---|
| 398 | tmp = binascii.hexlify(packed_ip[2:4]) |
---|
| 399 | if ( int(tmp,16)==0 ): |
---|
| 400 | cpt+=1 |
---|
| 401 | double = 1 |
---|
| 402 | if (cpt==1): |
---|
| 403 | string_ip += ":" |
---|
| 404 | else: |
---|
| 405 | cpt=0 |
---|
| 406 | string_ip += ":"+tmp |
---|
| 407 | tmp = binascii.hexlify(packed_ip[4:6]) |
---|
| 408 | if ( int(tmp,16)==0 ): |
---|
| 409 | cpt+=1 |
---|
| 410 | double = 1 |
---|
| 411 | if (cpt==1): |
---|
| 412 | string_ip += ":" |
---|
| 413 | else: |
---|
| 414 | cpt=0 |
---|
| 415 | string_ip += ":"+tmp |
---|
| 416 | tmp = binascii.hexlify(packed_ip[6:8]) |
---|
| 417 | if ( int(tmp,16)==0 ): |
---|
| 418 | if (double==1 and cpt==0): |
---|
| 419 | string_ip += ":0" |
---|
| 420 | else: |
---|
| 421 | cpt+=1 |
---|
| 422 | double = 1 |
---|
| 423 | if (cpt==1): |
---|
| 424 | string_ip += ":" |
---|
| 425 | else: |
---|
| 426 | cpt=0 |
---|
| 427 | string_ip += ":"+tmp |
---|
| 428 | tmp = binascii.hexlify(packed_ip[8:10]) |
---|
| 429 | if ( int(tmp,16)==0 ): |
---|
| 430 | if (double==1 and cpt==0): |
---|
| 431 | string_ip += ":0" |
---|
| 432 | else: |
---|
| 433 | cpt+=1 |
---|
| 434 | double = 1 |
---|
| 435 | if (cpt==1): |
---|
| 436 | string_ip += ":" |
---|
| 437 | else: |
---|
| 438 | cpt=0 |
---|
| 439 | string_ip += ":"+tmp |
---|
| 440 | tmp = binascii.hexlify(packed_ip[10:12]) |
---|
| 441 | if ( int(tmp,16)==0 ): |
---|
| 442 | if (double==1 and cpt==0): |
---|
| 443 | string_ip += ":0" |
---|
| 444 | else: |
---|
| 445 | cpt+=1 |
---|
| 446 | double = 1 |
---|
| 447 | if (cpt==1): |
---|
| 448 | string_ip += ":" |
---|
| 449 | else: |
---|
| 450 | cpt=0 |
---|
| 451 | string_ip += ":"+tmp |
---|
| 452 | tmp = binascii.hexlify(packed_ip[12:14]) |
---|
| 453 | if ( int(tmp,16)==0 ): |
---|
| 454 | if (double==1 and cpt==0): |
---|
| 455 | string_ip += ":0" |
---|
| 456 | else: |
---|
| 457 | cpt+=1 |
---|
| 458 | double = 1 |
---|
| 459 | if (cpt==1): |
---|
| 460 | string_ip += ":" |
---|
| 461 | else: |
---|
| 462 | cpt=0 |
---|
| 463 | string_ip += ":"+tmp |
---|
| 464 | tmp = binascii.hexlify(packed_ip[14:16]) |
---|
| 465 | if ( int(tmp,16) != 0 ): |
---|
| 466 | string_ip += ":"+tmp |
---|
| 467 | else: |
---|
| 468 | string_ip += ":" |
---|
| 469 | else: |
---|
| 470 | string_ip = "::" |
---|
| 471 | return string_ip |
---|
| 472 | |
---|
| 473 | def get_main_menu(self, window): |
---|
| 474 | accel_group = gtk.AccelGroup() |
---|
| 475 | item_factory = gtk.ItemFactory(gtk.MenuBar, "<main>", accel_group) |
---|
| 476 | item_factory.create_items(menu_items) |
---|
| 477 | window.add_accel_group(accel_group) |
---|
| 478 | item_factory = item_factory |
---|
| 479 | return item_factory.get_widget("<main>") |
---|
| 480 | |
---|
| 481 | def reception(widget, event): |
---|
| 482 | global myInputControlThread |
---|
| 483 | global localAddr, localPort |
---|
| 484 | myInputControlThread = InputControl(localAddr, localPort) |
---|
| 485 | myInputControlThread.start() |
---|
| 486 | |
---|
| 487 | def close_recept(widget, event): |
---|
| 488 | global myInputControlThread |
---|
| 489 | if myInputControlThread != None: |
---|
| 490 | myInputControlThread.kill(1) |
---|
| 491 | myInputControlThread = None |
---|
| 492 | print "\n\n\n\n\n Input Stream closed. \n\n\n\n\n" |
---|
| 493 | |
---|
| 494 | def about_dialog(widget, event): |
---|
| 495 | dialog = gtk.MessageDialog( |
---|
| 496 | parent = None, |
---|
| 497 | flags = gtk.DIALOG_MODAL, |
---|
| 498 | type = gtk.MESSAGE_INFO, |
---|
| 499 | buttons = gtk.BUTTONS_OK, |
---|
| 500 | message_format = "Author : FX Andreu\nemail andreu@renater.fr\nCopyright (C) 2005 GIP RENATER\n\nThis file is part of renetcol.\n\nrenetcol 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.\n\nrenetcol 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.\n\nYou should have received a copy of the GNU General Public License along with renetcol; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ") |
---|
| 501 | dialog.set_title('About') |
---|
| 502 | dialog.connect('response', lambda dialog, response: dialog.destroy()) |
---|
| 503 | dialog.show() |
---|
| 504 | |
---|
| 505 | def check_dialog(widget, event, text): |
---|
| 506 | dialog = gtk.MessageDialog( |
---|
| 507 | parent = None, |
---|
| 508 | flags = gtk.DIALOG_MODAL, |
---|
| 509 | type = gtk.MESSAGE_INFO, |
---|
| 510 | buttons = gtk.BUTTONS_OK, |
---|
| 511 | message_format = text) |
---|
| 512 | dialog.set_title('Value check ') |
---|
| 513 | dialog.connect('response', lambda dialog, response: dialog.destroy()) |
---|
| 514 | dialog.show() |
---|
| 515 | |
---|
| 516 | def info_dialog(widget, event, text): |
---|
| 517 | dialog = gtk.MessageDialog( |
---|
| 518 | parent = None, |
---|
| 519 | flags = gtk.DIALOG_MODAL, |
---|
| 520 | type = gtk.MESSAGE_INFO, |
---|
| 521 | buttons = gtk.BUTTONS_OK, |
---|
| 522 | message_format = text) |
---|
| 523 | dialog.set_title('Information ') |
---|
| 524 | dialog.connect('response', lambda dialog, response: dialog.destroy()) |
---|
| 525 | dialog.show() |
---|
| 526 | |
---|
| 527 | def callback_freeze(widget, data=None): |
---|
| 528 | global freeze |
---|
| 529 | if widget.get_active() == 1: |
---|
| 530 | freeze = 1 |
---|
| 531 | print "FREEZE ACTIF" |
---|
| 532 | if widget.get_active() == 0: |
---|
| 533 | freeze = 0 |
---|
| 534 | print "FREEZE NOT ACTIF" |
---|
| 535 | |
---|
| 536 | def wind_destroy(widget, data=None): |
---|
| 537 | widget.destroy() |
---|
| 538 | |
---|
| 539 | def callback_record(widget, data=None): |
---|
| 540 | global record, record_file, record_file_name |
---|
| 541 | if widget.get_active() == 1: |
---|
| 542 | FileSel() |
---|
| 543 | if widget.get_active() == 0: |
---|
| 544 | record = 0 |
---|
| 545 | time.sleep(2) |
---|
| 546 | record_file.close() |
---|
| 547 | |
---|
| 548 | def strsend(target, message): |
---|
| 549 | global collectorAddr, collectorPort |
---|
| 550 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
| 551 | s.connect((collectorAddr, collectorPort)) |
---|
| 552 | s.send(message) |
---|
| 553 | s.close() |
---|
| 554 | |
---|
| 555 | def callback_refresh(widget, parent): |
---|
| 556 | global collectorAddr |
---|
| 557 | strsend(collectorAddr, 'REFRESH') |
---|
| 558 | parent.destroy() |
---|
| 559 | |
---|
| 560 | def callback_delete_rule(widget, parent): |
---|
| 561 | global collectorAddr, collectorPort |
---|
| 562 | if widget.get_active() == 1: |
---|
| 563 | sent_rule(widget, "DELETE") |
---|
| 564 | widget.set_active(0) |
---|
| 565 | parent.destroy() |
---|
| 566 | |
---|
| 567 | def callback_check_rule(widget, data=None): |
---|
| 568 | global router |
---|
| 569 | if widget.get_active()==1: |
---|
| 570 | router=1 |
---|
| 571 | |
---|
| 572 | def callback_collector(widget, controlWindow): |
---|
| 573 | global is_already_see, collectorRule, myInputControlThread, coll_window, collectorAddr |
---|
| 574 | if ((myInputControlThread == None) and (widget.get_active() == 1)): |
---|
| 575 | info_dialog(widget, None, "You must before activated the stream reception ! (File menu)") |
---|
| 576 | widget.set_active(0) |
---|
| 577 | else: |
---|
| 578 | if widget.get_active() == 1: |
---|
| 579 | widget.set_active(0) |
---|
| 580 | coll_window.connect("destroy", wind_destroy) |
---|
| 581 | coll_window.set_title("Collector control ") |
---|
| 582 | coll_window.set_size_request(500, 250) |
---|
| 583 | coll_vbox = gtk.VBox(False, 1) |
---|
| 584 | coll_vbox.set_border_width(1) |
---|
| 585 | coll_window.add(coll_vbox) |
---|
| 586 | coll_frame = gtk.Frame("The prefix/IP address/router, validate by enter") |
---|
| 587 | coll_vbox.pack_start(coll_frame, False, False, 5) |
---|
| 588 | coll_hbox = gtk.VBox(False, 0) |
---|
| 589 | coll_hbox.set_border_width(5) |
---|
| 590 | coll_frame.add(coll_hbox) |
---|
| 591 | button = gtk.CheckButton(" router ") |
---|
| 592 | button.connect("toggled", callback_check_rule, None) |
---|
| 593 | coll_hbox.pack_start(button, False, False, 0) |
---|
| 594 | mainTT.set_tip(button, "Check it to capture all flows from one router and enter the IPv4 address of this router. IN TEST") |
---|
| 595 | button.show() |
---|
| 596 | coll_pbbox = gtk.HButtonBox() |
---|
| 597 | coll_hbox.pack_start(coll_pbbox, False, True, 5) |
---|
| 598 | prefix_entry(45, "Required Format : \n <field> <operation> <address>\nlike 8 = 10.0.0.1 \nor 8 = 10.0.1.0/24 \nor 27 = 2001::1 \nor 27 = 2001::/64 (IPv6 address only on Linux system)\nonly '=' operator for the moment\n------------------------------\nIn this version 0.0.7, you can compare the following fields:\nROUTER_SRC_ADDR: 0\nIPV4_SRC_ADDR: 8\nIPV4_DST_ADDR: 12\nIPV4_NEXT_HOP: 15\nBGP_IPV4_NEXT_HOP: 18\nIPV6_SRC_ADDR: 27\nIPV6_DST_ADDR: 28\nIPV6_NEXT_HOP: 62\nBPG_IPV6_NEXT_HOP: 63\nIP_PROTOCOL_VERSION: 60\n---------------------------", coll_pbbox, "", 0) |
---|
| 599 | coll_frame2 = gtk.Frame("Disable rules") |
---|
| 600 | coll_vbox.pack_start(coll_frame2, False, False, 5) |
---|
| 601 | coll_hbox2 = gtk.VBox(False, 0) |
---|
| 602 | coll_hbox2.set_border_width(5) |
---|
| 603 | coll_frame2.add(coll_hbox2) |
---|
| 604 | coll_pbbox2 = gtk.HButtonBox() |
---|
| 605 | coll_hbox2.pack_start(coll_pbbox2, False, True, 5) |
---|
| 606 | buttondelete = gtk.ToggleButton("Delete") |
---|
| 607 | buttondelete.connect("toggled", callback_delete_rule, coll_window) |
---|
| 608 | mainTT.set_tip(buttondelete, "Delete all your rules of the collector configuration.") |
---|
| 609 | coll_hbox2.pack_start(buttondelete, False, False, 2) |
---|
| 610 | buttondelete.show() |
---|
| 611 | coll_vbox.show() |
---|
| 612 | coll_window.show_all() |
---|
| 613 | if (is_already_see==0): |
---|
| 614 | info_dialog(widget, None, "You should registered only one rule for each GUI parser ! Think of using the DELETE button !") |
---|
| 615 | is_already_see=1 |
---|
| 616 | |
---|
| 617 | def callback_fields_printed( widget, data=None): |
---|
| 618 | global printValue |
---|
| 619 | printValue[data] = widget.get_active() |
---|
| 620 | |
---|
| 621 | def callback_check_ip( widget, data=None): |
---|
| 622 | global src_Or_Dst_IP |
---|
| 623 | if widget.get_active()==1: |
---|
| 624 | if checkUpValues[1]: |
---|
| 625 | src_Or_Dst_IP = 1 |
---|
| 626 | else: |
---|
| 627 | widget.set_active(0) |
---|
| 628 | check_dialog(widget, None, "First, enter a source address.") |
---|
| 629 | else: |
---|
| 630 | src_Or_Dst_IP = 0 |
---|
| 631 | |
---|
| 632 | def callback_comboR(widget, data=None): |
---|
| 633 | global comboRouter, routerHash |
---|
| 634 | nop = 0 |
---|
| 635 | print struct.unpack('>L',socket.inet_aton(routerHash[comboRouter.entry.get_text()])) |
---|
| 636 | |
---|
| 637 | def callback_src_mask(widget, data=None): |
---|
| 638 | nop = 0 |
---|
| 639 | |
---|
| 640 | def callback_dst_mask(widget, data=None): |
---|
| 641 | nop = 0 |
---|
| 642 | |
---|
| 643 | def callback_tos(widget, data=None): |
---|
| 644 | nop = 0 |
---|
| 645 | |
---|
| 646 | def callback_prot(widget, data=None): |
---|
| 647 | nop = 0 |
---|
| 648 | |
---|
| 649 | def create_button(title, state, callback_function, ptr_var, parent, tooltip_Text): |
---|
| 650 | global mainTT |
---|
| 651 | button = gtk.ToggleButton(title) |
---|
| 652 | if state==1: |
---|
| 653 | button.set_active(True) |
---|
| 654 | else: |
---|
| 655 | button.set_active(False) |
---|
| 656 | button.connect("toggled", callback_function, ptr_var) |
---|
| 657 | if tooltip_Text!=None: |
---|
| 658 | mainTT.set_tip(button, tooltip_Text) |
---|
| 659 | parent.pack_start(button, False, True, 2) |
---|
| 660 | button.show() |
---|
| 661 | |
---|
| 662 | def create_entry(maxLen, tooltip_Text, parent, lab, id): |
---|
| 663 | global mainTT |
---|
| 664 | vbox = gtk.VBox(False, 0) |
---|
| 665 | parent.pack_start(vbox, False, True, 0) |
---|
| 666 | label = gtk.Label(lab) |
---|
| 667 | label.set_alignment(0, 0.5) |
---|
| 668 | vbox.pack_start(label, False, True, 0) |
---|
| 669 | entry = gtk.Entry(max=maxLen) |
---|
| 670 | entry.set_max_length(maxLen) |
---|
| 671 | entry.connect("activate", enter_callback, entry, id) |
---|
| 672 | entry.set_text("") |
---|
| 673 | entry.select_region(0, len(entry.get_text())) |
---|
| 674 | if tooltip_Text!=None: |
---|
| 675 | mainTT.set_tip(entry, tooltip_Text) |
---|
| 676 | vbox.pack_start(entry, False, True, 0) |
---|
| 677 | entry.show() |
---|
| 678 | |
---|
| 679 | def sent_rule(widget, msg): |
---|
| 680 | global collectorAddr, collectorPort, collectorAddr6, IPversion |
---|
| 681 | s = None |
---|
| 682 | if (IPversion==4): |
---|
| 683 | tmpaf = socket.AF_INET |
---|
| 684 | addr = collectorAddr |
---|
| 685 | port = collectorPort |
---|
| 686 | elif (IPversion == 6): |
---|
| 687 | tmpaf = socket.AF_INET6 |
---|
| 688 | addr = collectorAddr6 |
---|
| 689 | port = collectorPort |
---|
| 690 | else: |
---|
| 691 | print "Wrong IP version : ", IPversion |
---|
| 692 | exit(0) |
---|
| 693 | for res in socket.getaddrinfo(addr, port, tmpaf, socket.SOCK_STREAM): |
---|
| 694 | af, socktype, proto, canonname, sa = res |
---|
| 695 | try: |
---|
| 696 | s = socket.socket(af, socktype, proto) |
---|
| 697 | except socket.error, msg: |
---|
| 698 | s = None |
---|
| 699 | continue |
---|
| 700 | try: |
---|
| 701 | s.connect(sa) |
---|
| 702 | except socket.error, msg: |
---|
| 703 | s.close() |
---|
| 704 | s = None |
---|
| 705 | continue |
---|
| 706 | break |
---|
| 707 | if s is None: |
---|
| 708 | check_dialog( widget, None, "could not open socket") |
---|
| 709 | else: |
---|
| 710 | s.send(msg) |
---|
| 711 | check_dialog( widget, None, "Action was sent") |
---|
| 712 | s.close() |
---|
| 713 | |
---|
| 714 | def prefix_callback( widget, entry, id): |
---|
| 715 | global collPrefix, coll_window, router, localPort |
---|
| 716 | entry_text = entry.get_text() |
---|
| 717 | is_good = 0 |
---|
| 718 | if (id == 0): |
---|
| 719 | if ( entry_text==""): |
---|
| 720 | collPrefix = 0 |
---|
| 721 | else: |
---|
| 722 | rule = entry_text.split(' ') |
---|
| 723 | field = rule[0] |
---|
| 724 | operator = rule[1] |
---|
| 725 | value = rule[2] |
---|
| 726 | if (field=='0' or field=='8' or field=='12' or field=='15' or field=='18' or field=='47'): |
---|
| 727 | if (re.match('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(/\d{1,2})?$',value)): |
---|
| 728 | splitter = value.split('/') |
---|
| 729 | adip = struct.unpack('>L',socket.inet_aton(splitter[0]))[0] |
---|
| 730 | if len(splitter)>1: |
---|
| 731 | bits = int(splitter[1]) |
---|
| 732 | if bits <= 32 and bits > 0: |
---|
| 733 | mask[1] = 0xffffffffL << (32 - bits) |
---|
| 734 | is_good = 1 |
---|
| 735 | else: |
---|
| 736 | check_dialog( widget, None, "Wrong value in mask!") |
---|
| 737 | is_good = 0 |
---|
| 738 | else: |
---|
| 739 | mask[1] = 0xffffffffL |
---|
| 740 | is_good = 1 |
---|
| 741 | else: |
---|
| 742 | check_dialog( widget, None, "Wrong value !") |
---|
| 743 | if (field=='27' or field=='28' or field=='62'): |
---|
| 744 | ipv6ad_mask = value.split('/') |
---|
| 745 | if len(ipv6ad_mask)==1: |
---|
| 746 | try: |
---|
| 747 | socket.gethostbyaddr(value) |
---|
| 748 | is_good = 1 |
---|
| 749 | except socket.gaierror: |
---|
| 750 | check_dialog( widget, None, "Wrong value !") |
---|
| 751 | except socket.herror: |
---|
| 752 | check_dialog( widget, None, "I can't resolv !") |
---|
| 753 | except socket.error: |
---|
| 754 | check_dialog( widget, None, "Error in gethostbyaddr()!") |
---|
| 755 | else: |
---|
| 756 | is_good = 1 |
---|
| 757 | |
---|
| 758 | if (field=='60' or field=='46'): |
---|
| 759 | if (int(value) < 256): |
---|
| 760 | is_good = 1 |
---|
| 761 | if (is_good==1): |
---|
| 762 | |
---|
| 763 | |
---|
| 764 | msg = str(localPort) +" "+ field +" "+ operator+" "+ value |
---|
| 765 | sent_rule(widget, msg) |
---|
| 766 | widget.set_editable(False) |
---|
| 767 | coll_window.destroy() |
---|
| 768 | else: |
---|
| 769 | check_dialog( widget, None, "Internal program error ;) !") |
---|
| 770 | |
---|
| 771 | def prefix_entry(maxLen, tooltip_Text, parent, lab, id): |
---|
| 772 | global mainTT |
---|
| 773 | vbox = gtk.VBox(False, 0) |
---|
| 774 | parent.pack_start(vbox, False, True, 0) |
---|
| 775 | label = gtk.Label(lab) |
---|
| 776 | label.set_alignment(0, 0.5) |
---|
| 777 | vbox.pack_start(label, False, True, 0) |
---|
| 778 | entry = gtk.Entry(max=maxLen) |
---|
| 779 | entry.set_max_length(maxLen) |
---|
| 780 | entry.connect("activate", prefix_callback, entry, id) |
---|
| 781 | entry.set_text("") |
---|
| 782 | entry.select_region(0, len(entry.get_text())) |
---|
| 783 | if tooltip_Text!=None: |
---|
| 784 | mainTT.set_tip(entry, tooltip_Text) |
---|
| 785 | vbox.pack_start(entry, False, True, 0) |
---|
| 786 | entry.show() |
---|
| 787 | |
---|
| 788 | def user_quit(widget, data=None): |
---|
| 789 | global myInputControlThread, localPort, record_file |
---|
| 790 | if myInputControlThread != None: |
---|
| 791 | myInputControlThread.kill(1) |
---|
| 792 | myInputControlThread = None |
---|
| 793 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
| 794 | s.connect(('localhost', localPort)) |
---|
| 795 | s.send("FIN") |
---|
| 796 | s.close() |
---|
| 797 | if (record_file!=None): |
---|
| 798 | record_file.close() |
---|
| 799 | print "\n\n\n\n\n Input Stream closed. \n\n\n\n\n By :-) \n\n\n\n" |
---|
| 800 | gtk.main_quit() |
---|
| 801 | |
---|
| 802 | def enter_callback( widget, entry, id): |
---|
| 803 | global firstParseValue |
---|
| 804 | entry_text = entry.get_text() |
---|
| 805 | if id == 0: |
---|
| 806 | if (entry_text==""): |
---|
| 807 | checkUpValues[0] = 0 |
---|
| 808 | else: |
---|
| 809 | if (re.search('\D',entry_text)) or (int(entry_text) >= 99) or (int(entry_text) <= 0): |
---|
| 810 | check_dialog( widget, None, "Wrong value !") |
---|
| 811 | else: |
---|
| 812 | firstParseValue[0] = int(entry_text) |
---|
| 813 | checkUpValues[0] = 1 |
---|
| 814 | if id == 1: |
---|
| 815 | if ( entry_text==""): |
---|
| 816 | checkUpValues[8] = 0 |
---|
| 817 | checkUpValues[12] = 0 |
---|
| 818 | else: |
---|
| 819 | if (re.match('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(/\d{1,2})?$',entry_text)): |
---|
| 820 | splitter = entry_text.split('/') |
---|
| 821 | adip = struct.unpack('>L',socket.inet_aton(splitter[0]))[0] |
---|
| 822 | if len(splitter)>1: |
---|
| 823 | bits = int(splitter[1]) |
---|
| 824 | if bits <= 32 and bits > 0: |
---|
| 825 | mask[0] = 0xffffffffL << (32 - bits) |
---|
| 826 | else: |
---|
| 827 | check_dialog( widget, None, "Wrong value in mask!") |
---|
| 828 | else: |
---|
| 829 | mask[0] = 0xffffffffL |
---|
| 830 | firstParseValue[8] = adip & mask[0] |
---|
| 831 | checkUpValues[8] = 1 |
---|
| 832 | firstParseValue[12] = adip & mask[0] |
---|
| 833 | checkUpValues[12] = 1 |
---|
| 834 | else: |
---|
| 835 | check_dialog( widget, None, "Wrong value !") |
---|
| 836 | if id == 2: |
---|
| 837 | if ( entry_text==""): |
---|
| 838 | checkUpValues[2] = 0 |
---|
| 839 | else: |
---|
| 840 | check_dialog( widget, None, "NOT YET IMPLEMEMTED") |
---|
| 841 | |
---|
| 842 | |
---|
| 843 | |
---|
| 844 | |
---|
| 845 | |
---|
| 846 | |
---|
| 847 | |
---|
| 848 | |
---|
| 849 | |
---|
| 850 | |
---|
| 851 | if id == 3: |
---|
| 852 | if ( entry_text==""): |
---|
| 853 | checkUpValues[15] = 0 |
---|
| 854 | else: |
---|
| 855 | if (re.match('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(/\d{1,2})?$',entry_text)): |
---|
| 856 | splitter = entry_text.split('/') |
---|
| 857 | adip = struct.unpack('>L',socket.inet_aton(splitter[0]))[0] |
---|
| 858 | if len(splitter)>1: |
---|
| 859 | bits = int(splitter[1]) |
---|
| 860 | if bits <= 32 and bits > 0: |
---|
| 861 | mask[2] = 0xffffffffL << (32 - bits) |
---|
| 862 | else: |
---|
| 863 | check_dialog( widget, None, "Wrong value in mask!") |
---|
| 864 | else: |
---|
| 865 | mask[2] = 0xffffffffL |
---|
| 866 | firstParseValue[15] = adip & mask[2] |
---|
| 867 | checkUpValues[15] = 1 |
---|
| 868 | else: |
---|
| 869 | check_dialog( widget, None, "Wrong value !") |
---|
| 870 | if id == 4: |
---|
| 871 | if ( entry_text==""): |
---|
| 872 | checkUpValues[10] = 0 |
---|
| 873 | checkUpValues[14] = 0 |
---|
| 874 | else: |
---|
| 875 | if (re.search('\D',entry_text)) or (int(entry_text) > 65535) or (int(entry_text) < 0): |
---|
| 876 | check_dialog( widget, None, "Wrong value !") |
---|
| 877 | else: |
---|
| 878 | firstParseValue[10] = int(entry_text) |
---|
| 879 | checkUpValues[10] = 1 |
---|
| 880 | firstParseValue[14] = int(entry_text) |
---|
| 881 | checkUpValues[14] = 1 |
---|
| 882 | if id == 6: |
---|
| 883 | if ( entry_text==""): |
---|
| 884 | checkUpValues[1] = 0 |
---|
| 885 | else: |
---|
| 886 | size_list = string.split(entry_text) |
---|
| 887 | if (len(size_list)!=2)or((re.search('\D',size_list[0]))or(int(size_list[0])>999999999)or(int(size_list[0])<=0) or (re.search('\D',size_list[1]))or(int(size_list[1])>999999999)or(int(size_list[1])<=0)): |
---|
| 888 | check_dialog( widget, None, "Wrong value !") |
---|
| 889 | else: |
---|
| 890 | firstParseValue[1] = int(size_list[0]) |
---|
| 891 | if ( int(size_list[1]) == 999999999 ): |
---|
| 892 | firstParseValue[1] = 100000000000 |
---|
| 893 | else: |
---|
| 894 | firstParseValue[1] = int(size_list[1]) |
---|
| 895 | checkUpValues[1] = 1 |
---|
| 896 | if id == 7: |
---|
| 897 | if ( entry_text==""): |
---|
| 898 | checkUpValues[2] = 0 |
---|
| 899 | else: |
---|
| 900 | if (re.search('\D',entry_text)) or (int(entry_text) > 9999) or (int(entry_text) <= 0): |
---|
| 901 | check_dialog( widget, None, "Wrong value !") |
---|
| 902 | else: |
---|
| 903 | firstParseValue[2] = int(entry_text) |
---|
| 904 | checkUpValues[2] = 1 |
---|
| 905 | if id == 8: |
---|
| 906 | if ( entry_text==""): |
---|
| 907 | checkUpValues[0] = 0 |
---|
| 908 | else: |
---|
| 909 | if (re.search('\D',entry_text)) or (int(entry_text) > 99) or (int(entry_text) < 0): |
---|
| 910 | check_dialog( widget, None, "Wrong value !") |
---|
| 911 | else: |
---|
| 912 | firstParseValue[0] = int(entry_text) |
---|
| 913 | checkUpValues[0] = 1 |
---|
| 914 | if id == 9: |
---|
| 915 | if ( entry_text==""): |
---|
| 916 | checkUpValues[7] = 0 |
---|
| 917 | else: |
---|
| 918 | port_list = string.split(entry_text) |
---|
| 919 | if (len(port_list)!=2)or((re.search('\D',port_list[0])) or (int(port_list[0]) > 65535) or (int(port_list[0]) <= 0) or (re.search('\D',port_list[1])) or (int(port_list[1]) > 65535) or (int(port_list[1]) <= 0)): |
---|
| 920 | check_dialog( widget, None, "Wrong value !") |
---|
| 921 | else: |
---|
| 922 | firstParseValue[7] = int(port_list[0]) |
---|
| 923 | firstParseValue[11] = int(port_list[1]) |
---|
| 924 | checkUpValues[7] = 1 |
---|
| 925 | if id == 11: |
---|
| 926 | if ( entry_text==""): |
---|
| 927 | checkUpValues[6] = 0 |
---|
| 928 | else: |
---|
| 929 | if (re.search('\D',entry_text)) or (int(entry_text) > 255) or (int(entry_text) <= 0): |
---|
| 930 | check_dialog( widget, None, "Wrong value !") |
---|
| 931 | else: |
---|
| 932 | firstParseValue[6] = int(entry_text) |
---|
| 933 | checkUpValues[6] = 1 |
---|
| 934 | if id == 12: |
---|
| 935 | if ( entry_text==""): |
---|
| 936 | checkUpValues[4] = 0 |
---|
| 937 | else: |
---|
| 938 | if (re.search('\D',entry_text)) or (int(entry_text) > 255) or (int(entry_text) <= 0): |
---|
| 939 | check_dialog( widget, None, "Wrong value !") |
---|
| 940 | else: |
---|
| 941 | firstParseValue[4] = int(entry_text) |
---|
| 942 | checkUpValues[4] = 1 |
---|
| 943 | if id == 13: |
---|
| 944 | if ( entry_text==""): |
---|
| 945 | checkUpValues[5] = 0 |
---|
| 946 | else: |
---|
| 947 | if (re.search('\D',entry_text)) or (int(entry_text) > 255) or (int(entry_text) < 0): |
---|
| 948 | check_dialog( widget, None, "Wrong value !") |
---|
| 949 | else: |
---|
| 950 | firstParseValue[5] = int(entry_text) |
---|
| 951 | checkUpValues[5] = 1 |
---|
| 952 | if id == 14: |
---|
| 953 | if ( entry_text==""): |
---|
| 954 | checkUpValues[16] = 0 |
---|
| 955 | else: |
---|
| 956 | if (re.search('\D',entry_text)) or (int(entry_text) > 65535) or (int(entry_text) < 0): |
---|
| 957 | check_dialog( widget, None, "Wrong value !") |
---|
| 958 | else: |
---|
| 959 | firstParseValue[16] = int(entry_text) |
---|
| 960 | checkUpValues[16] = 1 |
---|
| 961 | if id == 15: |
---|
| 962 | if ( entry_text==""): |
---|
| 963 | checkUpValues[17] = 0 |
---|
| 964 | else: |
---|
| 965 | if (re.search('\D',entry_text)) or (int(entry_text) > 65535) or (int(entry_text) < 0): |
---|
| 966 | check_dialog( widget, None, "Wrong value !") |
---|
| 967 | else: |
---|
| 968 | firstParseValue[17] = int(entry_text) |
---|
| 969 | checkUpValues[17] = 1 |
---|
| 970 | if id == 18: |
---|
| 971 | if ( entry_text==""): |
---|
| 972 | checkUpValues[69] = 0 |
---|
| 973 | else: |
---|
| 974 | if (re.match('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}?$',entry_text)): |
---|
| 975 | adip = struct.unpack('>L',socket.inet_aton(entry_text))[0] |
---|
| 976 | firstParseValue[69] = adip |
---|
| 977 | checkUpValues[69] = 1 |
---|
| 978 | else: |
---|
| 979 | check_dialog( widget, None, "Wrong value !") |
---|
| 980 | |
---|
| 981 | if __name__ == "__main__": |
---|
| 982 | global window, mainTT, comboRouter, os_type, printShit |
---|
| 983 | |
---|
| 984 | gtk.threads_init() |
---|
| 985 | |
---|
| 986 | if (os.name =="nt"): |
---|
| 987 | os_type = 1 |
---|
| 988 | elif (os.name =="posix"): |
---|
| 989 | os_type = 0 |
---|
| 990 | else: |
---|
| 991 | os_type = 3 |
---|
| 992 | |
---|
| 993 | print "---------------------------------------------------------------" |
---|
| 994 | print "In this version 0.0.7, you can apply rules on the collector based on the following fields:" |
---|
| 995 | print "IPV4_SRC_ADDR: 8," |
---|
| 996 | print "IPV4_DST_ADDR: 12," |
---|
| 997 | print "IPV4_NEXT_HOP: 15," |
---|
| 998 | print "BGP_IPV4_NEXT_HOP: 18," |
---|
| 999 | print "IPV6_SRC_ADDR: 27," |
---|
| 1000 | print "IPV6_DST_ADDR: 28," |
---|
| 1001 | print "MPLS_TOP_LABEL_TYPE: 46," |
---|
| 1002 | print "IPV6_NEXT_HOP: 62," |
---|
| 1003 | print "BPG_IPV6_NEXT_HOP: 63," |
---|
| 1004 | print "IP_PROTOCOL_VERSION: 60, please note that the IPv4 template don't have this field" |
---|
[36] | 1005 | print "ROUTER_SRC_ADDR: 0, !!! it's not a real number for a field, but it's a great function" |
---|
[27] | 1006 | print "----------------------------------------------------------------" |
---|
| 1007 | |
---|
| 1008 | mainTT = gtk.Tooltips() |
---|
| 1009 | window = gtk.Window(gtk.WINDOW_TOPLEVEL) |
---|
| 1010 | window.connect("destroy", gtk.main_quit, "WM destroy") |
---|
| 1011 | window.set_title("RenetcolGUI") |
---|
| 1012 | window.set_size_request(850, 550) |
---|
| 1013 | |
---|
| 1014 | main_vbox = gtk.VBox(False, 1) |
---|
| 1015 | main_vbox.set_border_width(1) |
---|
| 1016 | window.add(main_vbox) |
---|
| 1017 | |
---|
| 1018 | |
---|
| 1019 | file_menu = gtk.Menu() |
---|
| 1020 | receive_item = gtk.MenuItem("Receive Stream") |
---|
| 1021 | stop_item = gtk.MenuItem("Stop Stream") |
---|
| 1022 | quit_item = gtk.MenuItem("Quit") |
---|
| 1023 | file_menu.append(receive_item) |
---|
| 1024 | file_menu.append(stop_item) |
---|
| 1025 | file_menu.append(quit_item) |
---|
| 1026 | receive_item.connect("activate", reception, None) |
---|
| 1027 | stop_item.connect("activate", close_recept, None) |
---|
| 1028 | quit_item.connect("activate", user_quit, None) |
---|
| 1029 | receive_item.show() |
---|
| 1030 | stop_item.show() |
---|
| 1031 | quit_item.show() |
---|
| 1032 | |
---|
| 1033 | help_menu = gtk.Menu() |
---|
| 1034 | about_item = gtk.MenuItem("About") |
---|
| 1035 | help_menu.append(about_item) |
---|
| 1036 | about_item.connect("activate", about_dialog, None) |
---|
| 1037 | about_item.show() |
---|
| 1038 | |
---|
| 1039 | menu_bar = gtk.MenuBar() |
---|
| 1040 | main_vbox.pack_start(menu_bar, False, False, 0) |
---|
| 1041 | menu_bar.show() |
---|
| 1042 | file_item = gtk.MenuItem("File") |
---|
| 1043 | file_item.show() |
---|
| 1044 | help_item = gtk.MenuItem("Help") |
---|
| 1045 | help_item.show() |
---|
| 1046 | |
---|
| 1047 | file_item.set_submenu(file_menu) |
---|
| 1048 | menu_bar.append(file_item) |
---|
| 1049 | menu_bar.append(file_menu) |
---|
| 1050 | file_item.set_right_justified(0) |
---|
| 1051 | |
---|
| 1052 | help_item.set_submenu(help_menu) |
---|
| 1053 | menu_bar.append(help_item) |
---|
| 1054 | menu_bar.append(help_menu) |
---|
| 1055 | help_item.set_right_justified(1) |
---|
| 1056 | |
---|
| 1057 | |
---|
| 1058 | frame = gtk.Frame("Input Stream Control") |
---|
| 1059 | main_vbox.pack_start(frame, False, True, 0) |
---|
| 1060 | hbox = gtk.HBox(False, 0) |
---|
| 1061 | hbox.set_border_width(5) |
---|
| 1062 | frame.add(hbox) |
---|
| 1063 | |
---|
| 1064 | |
---|
| 1065 | buttonfreeze = gtk.ToggleButton("Freeze") |
---|
| 1066 | buttonfreeze.connect("toggled", callback_freeze, None) |
---|
| 1067 | mainTT.set_tip(buttonfreeze, "Stop/Start the output in the window.") |
---|
| 1068 | hbox.pack_start(buttonfreeze, False, False, 2) |
---|
| 1069 | buttonfreeze.show() |
---|
| 1070 | |
---|
| 1071 | buttonrecord = gtk.ToggleButton("Record") |
---|
| 1072 | buttonrecord.connect("toggled", callback_record, None) |
---|
| 1073 | mainTT.set_tip(buttonrecord, "Stop/Start the record in a text file.") |
---|
| 1074 | hbox.pack_start(buttonrecord, False, False, 2) |
---|
| 1075 | buttonrecord.show() |
---|
| 1076 | |
---|
| 1077 | buttoncollector = gtk.ToggleButton("Collector Control") |
---|
| 1078 | buttoncollector.connect("toggled", callback_collector, None) |
---|
| 1079 | mainTT.set_tip(buttoncollector, "Change the rule which is applied on the collector. You must have activated the input stream before.") |
---|
| 1080 | hbox.pack_start(buttoncollector, False, False, 2) |
---|
| 1081 | buttoncollector.show() |
---|
| 1082 | |
---|
| 1083 | ipv4_vbox = gtk.VBox(False, 1) |
---|
| 1084 | ipv4_vbox.set_border_width(1) |
---|
| 1085 | main_vbox.add(ipv4_vbox) |
---|
| 1086 | |
---|
| 1087 | frame = gtk.Frame("Select printed fields") |
---|
| 1088 | ipv4_vbox.pack_start(frame, False, False, 0) |
---|
| 1089 | |
---|
| 1090 | vbox = gtk.VBox(False, 0) |
---|
| 1091 | vbox.set_border_width(5) |
---|
| 1092 | frame.add(vbox) |
---|
| 1093 | bbox = gtk.HButtonBox() |
---|
| 1094 | vbox.pack_start(bbox, False, True, 5) |
---|
| 1095 | |
---|
| 1096 | create_button("Src \n IP", 1, callback_fields_printed, 8, bbox, "Source IP") |
---|
| 1097 | create_button("Dst\n IP", 1, callback_fields_printed, 12, bbox, "Destination IP") |
---|
| 1098 | create_button("Flw\n IP", 0, callback_fields_printed, 15, bbox, "Following IP") |
---|
| 1099 | create_button("Index\n In", 0, callback_fields_printed, 10, bbox, "SNMP Index Input") |
---|
| 1100 | create_button("Index\n Out", 0, callback_fields_printed, 14, bbox, "SNMP Index Output") |
---|
| 1101 | create_button("Size (P)", 1, callback_fields_printed, 2, bbox, "Packets number") |
---|
| 1102 | create_button("Size (B)", 1, callback_fields_printed, 1, bbox, "Size in Bytes") |
---|
| 1103 | create_button("Time", 0, callback_fields_printed, 0, bbox, "Age of flow") |
---|
| 1104 | |
---|
| 1105 | b2box = gtk.HButtonBox() |
---|
| 1106 | vbox.pack_start(b2box, False, True, 5) |
---|
| 1107 | create_button("Src\n Port", 1, callback_fields_printed, 7, b2box, "TCP/UDP source port") |
---|
| 1108 | create_button("Dst\n Port", 1, callback_fields_printed, 11, b2box, "TCP/UDP destination port") |
---|
| 1109 | create_button("TCP\n Flags", 0, callback_fields_printed, 6, b2box, "TCP Flags") |
---|
| 1110 | create_button("Prot", 1, callback_fields_printed, 4, b2box, "Protocol") |
---|
| 1111 | create_button("Tos", 0, callback_fields_printed, 5, b2box, "Type of service") |
---|
| 1112 | create_button("Src\nAs", 0, callback_fields_printed, 16, b2box, "Source AS") |
---|
| 1113 | create_button("Dst\nAs", 0, callback_fields_printed, 17, b2box, "Destination AS") |
---|
| 1114 | create_button("Src\nMask", 0, callback_fields_printed, 9, b2box, "Source Mask") |
---|
| 1115 | create_button("Dst\nMask", 0, callback_fields_printed, 13, b2box, "Destination Mask") |
---|
| 1116 | |
---|
| 1117 | b3box = gtk.HButtonBox() |
---|
| 1118 | vbox.pack_start(b3box, False, True, 5) |
---|
| 1119 | create_button("Router", 0, callback_fields_printed, 69, b3box, "Router address") |
---|
| 1120 | |
---|
| 1121 | |
---|
| 1122 | frame = gtk.Frame("Parsing values") |
---|
| 1123 | ipv4_vbox.pack_start(frame, False, False, 5) |
---|
| 1124 | hbox = gtk.VBox(False, 0) |
---|
| 1125 | hbox.set_border_width(5) |
---|
| 1126 | frame.add(hbox) |
---|
| 1127 | pbbox = gtk.HButtonBox() |
---|
| 1128 | hbox.pack_start(pbbox, False, True, 5) |
---|
| 1129 | |
---|
| 1130 | create_entry(18, "Required Format : \n an IPv4 address like x.x.x.x \n or a network address like x.x.x.x/x", pbbox, "IPv4 address :", 1) |
---|
| 1131 | create_entry(39, "Required Format : \n an IPv6 address like x:x:x:x:x::x \n or a network address like x:x:x/x NOT YET SUPPORTED", pbbox, "(IPv6 address :)", 2) |
---|
| 1132 | create_entry(18, "Required Format : \n an IP address like x.x.x.x \n or a network address like x.x.x.x/x", pbbox, "Flw IP ad. :", 3) |
---|
| 1133 | dbbox = gtk.HButtonBox() |
---|
| 1134 | hbox.pack_start(dbbox, False, True, 5) |
---|
| 1135 | create_entry(5, "Required Format : a number less than 65535", dbbox, "SNMP Index In|Out :", 4) |
---|
| 1136 | create_entry(4, "Required Format : a number less than 9999", dbbox, "Packet NB :", 7) |
---|
| 1137 | create_entry(17, "Required Format : a range, each number less than 999999999 and space as separator: \"48 54\"", dbbox, "Bytes :", 6) |
---|
| 1138 | tbbox = gtk.HButtonBox() |
---|
| 1139 | hbox.pack_start(tbbox, False, True, 5) |
---|
| 1140 | create_entry(2, "Required Format : a number less than 99", tbbox, "Time :", 8) |
---|
| 1141 | create_entry(11, "Required Format : a range, each number less than 65535 and space as separator: \"6881 6889\"", tbbox, "Port :", 9) |
---|
| 1142 | |
---|
| 1143 | create_entry(3, "Required Format : a number less than 255", tbbox, "TCP Flag :", 11) |
---|
| 1144 | fbbox = gtk.HButtonBox() |
---|
| 1145 | hbox.pack_start(fbbox, False, True, 5) |
---|
| 1146 | create_entry(5, "Required Format : a number less than 65535", fbbox, "Protocol :", 12) |
---|
| 1147 | create_entry(3, "Required Format : a number less than 255", fbbox, "ToS :", 13) |
---|
| 1148 | create_entry(5, "Required Format : a number less than 65535", fbbox, "Src AS :", 14) |
---|
| 1149 | create_entry(5, "Required Format : a number less than 65535", fbbox, "Dst AS :", 15) |
---|
| 1150 | kbbox = gtk.HButtonBox() |
---|
| 1151 | hbox.pack_start(kbbox, False, True, 5) |
---|
| 1152 | create_entry(18, "Required Format : \n an IP address like x.x.x.x", kbbox, "The flows from this Router :", 18) |
---|
| 1153 | |
---|
| 1154 | |
---|
| 1155 | vbox.show() |
---|
| 1156 | main_vbox.show() |
---|
| 1157 | window.show_all() |
---|
| 1158 | mainTT.enable() |
---|
| 1159 | |
---|
| 1160 | |
---|
| 1161 | gtk.threads_enter() |
---|
| 1162 | gtk.main() |
---|
| 1163 | gtk.threads_leave() |
---|