Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI option to list available modules in mod_dir #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions chopshop
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def main():
type="string", help="interface to listen on")
optparser.add_option("-m", "--module_info", action="store_true", dest="modinfo",
default=False,help="print information about module(s) and exit")
optparser.add_option("-a", "--all_module_list", action="store_true", dest="modlist",
default=False,help="print names of available module(s) and exit")
optparser.add_option("-G", "--GMT", action="store_true", dest="GMT",
default=False, help="timestamps in GMT (tsprnt and tsprettyprnt only)")
optparser.add_option("-v", "--version", action="store_true", dest="version",
Expand All @@ -137,11 +139,11 @@ def main():
print "ChopShop Version %s (Choplib: %s)" % (VERSION, choplib.version())
sys.exit()

if len(args) <= 0:
if len(args) <= 0 and not options.modlist:
print "Module List Required"
sys.exit(0)

if not options.modinfo:
if not options.modinfo and not options.modlist:
if not options.interface:
if not options.filename:
#Nothing is set for input, attempt to read a list of files from stdin
Expand Down Expand Up @@ -173,12 +175,13 @@ def main():
choplib.aslist = options.aslist
choplib.longrun = options.longrun
choplib.modinfo = options.modinfo
choplib.modlist = options.modlist
choplib.GMT = options.GMT

if len(args) > 1:
choplib.bpf = args[0]
choplib.modules = args[1]
else:
elif len(args) == 1:
choplib.modules = args[0]

if options.gui:
Expand Down
2 changes: 2 additions & 0 deletions docs/chopshop_docs/chopshop_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ options:
-i INTERFACE, --interface=INTERFACE
interface to listen on
-m, --module_info print information about module(s) and exit
-a, --all_module_list
print list of modules available and exit.
-G, --GMT timestamps in GMT (tsprnt and tsprettyprnt only)
-v, --version print version and exit
-g, --gui Enable ChopShop Gui
Expand Down
93 changes: 71 additions & 22 deletions shop/ChopLib.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self):
'longrun': False,
'interface': '',
'modinfo': False,
'modlist': False,
'GMT': False,
'savefiles': False, #Should ChopShop handle the saving of files?
'text': False,
Expand Down Expand Up @@ -183,6 +184,15 @@ def modinfo(self):
def modinfo(self, v):
self.options['modinfo'] = v

@property
def modlist(self):
"""print information about available module(s) and exit."""
return self.options['modlist']

@modlist.setter
def modlist(self, v):
self.options['modlist'] = v

@property
def GMT(self):
"""timestamps in GMT (tsprnt and tsprettyprnt only)."""
Expand Down Expand Up @@ -301,7 +311,7 @@ def send_finished_msg(self, data = {}, stop_seq = False):
def run(self):
surgeon = None

if not self.options['modinfo']: #No point in doing surgery if it's modinfo
if not self.options['modinfo'] and not self.options['modlist']: #No point in doing surgery if it's modinfo or modlist
# Figure out where we're reading packets from
if not self.options['interface']:
if not self.options['filename']:
Expand Down Expand Up @@ -371,6 +381,23 @@ def run(self):
self.nidsp.join()
return

if self.options['modlist']:
self.kill_lock.acquire();
try:
self.tonids.put(['mod_list'])
resp = self.fromnids.get() #really just to make sure the functions finish
except Exception, e:
raise ChopLibException(e)
finally:
self.kill_lock.release()

#Process 2 will quit after doing its job

#Inform caller that the process is done
self.send_finished_msg()
#Surgeon should not be invoked so only need
#to cleanup nidsp
self.nidsp.join()
else:
self.kill_lock.acquire()
try:
Expand Down Expand Up @@ -519,27 +546,28 @@ def __nids_core_runner_(self, inq, outq, dataq, autostart = True):
#Setup the modules
args = options['modules']
mods = args.split(';')
try:
for mod in mods:
mod = mod.strip()
sindex = mod.find(' ')
if sindex != -1:
modl = []
modl.append(self.__loadModules_(mod[0:sindex],mod_dir))
modl.append(mod[sindex + 1:])
modl.append(mod[0:sindex])
module_list.append(modl)
else:
modl = []
modl.append(self.__loadModules_(mod,mod_dir))
modl.append("")
modl.append(mod)
module_list.append(modl)
except Exception, e:
outq.put(e)
sys.exit(-1)
if not args == '':
try:
for mod in mods:
mod = mod.strip()
sindex = mod.find(' ')
if sindex != -1:
modl = []
modl.append(self.__loadModules_(mod[0:sindex],mod_dir))
modl.append(mod[sindex + 1:])
modl.append(mod[0:sindex])
module_list.append(modl)
else:
modl = []
modl.append(self.__loadModules_(mod,mod_dir))
modl.append("")
modl.append(mod)
module_list.append(modl)
except Exception, e:
outq.put(e)
sys.exit(-1)

if len(module_list) == 0:
if len(module_list) == 0 and not options['modlist']:
outq.put('Zero Length Module List')
sys.exit(-1)

Expand Down Expand Up @@ -584,7 +612,28 @@ def __nids_core_runner_(self, inq, outq, dataq, autostart = True):

outq.put('fini')
sys.exit(0)

elif data[0] == 'mod_list':
all_mods = []
chop.prnt("Searching for modules in '%s'" % mod_dir)
for dirname, dirnames, filenames in os.walk(mod_dir):
for filename in filenames:
try:
nxt_mod_name = os.path.splitext(filename)[0]
mod_found = self.__loadModules_(nxt_mod_name, dirname)
if not nxt_mod_name in all_mods:
all_mods.append(nxt_mod_name);
except Exception, e:
# raise e
pass
break # only search mod_dir - no any subdirs - as modules are not available recursively
if not all_mods:
chop.prnt("No modules found")
else:
chop.prnt("Modules found: ")
chop.prnt(", ".join(all_mods))

outq.put('fini')
sys.exit(0)
elif data[0] == 'cont':
break
elif data[0] == 'stop': #Some error must have occurred
Expand Down