forked from tumi8/Protocol-Informatics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
123 lines (102 loc) · 4.01 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python -u
#
# Protocol Informatics Prototype
# Written by Marshall Beddoe <[email protected]>
# Extended by Lothar Braun <[email protected]>
# Copyright (c) 2004 Baseline Research
# (c) 2011 Lothar Braun
#
# Licensed under the LGPL
#
import PI
import discoverer
import entropy
import common
import sys, getopt, yaml, os, log4py, log4py.config, logging
def main():
configFile = None
log4py.config.fileConfig("log4py.properties")
logging.debug("ProtoX starting up")
#
# Parse command line options and do sanity checking on arguments
#
try:
(opts, args) = getopt.getopt(sys.argv[1:], "c:")
except:
usage()
for o,a in opts:
if o in ["-c"]:
configFile = a
else:
usage()
if not configFile:
# if we are started without a config file, we check for an existing file
# named config.yml in the current directory and try load it if available
if os.access("config.yml", os.R_OK):
print "Found default configuration file \"config.yml\". Trying to load the file ..."
conf = common.config.loadConfig("config.yml")
command_line_interface(conf)
else:
# ok, we didn't find a default config. We are now trying to create one
# from the default configuration
print "No default configuration found. Createing a default config file \"config.yml\"."
command_line_interface(None, True)
# this function should never return, but we make sure by exiting here
return
conf = common.config.loadConfig(configFile)
if conf.interactive:
command_line_interface(conf)
return
print "ERROR: The non-interactive mode is broken right now. Please use the command line interface ..."
sys.exit(-1)
if conf.inputFile != None:
file = conf.inputFile
else:
print "FATAL: You specified non-interactive mode, but didn't specify an inputFile. This is illegal ..."
sys.exit(-1)
#
# Open file and get sequences
#
try:
if conf.format == "pcap":
sequences = common.input.Pcap(file, conf.maxMessages, conf.onlyUniq, conf.messageDelimiter, conf.fieldDelimiter).getConnections()
elif conf.format == "ascii":
sequences = common.input.ASCII(file, conf.maxMessages, conf.onlyUniq, conf.messageDelimiter, conf.fieldDelimiter).getConnections()
elif conf.format == "bro":
sequences = common.input.Bro(file, conf.maxMessages, conf.onlyUniq, conf.messageDelimiter, conf.fieldDelimiter).getConnections()
else:
print "FATAL: Unknown file format"
sys.exit(-1)
except Exception as inst:
print ("FATAL: Error reading input file '%s':\n %s" % (file, inst))
sys.exit(-1)
if conf.analysis == "entropy":
entropy.entropy.entropy_core(sequences, conf.gnuplotFile)
elif conf.analysis == "PI":
PI.core.pi_core(sequences, conf.weight, conf.graph, conf.textBased)
elif conf.analysis == "reverx":
pass
else:
print "FATAL: Unknown analysis module %s configured" % (conf.analysis)
sys.exit(-1)
def usage():
print "usage: %s [ -c <config> ]" % \
sys.argv[0]
print " -c\tconfig file in yaml format (optional)."
print ""
print "If no config file is specified, the default configuration is read from config.yml in the current directory."
print "If no config.yml is available in the current directory, a new config.yml with default values is created."
sys.exit(-1)
def command_line_interface(config, savedefault = False):
import cmdinterface
cmdline = cmdinterface.cli.CommandLineInterface(config)
if savedefault:
cmdline.config.configFile = "config.yml"
cmdline.do_saveconfig("")
print ""
print "Welcome to Protocol-Informatics. What do you want to do today?"
print ""
cmdline.cmdloop()
if __name__ == "__main__":
main()
# vim: set sts=4 sw=4 cindent nowrap expandtab: