-
Notifications
You must be signed in to change notification settings - Fork 2
/
wildfire-to-stix.py
executable file
·303 lines (263 loc) · 8 KB
/
wildfire-to-stix.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python
#
# Copyright (c) 2014-2015 Palo Alto Networks, Inc. <info@paloaltonetworks.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
from __future__ import print_function
import sys
import argparse
import logging
from panstix import __version__
import panstix.utils
import panstix.packaging
LOG = logging.getLogger(__name__)
def dump_report_to_stix(options):
panstix.utils.set_id_namespace(
"https://github.com/PaloAltoNetworks-BD/pan-stix",
"pan-stix"
)
sp = panstix.packaging.get_stix_package_from_wfreport(
title=options.title,
short_description=options.title,
hash=options.hash,
tag=options.tag,
report=options.inreport,
sample=options.sample,
pcap=options.pcap
)
if options.outfile is not None:
f = open(options.outfile, 'w')
f.write(sp.to_xml())
f.close()
else:
sys.stdout.write(sp.to_xml())
def dump_report_to_stix_ol(options):
panstix.utils.set_id_namespace(
"https://github.com/PaloAltoNetworks-BD/pan-stix",
"pan-stix"
)
sp = panstix.packaging.get_stix_ol_package_from_wfreport(
title=options.title,
short_description=options.title,
hash=options.hash,
tag=options.tag,
report=options.inreport
)
if options.outfile is not None:
f = open(options.outfile, 'w')
f.write(sp.to_xml())
f.close()
else:
sys.stdout.write(sp.to_xml())
def dump_report_to_stix_il(options):
panstix.utils.set_id_namespace(
"https://github.com/PaloAltoNetworks-BD/pan-stix",
"pan-stix"
)
sp = panstix.packaging.get_stix_il_package_from_wfreport(
title=options.title,
short_description=options.title,
hash=options.hash,
tag=options.tag,
report=options.inreport,
evidence=options.evidence,
decontext=options.decontext
)
if options.outfile is not None:
f = open(options.outfile, 'w')
f.write(sp.to_xml())
f.close()
else:
sys.stdout.write(sp.to_xml())
def dump_report_to_maec(options):
panstix.utils.set_id_namespace(
"https://github.com/PaloAltoNetworks-BD/pan-stix",
"pan-stix"
)
mp = panstix.packaging.get_maec_package_from_wfreport(
hash=options.hash,
report=options.inreport,
tag=options.tag,
pcap=options.pcap
)
if options.outfile is not None:
f = open(options.outfile, 'w')
f.write(mp.to_xml())
f.close()
else:
sys.stdout.write(mp.to_xml())
def _parse_opts():
parser = argparse.ArgumentParser(
description="Convert Palo Alto Networks Wildfire reports to STIX/MAEC",
add_help=False
)
parser.add_argument(
'--version',
action='version',
version=__version__
)
parser.add_argument(
'-t', '--tag',
metavar='<pan-python tag>',
action='store',
help='pan-python tag for Wildfire API'
)
parser.add_argument(
'--verbose',
action='store_true',
help='verbose'
)
parser.add_argument(
'-h', '--hash',
action='store',
metavar='<hash>',
help='hash of the sample'
)
parser.add_argument(
'--help',
action='help',
help='help'
)
parser.add_argument(
'-i', '--in',
dest='inreport',
metavar='<report filename>',
action='store',
help='local Wildfire report'
)
parser.add_argument(
'--no-pcap',
dest='no_pcap',
action='store_true',
default=False,
help='do not include pcap'
)
parser.add_argument(
'--pcap',
dest='pcap',
metavar='<pcap source>',
action='store',
default='network',
help='pcap filename or \'network\' for retriving from Wildfire API'
)
parser.add_argument(
'--no-sample',
dest='no_sample',
action='store_true',
default=False,
help='do not include sample'
)
parser.add_argument(
'--sample',
dest='sample',
action='store',
default='network',
metavar='<sample source>',
help='sample filename or \'network\' for retriving from Wildfire API'
)
parser.add_argument(
'-f', '--outfmt',
action='store',
metavar='<output format>',
default='stix',
choices=['stix', 'stix-il', 'stix-ol', 'maec'],
help='output format'
)
parser.add_argument(
'-o', '--out',
action='store',
dest='outfile',
metavar='<output filename>',
help='output filename'
)
parser.add_argument(
'-e', '--evidence',
action='store',
dest='evidence',
type=float,
metavar='<evidence score>',
help='minimum evidence score'
)
parser.add_argument(
'--title',
action='store',
default=None,
metavar='<title>',
help='title of the STIX package'
)
parser.add_argument(
'-d', '--decontextualize',
action='store_true',
dest='decontext',
default=False,
help='remove context from observables'
)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
options = parser.parse_args()
if options.no_pcap:
options.pcap = None
if options.no_sample:
options.sample = None
if options.hash is not None and options.inreport is not None:
print('CRITICAL: only one of \'in\' or \'hash\' should be specified',
file=sys.stderr)
sys.exit(1)
if options.hash is None and options.inreport is None:
print('CRITICAL: one of \'in\' or \'hash\' should be specified',
file=sys.stderr)
sys.exit(1)
if options.hash is not None and options.tag is None:
print('CRITICAL: tag should be specified to retrieve reports by hash',
file=sys.stderr)
sys.exit(1)
if options.pcap == 'network' and options.tag is None:
print('CRITICAL: tag should be specified to retrieve pcaps',
file=sys.stderr)
sys.exit(1)
if options.sample == 'network' and options.tag is None:
print('CRITICAL: should be specified to retrieve samples',
file=sys.stderr)
sys.exit(1)
if options.outfmt == 'maec' and options.title is not None:
print('WARNING: title option ignored for MAEC format')
if options.decontext and options.outfmt not in ['stix-il']:
print('CRITICAL: decontext can be used only with stix-ol and stix-il')
sys.exit(1)
return options
def main():
options = _parse_opts()
loglevel = logging.INFO
if options.verbose:
loglevel = logging.DEBUG
logging.basicConfig(
format='%(asctime)s [%(levelname)s] %(message)s',
datefmt='%d/%m/%Y %I:%M:%S %p',
level=loglevel
)
if options.outfmt == 'stix':
dump_report_to_stix(options)
elif options.outfmt == 'maec':
dump_report_to_maec(options)
elif options.outfmt == 'stix-ol':
dump_report_to_stix_ol(options)
elif options.outfmt == 'stix-il':
dump_report_to_stix_il(options)
else:
logging.critical('unhandled output format %s' % options.outfmt)
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()