-
Notifications
You must be signed in to change notification settings - Fork 0
/
smass.py
85 lines (64 loc) · 2.32 KB
/
smass.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
import os
from os.path import basename, isfile
import argparse
import logging
import sys
from lib.configreader import parse_config
from lib.Artifact import Artifact
def read_config():
configfile = "smass.conf"
parser = argparse.ArgumentParser()
description='Arguments to start SMASS'
parser.add_argument('--configfile', type=str, nargs=1,
help='SMASS configuration file')
args, remaining_argv = parser.parse_known_args()
parser.add_argument('--type', type=str, nargs=1,
help='Select type of file')
parser.add_argument('--source', type=str, nargs='?',
help='Where the artifact was obtained')
parser.add_argument('--tags', type=str, nargs='*',
help='Tags to describe the artifact')
parser.add_argument('--comment', type=str, nargs='?',
help='Comment(s) describing the artifact')
parser.add_argument("-v", "--verbose", help="increase output verbosity",
action="store_true")
parser.add_argument("-d", "--debug", help="Debug output",
action="store_true")
parser.add_argument('artifact', type=str,
help='Path to the artifact')
if args.configfile:
configfile = args.configfile
config = parse_config(configfile)
parser.set_defaults(**config)
args = parser.parse_args(remaining_argv)
return args
config = read_config()
if config.verbose:
logging.basicConfig(level=logging.INFO)
if config.debug:
logging.basicConfig(level=logging.DEBUG)
if not isfile(config.artifact):
logging.critical("Artifact {} not found: no such file or directory".format(config.artifact))
sys.exit(1)
path = config.artifact
name = basename(config.artifact)
statinfo = os.stat(path)
size = statinfo.st_size
try:
f = open(config.artifact, "rb")
data = f.read()
except IOError, e:
logging.critical("Could not read {}: {} ".format(path, e))
sys.exit(1)
artifact = Artifact(config, name, size, data)
artifact.analyse()
if not hasattr(artifact, 'report'):
logging.critical("Analysis report not generated!")
sys.exit(1)
meta = {
"source": config.source,
"tags": config.tags,
"comment": config.comment
}
artifact.report['meta'] = meta
artifact.database.collection.insert(artifact.report)