-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsrl.py
234 lines (191 loc) · 7.77 KB
/
nsrl.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
#
# Copyright (c) 2013-2018 Quarkslab.
# This file is part of IRMA project.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the top-level directory
# of this distribution and at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# No part of the project, including this file, may be copied,
# modified, propagated, or distributed except according to the
# terms contained in the LICENSE file.
import logging
import json
import plyvel
log = logging.getLogger(__name__)
class NSRL(object):
def __init__(self,
nsrl_file,
nsrl_product,
nsrl_os, nsrl_manufacturer,
**kwargs):
# TODO: need to specify paths in constructor,
# temporary pass via kwargs
self.nsrl_file = NSRLFile(nsrl_file)
self.nsrl_product = NSRLProduct(nsrl_product)
self.nsrl_os = NSRLOS(nsrl_os)
self.nsrl_manufacturer = NSRLManufacturer(nsrl_manufacturer)
def lookup_by_sha1(self, sha1sum):
operations = [
(sha1sum, 'SHA-1', self.nsrl_file, None),
(None, 'ProductCode', self.nsrl_product, 'SHA-1'),
(None, 'OpSystemCode', self.nsrl_os, 'SHA-1'),
(None, 'MfgCode', self.nsrl_manufacturer, 'ProductCode')
]
entries = dict((name, {}) for (_, name, _, _) in operations)
for value, key, database, where in operations:
if value:
entries[key][value] = database.get(bytes(value))
else:
subkeys = set()
for subkey, subitem in list(entries[where].items()):
if not isinstance(subitem, list):
subitem = [subitem]
subkeys.update([x[key] for x in subitem])
for subkey in subkeys:
entries[key][subkey] = database.get(bytes(subkey))
return entries
class NSRLCreate:
key = None
db = None
def __init__(self, db, records, **kwargs):
self.db = plyvel.DB(db, **kwargs, create_if_missing=True)
def get(self, db_key):
return self.db.get(bytes(db_key, 'utf-8'))
@classmethod
def create_database(cls, dbfile, records, **kwargs):
i = 0
from csv import DictReader
csv_file = open(records, 'r')
csv_entries = DictReader(csv_file)
db = plyvel.DB(dbfile, **kwargs, create_if_missing=True)
try:
for row in csv_entries:
key = bytes(row.pop(cls.key), 'utf-8')
value = db.get(key, None)
if not value:
row = json.dumps(row).encode('utf-8')
db.put(key, row)
else:
db.delete(key)
existing_entry = json.loads(value.decode('utf-8'))
row = { key: value for (key, value) in dict(list(existing_entry.items()) + list(row.items())).items() }
row = json.dumps(row).encode('utf-8')
db.put(key, row)
except UnicodeDecodeError:
i += 1
print("Number of non-unicode hex: ", i)
db.close()
# ==================
# NSRL File Record
# ==================
class NSRLFile(NSRLCreate):
key = "SHA-1"
def __init__(self, db, **kwargs):
# give default_dir value somewhere
super(NSRLFile, self).__init__(db, 'NSRLFile.txt', **kwargs)
# =================
# NSRL OS Record
# =================
class NSRLOS(NSRLCreate):
key = "OpSystemCode"
def __init__(self, db, **kwargs):
# give default_dir value somewhere
super(NSRLOS, self).__init__(db, 'NSRLOS.txt', **kwargs)
# ================
# NSRL OS Record
# ================
class NSRLManufacturer(NSRLCreate):
key = "MfgCode"
def __init__(self, db, **kwargs):
# give default_dir value somewhere
super(NSRLManufacturer, self).__init__(db, 'NSRLMfg.txt', **kwargs)
# =====================
# NSRL Product Record
# =====================
class NSRLProduct(NSRLCreate):
key = "ProductCode"
def __init__(self, db, **kwargs):
# give default_dir value somewhere
super(NSRLProduct, self).__init__(db, 'NSRLProd.txt', **kwargs)
if __name__ == '__main__':
##########################################################################
# local import
##########################################################################
import argparse
##########################################################################
# defined functions
##########################################################################
nsrl_databases = {
'file': NSRLFile,
'os': NSRLOS,
'manufacturer': NSRLManufacturer,
'product': NSRLProduct,
}
def nsrl_create_database(**kwargs):
database_type = kwargs['type']
nsrl_databases[database_type].create_database(kwargs['database'],
kwargs['filename'])
def nsrl_get(**kwargs):
database_type = kwargs['type']
database = nsrl_databases[database_type](kwargs['database'])
value = database.get(kwargs['key'])
print(("key {0}: value {1}".format(kwargs['key'], value)))
##########################################################################
# arguments
##########################################################################
# define command line arguments
desc_msg = 'NSRL database module CLI mode'
parser = argparse.ArgumentParser(description=desc_msg)
parser.add_argument('-v',
'--verbose',
action='count',
default=0)
subparsers = parser.add_subparsers(help='sub-command help')
# Create the database
help_msg = 'create NSRL records into a database'
create_parser = subparsers.add_parser('create',
help=help_msg)
create_parser.add_argument('-t',
'--type',
type=str,
choices=['file', 'os',
'manufacturer', 'product'],
help='type of the record')
create_parser.add_argument('filename',
type=str,
help='filename of the NSRL record')
create_parser.add_argument('database',
type=str,
help='database to store NSRL records')
create_parser.set_defaults(func=nsrl_create_database)
# create the scan parser
get_parser = subparsers.add_parser('get',
help='get the entry from database')
get_parser.add_argument('-t',
'--type',
type=str,
choices=['file', 'os', 'manufacturer', 'product'],
help='type of the record')
get_parser.add_argument('database',
type=str,
help='database to read NSRL records')
get_parser.add_argument('key',
type=str,
help='key to retreive')
get_parser.set_defaults(func=nsrl_get)
args = parser.parse_args()
# set verbosity
if args.verbose == 1:
logging.basicConfig(level=logging.INFO)
elif args.verbose == 2:
logging.basicConfig(level=logging.DEBUG)
args = vars(parser.parse_args())
func = args.pop('func')
# with 'func' removed, args is now a kwargs
# with only the specific arguments
# for each subfunction useful for interactive mode.
func(**args)