-
Notifications
You must be signed in to change notification settings - Fork 3
/
sfc.py
207 lines (161 loc) · 4.77 KB
/
sfc.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
import scapToMongo
from pymongo import MongoClient
import datetime
import subprocess
import time
import pprint
import sys
import smtplib
from email.mime.text import MIMEText
cl = MongoClient()
collUsers = cl["scans"]["users"]
def getDB(user,db="scans"):
coll = cl[db][user]
return coll
def scan(user,container,type="oval",db="scans",destination="",note=False):
'''
Main function for performing a scan
:param user: username
:param container: name of container
:param type: oval or xccdf
:param db: name of database (optional)
:param destination: name of report file to save to
:return: html (string) the raw html of the report
'''
if type not in ["xccdf","oval"]:
print("invalid param: type")
sys.exit(1)
db = str(db)
coll = cl[db][user]
type = type.lower()
if collUsers.find({"username":user}).count() < 1:
createUser(user)
#'''
#'''
'''
TODO:
We need to check for the os / version of the container
'''
# Create report filename
#
if destination == "":
if type == "oval":
destination = "report.html"
elif type == "xccdf":
destination = "report1.html"
else:
destination = "report-" + str(user) + str(container) + str(time.time())
'''
TODO:
Fix everything here...
'''
if type == "xccdf":
if len(coll.distinct("scanid",{"type":"compliance"})) > 20:
cleanupUser(user)
if len(coll.distinct("scanid")) > 20:
print("User quota exceeded: no more XCCDF scans allowed")
print("User cleanupUser(user,age=?) to clear entries")
sys.exit(1)
'''
subprocess.call(
"oscap-docker xccdf eval " +
"--profile xccdf_org.ssgproject.content_profile_common "+
"--report " + destination +
"ssg-ubuntu1604-ds.xml")
#'''
html,scanid = scapToMongo.xccdf(destination,user)
elif type == "oval":
# Record
if len(coll.distinct("scanid",{"type":"vulnerability"})) > 5:
cleanupUser(user)
if len(coll.distinct("scanid")) > 5:
print("User quota exceeded: no more OVAL scans allowed")
print("User cleanupUser(user,age=?) to clear entries")
'''
subprocess.call(
"oscap-docker oval eval " +
"--profile xccdf_org.ssgproject.content_profile_common "+
"--report " + destination +
"ssg-ubuntu1604-ds.xml")
#'''
html,scanid = scapToMongo.oval(destination,user)
else:
print("ERROR")
sys.exit(1)
'''
To here
'''
# associate the scanid with the user in
collUsers.update({"username":user},
{"$push":{"scans":scanid}}
)
#pprint.pprint(html)
sendNotification(user,html)
print("Done")
return html
def createUser(user,db="scans"):
'''
:param user: username
:param db: name of database (optional)
:return:
'''
print("Creating new user: ", user)
coll = cl[db][user]
coll.insert({"init":"init"})
collUsers.insert(
{"username": user,
"scans":[],
"email":"bsowens@bu.edu"}
)
def removeUser(user,db="scans"):
'''
:param user: username
:param db: name of database (optional)
:return:
'''
coll = cl[db][user]
coll.drop()
collUsers.remove(
{"username":user}
)
print("User: ", user, " is dead")
def cleanupUser(user,db="scans",age=24,type=["xccdf","oval"],all=False):
'''
:param user: username
:param db: name of database (optional)
:param age: deletes entries older than this number (hours) (optional)
:param all: override to delete all entries
:return:
'''
coll = cl[db][user]
if all == True:
coll.remove({})
print("Attemping autocleaup")
timeCutoff = datetime.datetime.now() - datetime.timedelta(hours=age)
coll.remove({
"timestamp": {"$lt": timeCutoff}}
)
def getVersion():
os = subprocess.call("echo $ID")
version = subprocess.call("echo $VERSION_ID")
return os,version
def sendNotification(user,html,email=""):
sender = 'no-reply@email.com'
receivers = ['bsowens@bu.edu']
subject = "Report"
message = "From: " + sender +\
"To: " + receivers[0] +\
"Subject: " + subject + "\n\n"
"Here is your report \n" + html
message.encode('utf-8').strip()
pprint.pprint(message)
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print("Successfully sent email")
except:
print("Error: unable to send email")
'''
scapToMongo.xccdf("report1.html","ben")
scapToMongo.oval("report.html","ben")
'''