Last active
November 7, 2020 12:15
-
-
Save EONRaider/35484fc55c643ddbdac7ae55f919419d to your computer and use it in GitHub Desktop.
Translate TCP flags from HEX, INT or OCTAL values to text in Python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# https://gist.github.com/EONRaider/35484fc55c643ddbdac7ae55f919419d | |
__author__ = 'EONRaider, keybase.io/eonraider' | |
def translate_tcp_flags(flag_code: str, *, base: int): | |
flag_names = 'NS', 'CWR', 'ECE', 'URG', 'ACK', 'PSH', 'RST', 'SYN', 'FIN' | |
flag_bits = format(int(flag_code, base=base), '09b') | |
yield from (flag_name for flag_name, flag_bit in zip(flag_names, flag_bits) | |
if flag_bit == '1') | |
if __name__ == '__main__': | |
assert list(translate_tcp_flags('2', base=16)) == ['SYN'] | |
assert list(translate_tcp_flags('10', base=16)) == ['ACK'] | |
assert list(translate_tcp_flags('10', base=10)) == ['PSH', 'SYN'] | |
assert list(translate_tcp_flags('18', base=16)) == ['ACK', 'PSH'] | |
assert list(translate_tcp_flags('18', base=10)) == ['ACK', 'SYN'] | |
assert list(translate_tcp_flags('24', base=16)) == ['URG', 'RST'] | |
assert list(translate_tcp_flags('24', base=8)) == ['ACK', 'RST'] | |
assert list(translate_tcp_flags('17', base=8)) == ['PSH', 'RST', 'SYN', 'FIN'] | |
assert list(translate_tcp_flags('17', base=16)) == ['ACK', 'RST', 'SYN','FIN'] | |
assert list(translate_tcp_flags('3b', base=16)) == ['URG', 'ACK', 'PSH', 'SYN', 'FIN'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment