Skip to content

Commit

Permalink
Add rough parsing of IP packets
Browse files Browse the repository at this point in the history
  • Loading branch information
HbHbNr committed Aug 29, 2020
1 parent 49f6e9c commit d4b604d
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions layer4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# GNU General Public License Version 3

def parseIPpackage(s, i):
version = s[i + 0] >> 4
headerlength = (s[i + 0] & 0xf) * 4
totallength = int.from_bytes([s[i + 2], s[i + 3]], 'big')
identification = int.from_bytes([s[i + 4], s[i + 5]], 'big')
protocol = s[i + 9]
sourceip = int.from_bytes([s[i + 12], s[i + 13], s[i + 14], s[i + 15]], 'big')
destinationip = int.from_bytes([s[i + 16], s[i + 17], s[i + 18], s[i + 19]], 'big')
print(str(version) + ' ' + str(headerlength) + ' ' + str(totallength) + ' ' + str(identification) + ' ' + str(protocol) + ' ' + hex(sourceip) + ' ' + hex(destinationip))
return (totallength, s[i + headerlength + 8:i + totallength - headerlength + 1])

def parseIPpackages(stream):
stream = bytes(stream)
allcontent = bytearray()
num = 1
i = 0
while i < len(stream):
print(str(num) + '.: ' + str(i))
(length, content) = parseIPpackage(stream, i)
i += length
allcontent.extend(content)
num += 1
return allcontent


if __name__ == '__main__':
import ascii85

payload = ascii85.loadpayload('layers/layer4.txt')
decoded = ascii85.decode(payload)
decoded = parseIPpackages(decoded)
print(decoded)

0 comments on commit d4b604d

Please sign in to comment.