Skip to content

Instantly share code, notes, and snippets.

@samirahmed
Created October 13, 2012 21:12
Show Gist options
  • Save samirahmed/3886158 to your computer and use it in GitHub Desktop.
Save samirahmed/3886158 to your computer and use it in GitHub Desktop.
Response Difference In With Different Flags
import sys
import os
import subprocess
def execute( command):
result = ""
try:
result = os.popen( command ).read()
except Exception:
result = "FAILED: "+command
finally:
return result
def expected( key):
if key == 'seq' or key == 'ack' or key == 'sum':
return True
if key == 'ip' or key=='rtt' :
return True
return False
def compare( aa, bb ):
diff = []
aa_keys = aa.keys()
same = True
for key in bb.keys():
if not(key in aa_keys):
return (False , [])
if bb[key] != aa[key]:
if not expected(key):
diff.append( (key,aa[key],bb[key]) )
same = False;
return (same,diff)
def isData( line):
if line.startswith('len=') or line.startswith('sport=') or line.startswith('seq='):
return True
else:
return False
def parse( output ,command):
result = {}
try:
lines= output.split('\n')
response = filter( isData ,lines)
response = ' '.join(response)
print 'response' , response
print len(response)
if len(response) > 1 :
data_pairs = response.split(' ')
print data_pairs
for data in data_pairs:
key_value = data.split('=')
if len(key_value) == 2:
result[ key_value[0] ] = key_value[1]
except Exception:
print "UNPARSABLE ",command," \n ", result
finally:
return result
def print_diff( diff ,flags , ii , filehandle ):
if diff[0]:
line = str(ii) + " SAME: " +str(flags) + '\n'
#filehandle.write(line)
#print line
else:
line = str(ii) + " DIFF: " +str(flags) + str(diff[1]) + '\n'
print line
filehandle.write(line)
filehandle.flush()
ip2 = ""
ip2 = ""
port_1 = ""
port_2 = ""
if len(sys.argv) < 4:
print "Sorry Need and IP as argument"
sys.exit()
else:
ip1 = sys.argv[1]
ip2 = sys.argv[2]
port_1 = sys.argv[3]
port_2 = sys.argv[4]
print 'IP 1 : ', ip1
print 'IP 2 : ', ip2
print 'Port : ', port_1
print 'Port : ', port_2
filename = 'port_'+port_1+'_'+port_2+'_scan.txt'
fileh = open(filename,'w')
hping_command_1 = "hping3 -i eth2 -c 1 -V "+ip1+" "
hping_command_2 = "hping3 -i eth2 -c 1 -V "+ip2+" "
flags = ['-S', '-R' ,'-P','-U','-A','-X','-Y', '-F']
flag_max = 2**(len(flags))-1
possible_flags_1 = [hping_command_1]* ( 2** len(flags) )
possible_flags_2 = [hping_command_2]* ( 2** len(flags) )
for ii in range(2**( len(flags) )):
for jj in range(len(flags)):
if ii & 2**(jj):
possible_flags_1[ii] += " " + flags[jj]
possible_flags_2[ii] += " " + flags[jj]
# print possible_flags[ii]
difference = []
for ii in range(len(possible_flags_1)):
command_1 = possible_flags_1[ii] + " -p "+ str(port_1)
command_2 = possible_flags_2[ii] + " -p "+ str(port_2)
result_1 = execute(command_1)
result_2 = execute(command_2)
kv_1 =parse(result_1,command_1)
kv_2 =parse(result_2,command_2)
difference.append( compare( kv_1, kv_2) )
print command_1, ':', str(difference[ii])
print_diff( difference[ii], command_1 , ii , fileh )
fileh.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment