forked from Sidnioulz/PolicyAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
executable file
·570 lines (506 loc) · 23 KB
/
__main__.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/usr/bin/env python3
from ApplicationStore import ApplicationStore
from AttackSimulator import AttackSimulator
from Event import dbgPrintExcludedEvents
from EventStore import EventStore
from FileStore import FileStore
from PreloadLoggerLoader import PreloadLoggerLoader
from SqlLoader import SqlLoader
from UserConfigLoader import UserConfigLoader
from GraphEngine import GraphEngine
from PolicyEngine import PolicyEngine
from FrequentFileEngine import FrequentFileEngine
from LibraryManager import LibraryManager
from Policies import OneLibraryPolicy, CompoundLibraryPolicy, UnsecurePolicy, \
FileTypePolicy, DesignationPolicy, FolderPolicy, \
OneFolderPolicy, DistantFolderPolicy, FilenamePolicy, \
OneDistantFolderPolicy, FutureAccessListPolicy, \
StickyBitPolicy, CustomLibraryPolicy, \
Win8Policy, Win10Policy, ProtectedFolderPolicy, \
LibraryFolderPolicy, RemovableMediaPolicy, \
FolderFilenamePolicy, FolderRestrictedAppsPolicy, \
CompositionalPolicy, HSecurePolicy, HBalancedPolicy, \
HBalancedSecuredPolicy, FolderSecuredPolicy, \
HUsableSecuredPolicy
from constants import DATABASENAME, USERCONFIGNAME
from utils import __setCheckMissing, __setDebug, __setOutputFs, \
__setRelatedFiles, __setScore, __setGraph, __setAttacks, \
__setPrintClusters, __setUser, __setCheckExcludedFiles, \
__setPlottingDisabled, __setSkip, __setPrintExtensions, \
__setFrequency, \
checkMissingEnabled, debugEnabled, outputFsEnabled, \
relatedFilesEnabled, scoreEnabled, graphEnabled, \
printClustersEnabled, checkExcludedFilesEnabled, \
skipEnabled, attacksEnabled, printExtensions, \
initMimeTypes, getDataPath, registerTimePrint, tprnt
import getopt
import sys
import os
import mimetypes
USAGE_STRING = 'Usage: __main__.py [--user=<NAME> --check-excluded-files ' \
'--check-missing --score\n\t\t--skip=<Policy,Policy,\'graphs' \
'\'> --clusters --graph --extensions\n\t\t--disable-plotting ' \
'--attacks --related-files --frequency\n\t\t--output=<DIR> ' \
'--debug] ' \
'\n\nor: __main__.py --inode=<INODE> [--user=<NAME> ' \
'--debug]' \
'\n\nor: __main__.py --post-analysis=<DIR,DIR,DIR> ' \
'[--related-files --debug]'\
'\n\nor: __main__.py --help'
# Main function
# @profile
def main(argv):
__opt_inode_query = None
__opt_post_analysis = None
__opt_quick_pol = None
# Parse command-line parameters
try:
(opts, args) = getopt.getopt(argv, "hta:cedf:o:q:sk:rpgGi:u:x",
["help",
"attacks",
"post-analysis=",
"check-missing",
"check-excluded-files",
"debug",
"frequency",
"inode",
"extensions",
"related-files",
"output=",
"output-fs=",
"score",
"quick-pol=",
"skip=",
"user",
"clusters",
"print-clusters",
"graph",
"graph-clusters",
"disable-plotting"])
except(getopt.GetoptError):
print(USAGE_STRING)
sys.exit(2)
else:
for opt, arg in opts:
if opt in ('-h', '--help'):
print(USAGE_STRING + "\n\n\n\n")
print("--attacks:\n\tSimulates attacks and reports "
"on proportions of infected files and apps.\n")
print("--check-excluded-files:\n\tPrints the lists of files "
"accessed by apps that also wrote to excluded\n\tfiles,"
" then aborts execution of the program.\n")
print("--check-missing:\n\tChecks whether some Desktop IDs "
"for apps in the user's directory are\n\tmissing. If so,"
" aborts execution of the program.\n")
print("--clusters:\n\tPrints clusters of files with "
"information flows to one another. Requires\n\tthe "
"--score option.\n")
print("--debug:\n\tPrints additional debug information in "
"various code paths to help debug\n\tthe program.\n")
print("--disable-plotting:\n\tDo not plot cluster graphs. See "
"the --graph option.\n")
print("--extensions:\n\tPrints file extensions and MIME type "
"associations for this user.\n")
print("--frequency:\n\tSets the frequency used by the "
"frequent-itemsets algorithm in the\n\t--related-files "
"post-analysis. Requires the --related-files option.\n")
print("--graph:\n\tFind communities in file/app "
"accesses using graph theory methods.\n")
print("--help:\n\tPrints this help information and exits.\n")
print("--output=<DIR>:\n\tSaves a copy of the simulated "
"files, and some information on events\n\trelated to "
"them, in a folder created at the <DIR> path.\n")
print("--post-analysis=<DIR,DIR,DIR>:\n\t"
"Uses the value pointed to"
" by --output in order to produce graphs and\n\t"
"statistics.\n")
print("--quick-pol=Policy:\n\tReplace the default policies "
"with this one single Policy.\n")
print("--related-files:\n\tMines for files that are frequently"
" accessed together by apps. Produces\n\toutput files in"
" scoring mode, and an analysis output in post-analysis"
"\n\tmode. See also --frequency.\n")
print("--score:\n\tCalculates the usability and security "
"scores of a number of file access\n\tcontrol policies"
", replayed over the simulated accesses. Prints results"
"\n\tand saves them to the output directory.\n")
print("--skip=<Policy,Policy,'graphs'>:\n\tSkip the scoring of "
"policies in the lists. If the list contains the word"
"\n\t'graphs', skips the general graph computation.\n")
sys.exit()
elif opt in ('-c', '--check-missing'):
__setCheckMissing(True)
elif opt in ('-e', '--check-excluded-files'):
__setCheckExcludedFiles(True)
elif opt in ('-x', '--extensions'):
__setPrintExtensions(True)
elif opt in ('-d', '--debug'):
__setDebug(True)
elif opt in ('-r', '--related-files'):
__setRelatedFiles(True)
elif opt in ('-s', '--score'):
__setScore(True)
elif opt in ('-p', '--print-clusters', '--clusters'):
__setPrintClusters(True)
elif opt in ('-g', '--graph-clusters', '--graph'):
__setGraph(True)
elif opt in ('-t', '--attacks'):
__setAttacks(True)
elif opt in ('-G', '--disable-plotting'):
__setPlottingDisabled(True)
elif opt in ('-f', '--frequency'):
if not arg:
print(USAGE_STRING)
sys.exit(2)
__setFrequency(arg[1:] if arg[0] == '=' else arg)
elif opt in ('-o', '--output-fs', '--output'):
if not arg:
print(USAGE_STRING)
sys.exit(2)
__setOutputFs(arg[1:] if arg[0] == '=' else arg)
elif opt in ('-u', '--user'):
if not arg:
print(USAGE_STRING)
sys.exit(2)
__setUser(arg[1:] if arg[0] == '=' else arg)
elif opt in ('-i', '--inode'):
if not arg:
print(USAGE_STRING)
sys.exit(2)
try:
__opt_inode_query = (arg[1:] if arg[0] == '=' else arg)
except(ValueError) as e:
print(USAGE_STRING)
sys.exit(2)
elif opt in ('-a', '--post-analysis'):
if not arg:
print(USAGE_STRING)
sys.exit(2)
__opt_post_analysis = (arg[1:] if arg[0] == '=' else arg)
elif opt in ('-q', '--quick-pol'):
if not arg:
print(USAGE_STRING)
sys.exit(2)
__opt_quick_pol = (arg[1:] if arg[0] == '=' else arg)
elif opt in ('-k', '--skip'):
if not arg:
print(USAGE_STRING)
sys.exit(2)
__opt_skip = (arg[1:] if arg[0] == '=' else arg)
__setSkip(__opt_skip.split(","))
registerTimePrint()
if __opt_post_analysis:
if relatedFilesEnabled():
tprnt("Starting post-analysis of related files...\n")
engine = FrequentFileEngine()
engine.processFrequentItemLists(__opt_post_analysis)
else:
tprnt("Starting post-analysis of usability/security scores...\n")
from AnalysisEngine import AnalysisEngine
if outputFsEnabled():
engine = AnalysisEngine(inputDir=__opt_post_analysis,
outputDir=outputFsEnabled())
else:
engine = AnalysisEngine(inputDir=__opt_post_analysis)
engine.analyse()
sys.exit(0)
# Make the application, event and file stores
store = ApplicationStore.get()
evStore = EventStore.get()
fileStore = FileStore.get()
initMimeTypes()
datapath = getDataPath()
# Load up user-related variables
userConf = UserConfigLoader.get(path=datapath+USERCONFIGNAME)
# Load up and check the SQLite database
sql = None
tprnt("\nLoading the SQLite database: %s..." % (datapath+DATABASENAME))
try:
sql = SqlLoader(datapath+DATABASENAME)
except ValueError as e:
print("Failed to parse SQL: %s" % e.args[0], file=sys.stderr)
sys.exit(-1)
if checkMissingEnabled():
tprnt("Checking for missing application identities...")
sql.listMissingActors()
sql.loadDb(store)
sqlAppCount = sql.appCount
sqlInstCount = sql.instCount
sqlEvCount = sql.eventCount
sqlValidEvCount = sql.validEventRatio
tprnt("Loaded the SQLite database.")
# Load up the PreloadLogger file parser
tprnt("\nLoading the PreloadLogger logs in folder: %s..." % datapath)
pll = PreloadLoggerLoader(datapath)
if checkMissingEnabled():
tprnt("Checking for missing application identities...")
pll.listMissingActors()
pll.loadDb(store)
pllAppCount = pll.appCount
pllInstCount = pll.instCount
pllEvCount = pll.eventCount
pllValidEvCount = pll.validEventRatio
tprnt("Loaded the PreloadLogger logs.")
# Resolve actor ids in all apps' events
tprnt("\nUsing PreloadLogger Applications to resolve interpreters in "
"Zeitgeist Applications...")
(interpretersAdded, instancesEliminated) = store.resolveInterpreters()
tprnt("Resolved interpreter ids in %d Applications, and removed %d "
"instances by merging them with another as a result." % (
interpretersAdded, instancesEliminated))
# Update events' actor ids in the ApplicationStore, then take them and send
# them to the EvnetStore. Finally, sort the EventStore by timestamp.
tprnt("\nInserting and sorting all events...")
store.sendEventsToStore()
evStore.sort()
evCount = evStore.getEventCount()
tprnt("Sorted all %d events in the event store." % evCount)
# Simulate the events to build a file model
tprnt("\nSimulating all events to build a file model...")
evStore.simulateAllEvents()
del sql
del pll
evStore.sort()
tprnt("Simulated all events. %d files initialised." % len(fileStore))
appCount = store.getAppCount()
userAppCount = store.getUserAppCount()
instCount = len(store)
userInstCount = store.getUserInstCount()
fileCount = len(fileStore)
docCount = fileStore.getUserDocumentCount(userConf.getSetting("HomeDir"))
if printExtensions():
exts = set()
for f in fileStore:
exts.add(f.getExtension())
try:
exts.remove(None)
except(KeyError):
pass
tprnt("Info: the following file extensions were found:")
for e in sorted(exts):
print("\t%s: %s" % (e, mimetypes.guess_type("f.%s" % e, strict=False)))
if checkExcludedFilesEnabled():
tprnt("\nPrinting files written and read by instances which wrote"
"to excluded directories...")
dbgPrintExcludedEvents()
import time as t
t.sleep(10)
# Manage --inode queries
if __opt_inode_query:
inodes = __opt_inode_query.split(",")
for inode in sorted(int(i) for i in inodes):
f = fileStore.getFile(inode)
tprnt("\nInode queried: %d" % inode)
tprnt("Corresponding file: %s\n\t(%s)" % (f.getName(), f))
sys.exit(0)
# Print the model as proof of concept
if debugEnabled():
tprnt("\nPrinting the file model...\n")
fileStore.printFiles(showDeleted=True,
showCreationTime=True,
showDocumentsOnly=True,
userHome=userConf.getSetting("HomeDir"),
showDesignatedOnly=False)
# Make the filesystem corresponding to the model
if outputFsEnabled():
tprnt("\nMaking a copy of the file model at '%s'...\n" %
outputFsEnabled())
fileStore.makeFiles(outputDir=outputFsEnabled(),
showDeleted=True,
showDocumentsOnly=False,
userHome=userConf.getSetting("HomeDir"),
showDesignatedOnly=False)
with open(os.path.join(outputFsEnabled(), "statistics.txt"), "w") as f:
msg = "SQL: %d apps; %d instances; %d events; %d%% valid\n" % \
(sqlAppCount, sqlInstCount, sqlEvCount, sqlValidEvCount)
msg += "PreloadLogger: %d apps; %d instances; %d events; " \
"%d%% valid\n" % \
(pllAppCount, pllInstCount, pllEvCount, pllValidEvCount)
msg += "Simulated: %d apps; %d instances; %d user apps; %d user" \
" instances; %d events; %d files; %d user documents\n" % \
(appCount, instCount, userAppCount, userInstCount,
evCount, fileCount, docCount)
exclLists = userConf.getDefinedSecurityExclusionLists()
for l in exclLists:
msg += "Exclusion list '%s' defined.\n" % l
print(msg, file=f)
# Build a general access graph.
if graphEnabled():
skipList = skipEnabled()
if skipList and 'graphs' in skipList:
tprnt("\nGraphs in skip list, skipping global graph generation.")
else:
engine = GraphEngine.get()
engine.runGraph(policy=None)
# Policy engine. Create a policy and run a simulation to score it.
if scoreEnabled() or attacksEnabled() or graphEnabled():
engine = PolicyEngine()
if __opt_quick_pol:
policies = [__opt_quick_pol]
polArgs = [None]
else:
policies = [CompoundLibraryPolicy,
CustomLibraryPolicy,
DesignationPolicy,
DistantFolderPolicy,
FilenamePolicy,
FileTypePolicy,
FolderPolicy,
OneDistantFolderPolicy,
OneFolderPolicy,
OneLibraryPolicy,
UnsecurePolicy,
Win10Policy,
Win8Policy,
HSecurePolicy,
HBalancedPolicy,
'HSecureSbPolicy',
'HSecureSbFaPolicy',
'HSecureFaPolicy',
'HBalancedSbPolicy',
'HBalancedSbFaPolicy',
'HBalancedFaPolicy',
'OneDistantFolderSbPolicy',
'OneDistantFolderSbFaPolicy',
'OneDistantFolderFaPolicy',
'HUsableSecuredSbPolicy',
'HUsableSecuredSbFaPolicy',
'HUsableSecuredFaPolicy',
'HBalancedSecuredSbPolicy',
'HBalancedSecuredSbFaPolicy',
'HBalancedSecuredFaPolicy',
'DistantFolderSbPolicy',
'DistantFolderSbFaPolicy',
'DistantFolderFaPolicy',
'LibraryFolderSbPolicy',
'LibraryFolderSbFaPolicy',
'LibraryFolderFaPolicy',
'FileTypeSbPolicy',
'FileTypeSbFaPolicy',
'FileTypeFaPolicy',
'OneFolderSbPolicy',
'OneFolderSbFaPolicy',
'OneFolderFaPolicy',
'FolderSbPolicy',
'FolderSbFaPolicy',
'FolderFaPolicy',
'OneLibrarySbPolicy',
'OneLibrarySbFaPolicy',
'OneLibraryFaPolicy',
'CompoundLibrarySbPolicy',
'CompoundLibrarySbFaPolicy',
'CompoundLibraryFaPolicy',
'CustomLibrarySbPolicy',
'CustomLibrarySbFaPolicy',
'CustomLibraryFaPolicy',
]
polArgs = [None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
]
# dict(folders=["~/Downloads", "/tmp"])
skipList = skipEnabled()
for (polIdx, polName) in enumerate(policies):
pol = None
arg = polArgs[polIdx]
# Names with certain suffixes are dynamically generated policies.
if isinstance(polName, str):
if polName.endswith('SbPolicy'):
pols = [getattr(sys.modules[__name__], polName[:-8]+'Policy'),
StickyBitPolicy]
args = [arg,
dict(folders=["~", "/media", "/mnt"])]
pol = CompositionalPolicy(pols, args, polName)
elif polName.endswith('SbFaPolicy'):
pols = [getattr(sys.modules[__name__], polName[:-10]+'Policy'),
StickyBitPolicy,
FutureAccessListPolicy]
args = [arg,
dict(folders=["~", "/media", "/mnt"]),
None]
pol = CompositionalPolicy(pols, args, polName)
elif polName.endswith('FaPolicy'):
pols = [getattr(sys.modules[__name__], polName[:-8]+'Policy'),
FutureAccessListPolicy]
args = [arg,
None]
pol = CompositionalPolicy(pols, args, polName)
# A normal policy, just invoke it directly.
else:
polName = getattr(sys.modules[__name__], polName)
# Existing policies, with arguments / or normal policies passed as
# strings, including via the --quick flag.
if not pol:
pol = polName(**arg) if arg else polName()
tprnt("\nRunning %s..." % pol.name)
if skipList and pol.name in skipList:
tprnt("%s is in skip list, skipping." % pol.name)
continue
engine.runPolicy(pol,
outputDir=outputFsEnabled(),
printClusters=printClustersEnabled())
if pol.name == "FileTypePolicy" and checkMissingEnabled():
pol.abortIfUnsupportedExtensions()
if attacksEnabled():
tprnt("Simulating attacks on %s..." % pol.name)
sim = AttackSimulator(seed=0)
sim.runAttacks(pol, outputDir=outputFsEnabled() or "/tmp/")
del pol
# Calculate frequently co-accessed files:
if relatedFilesEnabled():
engine = FrequentFileEngine()
tprnt("\nMining for frequently co-accessed file types...")
engine.mineFileTypes()
if __name__ == "__main__":
main(sys.argv[1:])