Skip to content

Instantly share code, notes, and snippets.

@gbossert
Created March 3, 2017 16:33
Show Gist options
  • Save gbossert/74ed94ba6f606446e3c4cf9ca8c41711 to your computer and use it in GitHub Desktop.
Save gbossert/74ed94ba6f606446e3c4cf9ca8c41711 to your computer and use it in GitHub Desktop.
import operator
from netzob.all import *
data = ["hello bernard !", "hello albert !", "hello netzob !"]
messages = [RawMessage(d) for d in data]
f0 = Field(ASCII("hello "))
f1 = Field(ASCII(nbChars=(1,20)))
f2 = Field(ASCII(" !"))
s = Symbol(fields = [f0, f1, f2])
s.messages = messages
print(s)
# Field | Field | Field
# -------- | --------- | -----
# 'hello ' | 'bernard' | ' !'
# 'hello ' | 'albert' | ' !'
# 'hello ' | 'netzob' | ' !'
# -------- | --------- | -----
sorted_x = sorted(f1.getMessageValues().items(), key=operator.itemgetter(1))
for message, field_value in sorted_x:
print(field_value, " | ", message.data)
# b'albert' | hello albert !
# b'bernard' | hello bernard !
# b'netzob' | hello netzob !
@pandasauce
Copy link

For nicer output on binary protocols:

for message, field_value in sorted_x:
    print(TypeConverter.convert(field_value, Raw, HexaString), " | ", TypeConverter.convert(message.data, Raw, HexaString))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment