-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcrx-jsinventory
executable file
·136 lines (119 loc) · 4.07 KB
/
crx-jsinventory
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
#!/usr/bin/env python3.7
#
# Copyright (C) 2017 The University of Sheffield, UK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Tool for extracting crx file from a tar archive."""
import sys
import getopt
import csv
import logging
from collections import OrderedDict
from zipfile import ZipFile
from tabulate import tabulate
from ExtensionCrawler.js_decomposer import decompose_js
from ExtensionCrawler.config import (const_log_format)
def helpmsg():
"""Print help message."""
print("crx-jsinventory [OPTION] crx-file|js-file")
print(" -h print this help text")
print(" -c=<FILE> cvs file (output)")
print(" -v verbose")
print(
" -d disable use of database with file information (not recommended)"
)
print(" -s silent")
def main(argv):
"""Main function of the extension crawler."""
verbose = False
silent = False
csvfile = None
database = True
try:
opts, args = getopt.getopt(argv, "hvdsc:", ["cvs="])
except getopt.GetoptError:
helpmsg()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
helpmsg()
sys.exit()
elif opt == '-v':
verbose = True
elif opt == '-s':
silent = True
elif opt == '-d':
database = False
elif opt in ('-c', "--cvs"):
csvfile = arg
if len(args) > 0:
filename = args[0]
else:
helpmsg()
sys.exit()
if verbose:
loglevel = logging.INFO
else:
loglevel = logging.WARNING
logger = logging.getLogger()
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(logging.Formatter(const_log_format()))
logger.addHandler(ch)
logger.setLevel(loglevel)
fieldnames = [
'filename', 'path', 'size', 'dec_size', 'md5', 'sha1', 'mimetype',
'description', 'encoding', 'type', 'detectionMethod',
'detectionMethodDetails', 'lib', 'version', 'lib_filename',
'evidenceText', 'evidenceStartPos', 'evidenceEndPos'
]
brief_fieldnames = [
'filename', 'md5', 'type', 'detectionMethod', 'lib', 'version',
'lib_filename'
]
if filename.endswith('.crx'):
with ZipFile(filename) as crxobj:
inventory = decompose_js(crxobj, database)
else:
inventory = decompose_js(filename, database)
if not silent:
if verbose:
print_fieldnames = fieldnames
else:
print_fieldnames = brief_fieldnames
print_inventory = []
for item in inventory:
tmp = {k: item[k] for k in print_fieldnames}
if 'type' in tmp:
tmp['type'] = tmp['type'].value
if 'detectionMethod' in tmp:
tmp['detectionMethod'] = tmp['detectionMethod'].value
if 'md5' in tmp:
tmp['md5'] = tmp['md5'].hex()
if 'sha1' in tmp:
tmp['sha1'] = tmp['sha1'].hex()
print_inventory.append(
OrderedDict(
sorted(
tmp.items(),
key=lambda t: print_fieldnames.index(t[0]))))
print(tabulate(print_inventory, headers='keys'))
if csvfile is not None:
with open(csvfile, 'w') as csvobj:
writer = csv.DictWriter(csvobj, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(inventory)
if __name__ == "__main__":
main(sys.argv[1:])