apt-sec.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. #!/usr/bin/python3
  2. ## Based on the perl code of Trustminer by CASED
  3. ## Nikos
  4. import sys
  5. import os
  6. from pymongo import MongoClient
  7. #mongodb assumes database at default path
  8. import logging, sys
  9. import configparser
  10. import json
  11. import urllib.request
  12. import datetime
  13. import debian_advisory as da
  14. import timeseries as ts
  15. import cveparse as cv
  16. import matplotlib.pyplot as plt
  17. import numpy as np
  18. from dateutil import parser
  19. import plotly.plotly as py
  20. import plotly.graph_objs as go
  21. import machine_learning as ml
  22. logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
  23. ## Increase the recursion limit by much to allow bs to parse large files
  24. ## This is not good practise
  25. sys.setrecursionlimit(6000)
  26. #load config file as library
  27. config = configparser.ConfigParser()
  28. config.read('config_test')
  29. if config.sections == []:
  30. print('configuration file not found\n')
  31. sys.exit(1)
  32. #global variables
  33. secperday = 60*60*24
  34. now = datetime.datetime.now()
  35. verbosity = 1
  36. ###############################################################################
  37. ## logging
  38. # 1 fatal errors
  39. # 2 errors
  40. # 3 note
  41. # 4 trace
  42. # 5 debug
  43. def msg(lvl,msg):
  44. if lvl <= int(config['LOG']['loglevel']):
  45. print(msg)
  46. def debug(msg):
  47. msg(5, msg)
  48. # Need to see if this is necessary
  49. ## load state, different from DBs in that we always need it
  50. def load_state():
  51. cache = config['DIR']['cache_dir'] + 'state'
  52. err = 0
  53. state = dict()
  54. try:
  55. with open(cache) as json_data:
  56. state = json.load(json_data)
  57. except FileNotFoundError:
  58. # Load default state - start from the beginning
  59. state['cache_dir'] = cache
  60. state['next_adv'] = 0
  61. state['next_fsa'] = 0
  62. state['Packages'] = ''
  63. state['Sources'] = ''
  64. state['Sha1Sums'] = ''
  65. err += 1
  66. return (state, err)
  67. ###############################################################################
  68. ## save state, different from DBs in that we always need it
  69. def save_state(state):
  70. cache = config['DIR']['cache_dir'] + 'state'
  71. try:
  72. with open(cache, 'w') as fp:
  73. json.dump(state, fp)
  74. except IOError:
  75. print('write cache state failed!! Fatal error')
  76. sys.exit(1)
  77. ###############################################################################
  78. ## load sha lists :TODO later
  79. def load_sha1lists():
  80. cache = config['DIR']['cache_dir'] + 'state'
  81. ###############################################################################
  82. ## save sha lists :TODO later
  83. def save_sha1lists():
  84. pass
  85. ###############################################################################
  86. ## load from files
  87. def load_DBs():
  88. dsatable = dict()
  89. src2dsa = dict()
  90. dsa2cve = dict()
  91. cvetable = dict()
  92. cache = config['DIR']['cache_dir']
  93. cache_dsatable = cache + 'dsatable'
  94. try:
  95. with open(cache_dsatable) as fp:
  96. dsatable = json.load(fp)
  97. except (IOError, ValueError):
  98. print('read cache dsatable failed!! Maybe first run of the system?')
  99. cache_src2dsa = cache + 'src2dsa'
  100. try:
  101. with open(cache_src2dsa) as fp:
  102. src2dsa = json.load(fp)
  103. except (IOError, ValueError):
  104. print('read cache src2dsa failed!! Maybe first run of the system?')
  105. cache_dsa2cve = cache + 'dsa2cve'
  106. try:
  107. with open(cache_dsa2cve) as fp:
  108. dsa2cve = json.load(fp)
  109. except (IOError, ValueError):
  110. print('read cache dsa2cve failed!! Maybe first run of the system?')
  111. cache_cvetable = cache + 'cvetable'
  112. try:
  113. with open(cache_cvetable) as fp:
  114. cvetable = json.load(fp)
  115. except (IOError, ValueError):
  116. print('read cache cvetable failed!! Maybe first run of the system?')
  117. return(dsatable, src2dsa, dsa2cve, cvetable)
  118. ###############################################################################
  119. ## help for save_DBs
  120. def myconverter(o):
  121. if isinstance(o, datetime.datetime) or isinstance(o, datetime.timedelta):
  122. return str(o)
  123. ###############################################################################
  124. ## save to files
  125. def save_DBs(dsatable, src2dsa, dsa2cve, cvetable):
  126. cache = config['DIR']['cache_dir']
  127. cache_dsatable = cache + 'dsatable'
  128. try:
  129. with open(cache_dsatable, 'w') as fp:
  130. json.dump(dsatable, fp, default = myconverter)
  131. except IOError:
  132. print('write cache dsatable failed!! Fatal error')
  133. sys.exit(1)
  134. cache_src2dsa = cache + 'src2dsa'
  135. try:
  136. with open(cache_src2dsa, 'w') as fp:
  137. json.dump(src2dsa, fp)
  138. except IOError:
  139. print('write cache src2dsa failed!! Fatal error')
  140. sys.exit(1)
  141. cache_dsa2cve = cache + 'dsa2cve'
  142. try:
  143. with open(cache_dsa2cve, 'w') as fp:
  144. json.dump(dsa2cve, fp)
  145. except IOError:
  146. print('write cache dsa2cve failed!! Fatal error')
  147. sys.exit(1)
  148. cache_cvetable = cache + 'cvetable'
  149. try:
  150. with open(cache_cvetable, 'w') as fp:
  151. json.dump(cvetable, fp, default = myconverter)
  152. except IOError:
  153. print('write cache cvetable failed!! Fatal error')
  154. sys.exit(1)
  155. ###############################################################################
  156. ## Fetch current Packages, Sources and sha1sums files
  157. ## These are needed to find CVE stats by sha1sums/pkg-names
  158. ## Only Sha1Sums is custom generated, others are from Debian.
  159. ## FIXME: Server might do on-the-fly gzip (but should not for bzip2)
  160. ## Return: 1 on success, to signal that new parsing is needed.
  161. def fetchMeta(filename):
  162. urlbase = config['URL']['pkg_base_url']
  163. mydir = config['DIR']['cache_dir']
  164. bzFile = filename + '.bz2'
  165. url = urlbase + bzFile
  166. logging.info('Checking meta file from ' + url + '\n')
  167. # Download file
  168. urllib.request.urlretrieve(url, mydir + bzfile)
  169. # TODO catch exceptions like file not found
  170. # TODO check if file has changed, if it is new unpack
  171. ###############################################################################
  172. # Sources and Packages are not completely consistent, esp for debian-multimedia
  173. # He we store manual mappings for these..
  174. def addOrphanPkgs(pkg2src):
  175. pkg2src['liblame-dev'] = "lame";
  176. pkg2src['lame-extras'] = "lame";
  177. pkg2src['moonlight'] = "moon";
  178. pkg2src['libmoon0'] = "moon";
  179. pkg2src['xmms-mp4'] = "xmms2";
  180. pkg2src['xmms-mp4'] = "xmms2";
  181. pkg2src['lazarus-src-0.9.30'] = "lazarus";
  182. pkg2src['lazarus-ide-0.9.30'] = "lazarus";
  183. pkg2src['lcl-qt4-0.9.30'] = "lazarus";
  184. pkg2src['lazarus-ide-qt4-0.9.30'] = "lazarus";
  185. pkg2src['lcl-gtk2-0.9.30'] = "lazarus";
  186. pkg2src['lazarus-ide-gtk2-0.9.30'] = "lazarus";
  187. pkg2src['lcl-units-0.9.30'] = "lazarus";
  188. pkg2src['lazarus-0.9.30'] = "lazarus";
  189. pkg2src['lazarus-doc-0.9.30'] = "lazarus";
  190. pkg2src['lcl-0.9.30'] = "lazarus";
  191. pkg2src['lcl-utils-0.9.30'] = "lazarus";
  192. pkg2src['lcl-nogui-0.9.30'] = "lazarus";
  193. pkg2src['libx264-65'] = "x264";
  194. pkg2src['libx264-114'] = "x264";
  195. pkg2src['libx264-60'] = "x264";
  196. # pkg2src['libmlt3']
  197. # pkg2src['libgmerlin-avdec0']
  198. # pkg2src['libxul-dev']
  199. # pkg2src['libmyth-0.23.1-0']
  200. # pkg2src['libmpeg3hv']
  201. # pkg2src['libquicktimehv']
  202. # pkg2src['libxul0d']
  203. # pkg2src['acroread-fonts-kor']
  204. ###############################################################################
  205. ## Parse dpkg Packages file, create map deb-name->pkg-name
  206. def parsePackages(pkgfile):
  207. mydir = cache = config['DIR']['cache_dir']
  208. deb2pkg = dict()
  209. pkg2virt = dict()
  210. virt2pkg = ()
  211. logging.info('Parsing Packages file...\n')
  212. pkgfile = mydir + pkgfile
  213. #TODO open and parse pkg file
  214. ###############################################################################
  215. ## Parse dpkg Sources file, create map pkg-name->src-name
  216. def parseSources(srcfile):
  217. mydir = cache = config['DIR']['cache_dir']
  218. checklinecont = 0
  219. pkg2src = dict()
  220. logging.info('Parsing Sources file...\n')
  221. srcfile = mydir + srcfile
  222. #TODO open and parse sources file
  223. ###############################################################################
  224. def getSHA1(myhash, collection):
  225. return collection.find({"hash": myhash})
  226. ###############################################################################
  227. def addSHA1(myhash, deb, src):
  228. dic = getSHA1(myhash)
  229. thash = dic["hash"]
  230. tdeb = dic["deb"]
  231. tsrc = dic["src"]
  232. #TODO insert SHA to database
  233. ###############################################################################
  234. ## Parse Sha1Sums file. Format: "sha1sum::deb-name::unix-file-path"
  235. ## Create 2 maps: sha1sum->file, file->deb-name
  236. def parseSha1Sums(sha1file):
  237. pass
  238. ###############################################################################
  239. ## Parse local dpkg status, return list of debs
  240. def parseStatus(stsfile):
  241. pass
  242. ###############################################################################
  243. ## Parse Advisory (only Debian supported atm
  244. def parseAdvisory(adv):
  245. if state['vendor'] == 'debian':
  246. return da.parseDSAhtml(adv)
  247. else:
  248. print('Unsupported distribution. We only support Debian at the moment')
  249. system.exit(1)
  250. ###############################################################################
  251. ## Manually fix problems with Advisory entries
  252. def fixAdvisoryQuirks(arg, state, dsastats):
  253. if state['vendor'] == 'debian':
  254. return da.fixDSAquirks(arg, dsastats)
  255. else:
  256. print('Unsupported distribution. We only support Debian at the moment')
  257. system.exit(1)
  258. ###############################################################################
  259. ## Extract CVE ids from new advisories and print URL for mirror script
  260. def printCVEs(myid,adv, state):
  261. logging.info('Looking for CVEs in advisory...\n')
  262. dsastats = parseAdvisory(adv)
  263. if dsastats == []:
  264. return
  265. ## fix DSAs that don't contain correct CVE refs
  266. dsastats = fixAdvisoryQuirks(myid, state, dsastats);
  267. #TODO Fix this part
  268. ##for cve_id in dsastats
  269. ###############################################################################
  270. ## Update internal vuln. DB with new Advisory info
  271. ## Creates CVEtable for MTBF computation:
  272. ## ( cve-id => (date, delay, score1, score2, score3))
  273. def updateCVETables(myid, dsatable, state, src2dsa, dsa2cve, cvetable, client):
  274. logging.info('Updating vulnerability database with advisory ' + state['vendor'] + str(myid) + ' \n')
  275. adv = dsatable[myid]
  276. dsastats = parseAdvisory(adv)
  277. if dsastats == []:
  278. return
  279. dsastats = fixAdvisoryQuirks(myid, state, dsastats)
  280. for srcpkg in dsastats[0]:
  281. if srcpkg in src2dsa:
  282. src2dsa[srcpkg].append(myid)
  283. else:
  284. src2dsa[srcpkg] = []
  285. src2dsa[srcpkg].append(myid)
  286. dsa2cve[str(myid)] = dsastats[2]
  287. for cve_id in dsastats[2]:
  288. # No fetch CVE We use mongodb and cve-search
  289. cve = cv.fetchCVE(cve_id, client)
  290. cvestats = cv.parseCVE(cve_id, cve)
  291. # print(cvestats)
  292. # print(dsastats)
  293. finaldate = cvestats[0]
  294. if cvestats[0] > dsastats[1] or cvestats[0] == 0:
  295. finaldate = dsastats[1]
  296. cvedata = (finaldate, dsastats[1]-finaldate, cvestats[1], cvestats[2], cvestats[3])
  297. ## print(cvedata)
  298. cvetable[cve_id] = cvedata
  299. return cvetable
  300. ###############################################################################
  301. ## Check for updates on Package information
  302. def aptsec_update(state, config, dsatable, client, src2dsa, dsa2cve, src2month, cvetable):
  303. args = sys.argv
  304. # if not('--offline' in args):
  305. # fetchMeta('Packages')
  306. # fetchMeta('Sources')
  307. # fetchMeta('Sha1Sums')
  308. now = datetime.datetime.now()
  309. if not('--cves' in args):
  310. parsePackages('Packages')
  311. parseSources('Sources')
  312. # if not('--nosha1' in args):
  313. # parseSha1sums('Sha1Sums')
  314. if state['vendor'] == 'debian':
  315. newAdv = da.checkDSAs(state, config)
  316. else:
  317. print('Unsupported distribution. We only support Debian at the moment')
  318. system.exit(1)
  319. for myid in newAdv:
  320. if myid in dsatable:
  321. logging.info(state['vendor'] + ' advisory ' + myid + ' already known.\n')
  322. elif '--cves' in args:
  323. ## scan for CVE urls only?
  324. printCVEs(myid, newAdv[myid])
  325. else:
  326. ## store advisory and parse it
  327. dsatable[myid] = newAdv[myid]
  328. updateCVETables(myid, dsatable, state, src2dsa, dsa2cve, cvetable, client)
  329. # recompute all pkg statistics
  330. for srcpkg in src2dsa:
  331. processCVEs(srcpkg, now, src2dsa, dsa2cve, src2month, cvetable, config)
  332. return 0
  333. ###############################################################################
  334. ## find list of src pkgs from bin pkgs based on pkg2src
  335. def resolvePkg2Src(pkglist, pkg2src):
  336. srclist = []
  337. for pkg in pkglist:
  338. if pkg in pkg2src:
  339. srcpkg = pkg2src[pkg]
  340. srclist.append(srcpkg)
  341. else:
  342. logging.info('Could not find source package for: ' + pkg + ' .\n')
  343. return srclist
  344. ###############################################################################
  345. ## compute and store MTBF, MTBR and Scores of each src pkg
  346. ## output: %src2mtbf:
  347. ## (srcpkg=> ())
  348. def processCVEs(pkg, now, src2dsa, dsa2cve, src2month, cvetable, config):
  349. stats = [now, 0, 0, 0, 0, 0, 0]
  350. mylambda = config['TRUST']['lambda']
  351. cvestats = dict()
  352. logging.info('Processing package: ' + pkg + '.\n')
  353. # print(dsa2cve)
  354. ## @cvestats = (date base-score impact-score exploit-score)
  355. for dsa_id in src2dsa[pkg]:
  356. try:
  357. for cve_id in dsa2cve[dsa_id]:
  358. tt = cvetable[cve_id][0]
  359. if tt in cvestats:
  360. cvestats[cvetable[cve_id][0]] += 1
  361. else:
  362. cvestats[cvetable[cve_id][0]] = 1
  363. stats[1] += 1
  364. except KeyError:
  365. for cve_id in dsa2cve[str(dsa_id)]:
  366. tt = cvetable[cve_id][0]
  367. if tt in cvestats:
  368. cvestats[cvetable[cve_id][0]] += 1
  369. else:
  370. cvestats[cvetable[cve_id][0]] = 1
  371. stats[1] += 1
  372. # Ignore pkgs with less than one incident, should not happen..
  373. if stats[1] < 1:
  374. return
  375. prev_date = 0
  376. weight = 0
  377. dates = sorted(cvestats, key = cvestats.get)
  378. stats[0] = dates[0]
  379. count = sum(cvestats.values())
  380. print(pkg + ' ' + str(count))
  381. # if pkg == 'chromium-browser':
  382. # print(src2dsa[pkg])
  383. # pkg_plot(pkg, cvestats)
  384. format_data(pkg, cvestats, src2month)
  385. ##TODO Code to compute trust goes here
  386. ###############################################################################
  387. ## format vulnerability data into monthly intervals, suitable for tensorflow
  388. def format_data(pkg, cvestats, src2month):
  389. x = []
  390. y = []
  391. monthyear = []
  392. year = []
  393. items=list(cvestats.items())
  394. items.sort(key=lambda tup: tup[0])
  395. for data_dict in items:
  396. x.append(parser.parse(data_dict[0]))
  397. y.append(int(data_dict[1]))
  398. for i in range(2000, 2017):
  399. temp = []
  400. for j in range(12):
  401. temp.append(0)
  402. monthyear.append(temp)
  403. for i in range(len(x)):
  404. monthyear[x[i].year-2000][x[i].month-1] += y[i]
  405. months_list = [item for sublist in monthyear for item in sublist]
  406. temp_months = np.zeros(len(months_list))
  407. i = 0
  408. for element in months_list:
  409. temp_months[i] = np.float32(element)
  410. i += 1
  411. src2month[pkg] = temp_months
  412. return
  413. ###############################################################################
  414. ## plot vulnerability time distribution for a single package
  415. def pkg_plot(pkg, cvestats):
  416. colors = list("rgbcmyk")
  417. items = list(cvestats.items())
  418. #print(items)
  419. items.sort(key=lambda tup: tup[0])
  420. x = []
  421. y = []
  422. for data_dict in items:
  423. x.append(parser.parse(data_dict[0]))
  424. y.append(data_dict[1])
  425. monthyear = []
  426. year = []
  427. # initialize list
  428. for i in range(2000,2017):
  429. temp = []
  430. for j in range(12):
  431. temp.append(0)
  432. monthyear.append(temp)
  433. for i in range(len(x)):
  434. # print(str(x[i].year) + str(x[i].month))
  435. monthyear[x[i].year-2000][x[i].month-1] += y[i]
  436. newx = []
  437. yearsx = []
  438. year = []
  439. monthlabel = []
  440. month = []
  441. m1 = 0
  442. m2 = 0
  443. k = 0
  444. label_months = []
  445. months_list = [item for sublist in monthyear for item in sublist]
  446. for i in range(len(months_list)):
  447. label_months.append(i)
  448. plt.bar(label_months, months_list)
  449. for i in range(len(monthyear)):
  450. year.append(0)
  451. cc = 0
  452. for j in range(len(monthyear[i])):
  453. cc += monthyear[i][j]
  454. if j == 5:
  455. m1 = cc
  456. month.append(m1)
  457. if j == 11:
  458. month.append(cc - m1)
  459. k += 1
  460. year[i] = cc
  461. for i in range(len(year)):
  462. yearsx.append(i + 2000)
  463. k = 2000
  464. datapoints = []
  465. for i in range(len(month)):
  466. datapoints.append(i+1)
  467. if i%2 == 0:
  468. monthlabel.append(str(k) + '-1')
  469. else:
  470. monthlabel.append('-2')
  471. k += 1
  472. # plt.xticks(datapoints, monthlabel)
  473. print(year)
  474. # plt.plot.hist(yearsx,year)
  475. # plt.bar(yearsx, year, 1, color='blue')
  476. # plt.bar(datapoints, month, 1, color='blue')
  477. # ts.predict(month)
  478. plt.legend([pkg], loc='upper left')
  479. plt.show()
  480. return 0
  481. ###############################################################################
  482. ## print some meta-info on internal data
  483. def aptsec_about(dsatable, cvetable, pkg2src, src2dsa):
  484. num_dsa = len(dsatable)
  485. num_cve = len(cvetable)
  486. num_pkg = len(pkg2src)
  487. num_src = len(src2dsa)
  488. print('\nThe current database records %d binary packages and %d DSAs.\n', num_pkg, num_src)
  489. print('%d CVEs are associated with %d source packages.\n', num_cve, num_src)
  490. ###############################################################################
  491. ## use scores to suggest alternative packages
  492. def aptsec_alternatives(pkg):
  493. pass
  494. ###############################################################################
  495. ## print overview for pkg high scores
  496. def aptsec_hitlist():
  497. pass
  498. ###############################################################################
  499. ## evaluation helper
  500. ## compute stats until date given in $2, then compute stats
  501. ## for the next year to check accuracy of the prediction.
  502. ## @cvestats = (date base-score impact-score exploit-score)
  503. def simulate_stats(pkg, year):
  504. pass
  505. ###############################################################################
  506. ##TODO Printing functions
  507. ###############################################################################
  508. ## show info on a single src pkg, resolv to src if needed
  509. def aptsec_show(pkg, state, pkg2src, src2dsa, src2mtbf, cvetable):
  510. if state['vendor'] == 'debian':
  511. ADV = 'DSA-'
  512. else:
  513. print('Unsupported distribution. We only support Debian at the moment')
  514. system.exit(1)
  515. if (not(pkg in src2dsa)) and (pkg in pkg2src):
  516. print('\nResolving ' + pkg + ' to ' + pkg2src[pkg] + '\n')
  517. pkg = pkg2src[pkg]
  518. print('\nThe following binary packages are created from ' + pkg + ' :\n\n')
  519. lines = 0
  520. for i in pkg2src:
  521. if pkg2src[i] == pkg:
  522. print(i + '\n')
  523. lines += 1
  524. if lines < 1:
  525. print('-\n')
  526. if not (pkg in src2dsa and pkg in src2mtbf):
  527. print('\nNo vulnerabilities recorded for source package ' + pkg + '.\n')
  528. return
  529. print('\nAdvisories on package ' + pkg + ':\n\n')
  530. for dsa_id in sorted(src2dsa[pkg], key = src2dsa[pkg].get):
  531. print(ADV + dsa_id + '\n')
  532. for cve_id in dsa2cve[dsa_id]:
  533. (sec, minut, hrs, day, mon, yr) = gmtime(cvetable[cve_id][0])
  534. print('%s: Base Score: %04.1f, %02d.%02d.%04d\n', cve_id, cvetable[cve_id][2], day, mon+1, yr+1900)
  535. stats = src2mtbf[pkg]
  536. (sec, minut, hrs, day, mon, yr) = gmtime(stats[0])
  537. print('Now we print various iformation \n')
  538. ###############################################################################
  539. ## print help text
  540. def aptsec_help():
  541. print('See manual for correct usage\n')
  542. ###############################################################################
  543. ## Print system status report from component(files) measurements (sha1sums)
  544. ## Expected input format is Linux IMA. We assume input was validated.
  545. ##
  546. ## Note: aptsec_status(), considers *reportedly installed* packages, while this
  547. ## one looks at *actually loaded* software that influenced the CPU since bootup.
  548. def aptsec_attest(sha1file):
  549. pass
  550. ## Main Program starts here!!
  551. try:
  552. action = sys.argv[1]
  553. except IndexError:
  554. # print('No argument given')
  555. # aptsec_help()
  556. # sys.exit(0)
  557. action = ''
  558. client = MongoClient()
  559. dsatable = dict()
  560. cve_db = client.cvedb
  561. src2dsa = dict()
  562. dsa2cve = dict()
  563. cvetable = dict()
  564. src2month = dict()
  565. (state, err) = load_state()
  566. state['vendor'] = 'debian'
  567. #detect_distribution()
  568. #d = state['cache_dir']
  569. #if not os.path.exists(d):
  570. # os.makedirs(d)
  571. if action == 'update':
  572. (dsatable, src2dsa, dsa2cve, cvetable) = load_DBs()
  573. # loadsha1lists()
  574. aptsec_update(state,config, dsatable, client, src2dsa, dsa2cve, src2month, cvetable)
  575. # save_sha1lists()
  576. save_DBs(dsatable, src2dsa, dsa2cve, cvetable)
  577. save_state(state)
  578. ml.predict(src2month)
  579. elif action == 'status':
  580. load_DBs or exit(1)
  581. #handle errors more gracefully
  582. aptsec_status(sys.argv[2])
  583. elif action == 'show':
  584. load_DBs or exit(1)
  585. #handle errors more gracefully
  586. aptsec_show(sys.argv[2])
  587. else:
  588. aptsec_help()
  589. #print(state)
  590. save_state(state)
  591. #cve_db = client.cvedb
  592. #collection = db.cves
  593. #testcvss = collection.find_one({"cvss": 9.3})
  594. #print(testcvssi