forked from MITRECND/chopshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchopshop
executable file
·274 lines (223 loc) · 9.8 KB
/
chopshop
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
# Copyright (c) 2014 The MITRE Corporation. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#Chopshop
"""
TODO
Need to add multi-threading support -- requires locking of smallest_discard object
"""
VERSION = "4.1"
import sys
import signal
import os
import nids
import fileinput
import imp
from optparse import OptionParser, Option, OptionValueError
from threading import Thread
from threading import Lock
#Python Version Check
version = sys.version_info
version = float(str(version[0]) + "." + str(version[1]))
if version < 2.6:
print "Python Minimum Version 2.6 required"
sys.exit(-1)
from multiprocessing import Process, Queue as mQueue
import Queue
import time
#Chopshop Working Directory -- defaults to where script exists
CHOPSHOP_WD = os.path.realpath(os.path.dirname(sys.argv[0]))
sys.path.append(CHOPSHOP_WD + '/shop')
##### DEBUG CODE #####
### This is meant to be used for the sole purpose of chopshop core development
### DO NOT ENABLE THIS UNLESS YOU ARE WORKING ON THE CORE OR SHOP COMPONENTS
import ChopShopDebug as CSD
#CSD.enable_debug()
#####
from ChopConfig import ChopConfig
from ChopLib import ChopLib
from ChopUi import ChopUi, ChopStdout
from ChopException import ChopConfigException
global choplib
global chopui
def signal_handler(signal, frame):
try:
CSD.debug_out("Signal Caught\n")
choplib.stop()
chopui.stop()
chopui.join()
choplib.finish()
choplib.join()
except:
pass
sys.exit(0)
# send signal to finish processing data so far and exit
def abrt_signal_handler(signal, frame):
choplib.abort()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGABRT, abrt_signal_handler)
class CustomOption(Option):
ACTIONS = Option.ACTIONS + ("store_list",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("store_list",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("store_list",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("store_list",)
def take_action(self, action, dest, opt, value, values, parser):
if action == "store_list":
lvalue = value.split(",")
values.ensure_value(dest, []).extend(lvalue)
else:
Option.take_action(
self, action, dest, opt, value, values, parser)
def main():
global choplib
global chopui
choplib = ChopLib()
chopui = ChopUi()
chopconfig = ChopConfig()
user_config = os.path.expanduser("~") + "/.chopshop.cfg"
if os.path.isfile(user_config):
chopconfig.parse_config(user_config)
optparser = OptionParser(
usage='usage: %prog [options] ["bpf filter"] "list | (of, many) | modules ; and | more"',
option_class=CustomOption,
description='ChopShop is a MITRE created utility to aid analysts in decoding network traffic'
)
optparser.add_option("-B", "--base_dir", dest = "base_dir", default=None, action="store_list",
help = "Base directory to load modules and external libraries from. Option prioritized over -M and -E")
optparser.add_option("-c", "--configfile", action="store", dest="configfile",
type="string", default=None, help="Import a config file")
optparser.add_option("-C", "--saveconfig", action="store", dest="saveconfig",
type="string", default=None, help="Save current arguments to a config file")
optparser.add_option("-E", "--ext_dir", dest = "ext_dir", action="store_list", type="string",
default=None, help = "Directory to load external libraries from")
optparser.add_option("-f", "--file", action="store", dest="filename",
type="string", help="input pcap file")
optparser.add_option("-F", "--fileout", action="store", dest="fileout",
type="string", default=None, help="Enable File Output")
optparser.add_option("-g", "--gui", action="store_true", dest="gui",
default=False, help="Enable ChopShop Gui")
optparser.add_option("-G", "--GMT", action="store_true", dest="GMT",
default=False, help="timestamps in GMT (tsprnt and tsprettyprnt only)")
optparser.add_option("-i", "--interface", action="store", dest="interface",
type="string", help="interface to listen on")
optparser.add_option("-J", "--jsonout", action="store", dest="jsonout",
type="string", default=None, help="Enable JSON Output")
optparser.add_option("-l", "--aslist", action = "store_true", dest = "aslist",
default=False, help="Treat FILENAME as a file containing a list of files")
optparser.add_option("-L", "--long", action="store_true", dest="longrun",
default=False, help="Read from FILENAME forever even if there's no more pcap data")
optparser.add_option("-m", "--module_info", action="store_true", dest="modinfo",
default=False,help="print information about module(s) and exit")
optparser.add_option("-M", "--mod_dir", dest = "mod_dir", action="store_list", type="string",
default=None, help = "Directory to load modules from")
optparser.add_option("-s", "--savedir", action="store", dest="savedir",
type="string", default=None, help="Location to save carved files")
optparser.add_option("-S", "--stdout", action="store_true", dest="stdout",
default=False, help="Explicitly enable output to stdout")
optparser.add_option("-t", "--module_tree", action="store_true", dest="modtree",
default=False,help="print information about module tree and exit")
optparser.add_option("-v", "--version", action="store_true", dest="version",
default=False,help="print version and exit")
(options, args) = optparser.parse_args()
if options.version:
print "ChopShop Version %s (Choplib: %s)" % (VERSION, choplib.version())
sys.exit()
# Parse options and arguments.
try:
chopconfig.parse_opts(options, args)
except ChopConfigException, e:
print e
sys.exit(1)
choplib.base_dir = chopconfig.base_dir
choplib.mod_dir = chopconfig.mod_dir
choplib.ext_dir = chopconfig.ext_dir
choplib.aslist = chopconfig.aslist
choplib.longrun = chopconfig.longrun
choplib.modinfo = chopconfig.modinfo
choplib.modtree = chopconfig.modtree
choplib.GMT = chopconfig.GMT
choplib.bpf = chopconfig.bpf
choplib.modules = chopconfig.modules
if chopconfig.gui:
#Forecefully disable stdout
chopconfig.stdout = False
chopui.gui = True
if chopconfig.fileout:
chopui.fileout = True
chopui.filedir = chopconfig.fileout
if chopconfig.jsonout:
chopui.jsonout = True
chopui.jsondir = chopconfig.jsonout
choplib.jsonout = True
#If any of the other outputs are not chosen, enable stdout
if (not chopconfig.gui and
not chopconfig.fileout and
not chopconfig.jsonout):
chopconfig.stdout = True
if chopconfig.stdout or chopconfig.gui or chopconfig.fileout:
choplib.text = True
if chopconfig.stdout:
#ChopStdout.prepend_module_name = True
chopui.stdout = True
if chopconfig.savedir:
chopui.savefiles = True
chopui.savedir = chopconfig.savedir
choplib.savefiles = True
if options.saveconfig:
print "Saving config to %s" % options.saveconfig
chopconfig.save_config(options.saveconfig)
sys.exit()
if not chopconfig.modinfo and not chopconfig.modtree:
if not chopconfig.interface:
if not chopconfig.filename:
#Nothing is set for input, attempt to read a list of files from stdin
try:
files = sys.stdin.readlines()
except Exception, e:
sys.exit("Error getting files from stdin %s\n" % str(e))
try:
f = open('/dev/tty')
os.dup2(f.fileno(), 0)
except:
print "Unable to reclaim tty"
sys.exit(-1)
choplib.filelist = files
else:
if not os.path.exists(chopconfig.filename):
print "Unable to find file '%s'" % chopconfig.filename
sys.exit(-1)
choplib.filename = chopconfig.filename
else:
choplib.interface = chopconfig.interface
chopui.bind(choplib)
chopui.start()
choplib.start()
#choplib.setup_local_chop(pid = 0)
#choplib.chop.prettyprnt("GREEN", "ChopShop Ui/Lib started")
while chopui.is_alive():
time.sleep(.1)
chopui.join()
choplib.finish()
choplib.join()
if __name__ == '__main__':
main()