Skip to content

Instantly share code, notes, and snippets.

@jonorsky
Created May 17, 2018 03:32
Show Gist options
  • Save jonorsky/f1096360798d2673f995d5df725de023 to your computer and use it in GitHub Desktop.
Save jonorsky/f1096360798d2673f995d5df725de023 to your computer and use it in GitHub Desktop.
import socket
import binascii
if __name__ == "__main__":
#str_ = b"\x1c\x65\x9d\xbd\x82\xa0\xbc\x64\x4b\x74\x96\x71\x08\x00\x45\x00\x01\x1a\x0d\xb7\x00\x00\x36\x06\x15\x2c\xd8\x3a\xc8\x13\xc0\xa8\x00\x05\x00\x50\xdb\xe6\x10\x6d\x66\x40\x84\xc5\x42\xf5\x80\x18\x00\xab\xe9\x8a\x00\x00\x01\x01\x08\x0a\xaa\x7e\xe3\x26\x5f\x4e\x7b\xb9\x48\x54\x54\x50\x2f\x31\x2e\x31\x20\x33\x30\x34\x20\x4e\x6f\x74\x20\x4d\x6f\x64\x69\x66\x69\x65\x64\x0d\x0a\x45\x78\x70\x69\x72\x65\x73\x3a\x20\x57\x65\x64\x2c\x20\x31\x36\x20\x4d\x61\x79\x20\x32\x30\x31\x38\x20\x31\x36\x3a\x33\x31\x3a\x32\x34\x20\x47\x4d\x54\x0d\x0a\x44\x61\x74\x65\x3a\x20\x57\x65\x64\x2c\x20\x31\x36\x20\x4d\x61\x79\x20\x32\x30\x31\x38\x20\x31\x36\x3a\x33\x31\x3a\x32\x34\x20\x47\x4d\x54\x0d\x0a\x43\x61\x63\x68\x65\x2d\x43\x6f\x6e\x74\x72\x6f\x6c\x3a\x20\x70\x72\x69\x76\x61\x74\x65\x2c\x20\x6d\x61\x78\x2d\x61\x67\x65\x3d\x30\x0d\x0a\x45\x54\x61\x67\x3a\x20\x57\x2f\x22\x39\x66\x38\x33\x65\x31\x65\x61\x66\x35\x31\x39\x38\x34\x32\x63\x39\x36\x32\x38\x35\x33\x63\x36\x35\x61\x64\x31\x64\x64\x65\x37\x61\x36\x65\x66\x39\x66\x30\x36\x31\x63\x62\x33\x64\x62\x65\x61\x35\x36\x30\x62\x32\x35\x66\x39\x31\x63\x66\x31\x64\x65\x37\x37\x22\x0d\x0a\x53\x65\x72\x76\x65\x72\x3a\x20\x47\x53\x45\x0d\x0a\x0d\x0a"
#str_ = b"\xbc\x64\x4b\x74\x96\x71\x1c\x65\x9d\xbd\x82\xa0\x08\x00\x45\x00\x01\xc2\x97\x43\x40\x00\x40\x06\xb7\xe4\xc0\xa8\x00\x05\xb9\xc7\x6f\x99\xe2\x6e\x00\x50\x32\xab\x85\x7b\x10\x61\x22\xc6\x80\x18\x00\xe5\x58\xa4\x00\x00\x01\x01\x08\x0a\x91\x29\xd1\xbc\x08\xe7\x7d\xe3\x47\x45\x54\x20\x2f\x63\x61\x6d\x65\x72\x61\x2d\x73\x6c\x69\x64\x65\x73\x68\x6f\x77\x2f\x63\x73\x73\x2f\x63\x61\x6d\x65\x72\x61\x2e\x63\x73\x73\x20\x48\x54\x54\x50\x2f\x31\x2e\x31\x0d\x0a\x48\x6f\x73\x74\x3a\x20\x70\x72\x6f\x6a\x65\x63\x74\x2e\x64\x69\x6d\x70\x6f\x73\x74\x2e\x63\x6f\x6d\x0d\x0a\x43\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e\x3a\x20\x6b\x65\x65\x70\x2d\x61\x6c\x69\x76\x65\x0d\x0a\x55\x73\x65\x72\x2d\x41\x67\x65\x6e\x74\x3a\x20\x4d\x6f\x7a\x69\x6c\x6c\x61\x2f\x35\x2e\x30\x20\x28\x58\x31\x31\x3b\x20\x4c\x69\x6e\x75\x78\x20\x78\x38\x36\x5f\x36\x34\x29\x20\x41\x70\x70\x6c\x65\x57\x65\x62\x4b\x69\x74\x2f\x35\x33\x37\x2e\x33\x36\x20\x28\x4b\x48\x54\x4d\x4c\x2c\x20\x6c\x69\x6b\x65\x20\x47\x65\x63\x6b\x6f\x29\x20\x43\x68\x72\x6f\x6d\x65\x2f\x36\x36\x2e\x30\x2e\x33\x33\x35\x39\x2e\x31\x31\x37\x20\x53\x61\x66\x61\x72\x69\x2f\x35\x33\x37\x2e\x33\x36\x0d\x0a\x41\x63\x63\x65\x70\x74\x3a\x20\x74\x65\x78\x74\x2f\x63\x73\x73\x2c\x2a\x2f\x2a\x3b\x71\x3d\x30\x2e\x31\x0d\x0a\x52\x65\x66\x65\x72\x65\x72\x3a\x20\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x66\x72\x6f\x73\x74\x66\x6c\x75\x78\x2e\x63\x6f\x6d\x2f\x0d\x0a\x41\x63\x63\x65\x70\x74\x2d\x45\x6e\x63\x6f\x64\x69\x6e\x67\x3a\x20\x67\x7a\x69\x70\x2c\x20\x64\x65\x66\x6c\x61\x74\x65\x0d\x0a\x41\x63\x63\x65\x70\x74\x2d\x4c\x61\x6e\x67\x75\x61\x67\x65\x3a\x20\x65\x6e\x2d\x55\x53\x2c\x65\x6e\x3b\x71\x3d\x30\x2e\x39\x0d\x0a\x49\x66\x2d\x4d\x6f\x64\x69\x66\x69\x65\x64\x2d\x53\x69\x6e\x63\x65\x3a\x20\x53\x75\x6e\x2c\x20\x31\x37\x20\x41\x70\x72\x20\x32\x30\x31\x36\x20\x30\x36\x3a\x35\x35\x3a\x31\x35\x20\x47\x4d\x54\x0d\x0a\x0d\x0a"
#str_ = b"\x1c\x65\x9d\xbd\x82\xa0\xbc\x64\x4b\x74\x96\x71\x08\x00\x45\x00\x00\x6a\x6e\x94\x00\x00\x2a\x11\x51\x32\x08\x08\x08\x08\xc0\xa8\x00\x05\x00\x35\xa5\x58\x00\x56\xb0\xfc\xd6\xc3\x81\x80\x00\x01\x00\x01\x00\x00\x00\x01\x14\x77\x77\x77\x2d\x67\x6f\x6f\x67\x6c\x65\x2d\x61\x6e\x61\x6c\x79\x74\x69\x63\x73\x01\x6c\x06\x67\x6f\x6f\x67\x6c\x65\x03\x63\x6f\x6d\x00\x00\x01\x00\x01\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\xcf\x00\x04\xd8\x3a\xcb\x2e\x00\x00\x29\x02\x00\x00\x00\x00\x00\x00\x00"
conn = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))
counter=1
while True:
(str_, addr) = conn.recvfrom(65536)
raw_hex = binascii.hexlify(str_)
temp_str=""
ctr=0
vec=[]
source=""
destination=""
temp_src=""
word=""
port_source=0
port_destination=0
get_key=0
for i in raw_hex:
temp_str += chr(i)
ctr += 1;
if ctr==2:
vec.append(temp_str)
get_key+=1
if get_key==24:
x_key=int(temp_str,16)
elif get_key>=27 and get_key <=30:
source += str(int(temp_str,16))
if get_key!=30:
source +='.'
elif get_key>=31 and get_key <=34:
destination += str(int(temp_str,16))
if get_key!=34:
destination +='.'
elif get_key>=35 and get_key <=36:
temp_src += str(temp_str)
if get_key==36:
port_source=int(temp_src,16)
temp_src=""
elif get_key>=37 and get_key <=38:
temp_src += str(temp_str)
if get_key==38:
port_destination=int(temp_src,16)
else:
ps = binascii.unhexlify(str(temp_str))
#print(ps)
word += str(ps.decode('ascii', 'ignore'))
temp_str=""
ctr=0
it=0
des = src = typ = ""
for i in vec:
if it<6:
des += i
if it!=5:
des +=':'
elif it>=6 and it<=11:
src += i
if it!=11:
src += ':'
elif it>=12 and it<=13:
typ += i
if it!=13:
typ += ':'
it += 1
#Checking if HTTP found in the Data, if yes then process the packet
if "HTTP" in word[54:]:
x_ver=""
if typ=="08:00":
x_ver = 4
print("Ethernet Frame:")
#print("Frame {}: ".format(counter),end='')
print("Destination:",des,end='')
print(", Source: ",src,end='')
print(", Protocol: ",x_key,end=' ')
print()
print("IP{} Packet".format(x_ver))
print("Version: {}, ".format(x_ver),end=' ')
if x_key==2:
print("IP Packet: Ver: {}, Proto: {}, Src: {}, Dst: {}".format(x_ver,x_key,source,destination))
else:
print("IP Packet: Ver: {}, ".format(x_ver))
print("Protocol: {}, Source: {}, Target: {}".format(x_key,source,destination))
print("TCP Segment: ")
print("Source Port: {}, Destination Port: {}".format(port_source,port_destination))
print()
print()
print("Data:")
print(word[word.find('HTTP'):])
#print(word)
print()
counter += 1
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment