/
ninux.py
106 lines (91 loc) · 4.02 KB
/
ninux.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# @copyright Leonardo Maccari: leonardo.maccari@unitn.it
# released under GPLv3 license
from plugin import plugin
from threading import Thread
from ConfigParser import Error
import logging
import networkx as nx
import chardet
from dbmanager import *
class ninux(plugin):
def __init__(self):
Thread.__init__(self)
def initialize(self, parser, lc):
self.localSession = lc
self.parser = parser
self.pseudonymDumpFile = None
self.pseudonymFile = None
self.ownerPseudonymDict = {}
self.enabled, logLevel, self.pluginName = \
plugin.baseInitialize(self, parser, __file__, lc)
self.logger = logging.getLogger(self.pluginName)
self.logger.setLevel(logLevel)
try:
self.userName = self.parser.get('ninux', 'user')
self.userPasswd = self.parser.get('ninux', 'passwd')
self.dbURL = self.parser.get('ninux', 'url')
self.dbPort = self.parser.get('ninux', 'port')
self.dbName = self.parser.get('ninux', 'db')
except Error as e:
self.logger.error("Could not initalize ninux plugin. \'"+e+"\'")
if self.enabled == True:
sys.exit()
try:
self.period = plugin.convertTime(self, self.parser.get('ninux', 'period'))
except:
self.period = 300
def getStats(self):
""" called by the run() function of plugin class """
if self.enabled == False:
self.logger.info(plugin.disabledMessage)
return
ninuxURL = 'mysql://'+self.userName+":"+self.userPasswd+"@"+\
self.dbURL+":"+self.dbPort+"/"+self.dbName
self.logger.info("Getting data from Ninux network")
engine = create_engine(ninuxURL)
DBSession = sessionmaker(bind=engine)
session = DBSession()
### FIXME this should be ORM-ed, not just raw SQL as it is now
etxQuery = """select snode.id as sid, snode.name as sname,
snode.owner as sowner, snode.email as semail,
dnode.id as did, dnode.name as dname,
dnode.owner as downer, dnode.email as demail,
link.etx as etx_v
from nodeshot_node as snode join nodeshot_device as sdev join
nodeshot_interface as sifc join nodeshot_link as link join
nodeshot_interface as difc join nodeshot_device as ddev join
nodeshot_node as dnode where snode.id = sdev.node_id and sdev.id
= sifc.device_id and sifc.id = link.from_interface_id and
difc.id = link.to_interface_id and difc.device_id = ddev.id and
ddev.node_id = dnode.id"""
q = session.query("sid", "sname", "sowner", "semail", "did",
"dname", "downer", "demail", "etx_v").\
from_statement(etxQuery)
try:
c = len(q.all())
if c==0:
self.logger.error("no results from ninux DB!")
return
except:
#FIXME add error message from the DB
self.logger.error("could not connect to ninux DB!")
return
if self.myCrypto.disabled:
newScan = scan(network=self.pluginName)
else:
newScan = scan(network=self.pluginName, encrypted=True)
self.localSession.add(newScan)
g = nx.Graph()
fromSet = set()
for [sid, sn, sown, semail, did, dn, down, demail, etxValue] in q:
# some strings in ninux DB are encoded in weird
# encodings, guess the encoding and convert
sname = sn.decode(chardet.detect(sn)['encoding'])
dname = dn.decode(chardet.detect(dn)['encoding'])
g.add_node(str(sid), name=sname, owner=sown, email=semail)
g.add_node(str(did), name=dname, owner=down, email=demail)
g.add_edge(str(sid), str(did), weight=etxValue)
fromSet.add('"'+sown+'"' + " " + "<"+semail+">")
fromSet.add('"'+down+'"' + " " + "<"+demail+">")
self.dumpPseudonym(list(fromSet))
addGraphToDB(g, self.localSession, newScan, self.myCrypto)