Skip to content

Commit

Permalink
feat(cluster): add two methods to cluster by source and by destination
Browse files Browse the repository at this point in the history
  • Loading branch information
gbossert committed Oct 6, 2017
1 parent 0e168d4 commit 2ce2ff4
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions netzob/src/netzob/Inference/Vocabulary/Format.py
Expand Up @@ -369,6 +369,85 @@ def clusterByAlignment(messages, minEquivalence=50, internalSlick=True):
minEquivalence=minEquivalence, internalSlick=internalSlick)
return clustering.cluster(messages)

@staticmethod
@typeCheck(list)
def clusterBySource(messages):
"""Regroup messages sent by the same source
>>> import operator
>>> from netzob.all import *
>>> messages = [RawMessage("hello berlin", source="user"), RawMessage("hello paris", source="master")]
>>> messages.extend([RawMessage("hello madrid", source="master"), RawMessage("hello world", source="user")])
>>> symbols = Format.clusterBySource(messages)
>>> print(len(symbols))
2
>>> for symbol in sorted(symbols, key=operator.attrgetter("name")):
... print("{}:".format(symbol.name))
... print(symbol)
Symbol-master:
Field
--------------
'hello paris'
'hello madrid'
--------------
Symbol-user:
Field
--------------
'hello berlin'
'hello world'
--------------
"""

clusters = dict()
for message in messages:
if message.source in clusters.keys():
clusters[message.source].messages.append(message)
else:
clusters[message.source] = Symbol(name="Symbol-{}".format(message.source), messages = [message])

return list(clusters.values())

@staticmethod
@typeCheck(list)
def clusterByDestination(messages):
"""Regroup messages sent to the same destination
>>> import operator
>>> from netzob.all import *
>>> messages = [RawMessage("hello berlin", destination="user"), RawMessage("hello paris", destination="master")]
>>> messages.extend([RawMessage("hello madrid", destination="master"), RawMessage("hello world", destination="user")])
>>> symbols = Format.clusterByDestination(messages)
>>> print(len(symbols))
2
>>> for symbol in sorted(symbols, key=operator.attrgetter("name")):
... print("{}:".format(symbol.name))
... print(symbol)
Symbol-master:
Field
--------------
'hello paris'
'hello madrid'
--------------
Symbol-user:
Field
--------------
'hello berlin'
'hello world'
--------------
"""

clusters = dict()
for message in messages:
if message.destination in clusters.keys():
clusters[message.destination].messages.append(message)
else:
clusters[message.destination] = Symbol(name="Symbol-{}".format(message.destination), messages = [message])

return list(clusters.values())


@staticmethod
@typeCheck(list)
def clusterByApplicativeData(messages):
Expand Down

0 comments on commit 2ce2ff4

Please sign in to comment.