-
Notifications
You must be signed in to change notification settings - Fork 81
/
lazyScanner.py
executable file
·95 lines (80 loc) · 3.11 KB
/
lazyScanner.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'videns'
try:
import urllib.request as urllib2
except ImportError:
import urllib2
try:
from subprocess import DEVNULL # py3k
except ImportError:
import os
DEVNULL = open(os.devnull, 'wb')
import json
import re
import subprocess
VULNERS_LINKS = {'pkgChecker':'https://vulners.com/api/v3/audit/audit/',
'bulletin':'https://vulners.com/api/v3/search/id/?id=%s'}
class LazyScanner():
def __init__(self):
pass
def sshCommand(self,cmd):
cmdResult = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=DEVNULL, shell=True).communicate()[0]
if isinstance(cmdResult, bytes):
cmdResult = cmdResult.decode('utf8')
return cmdResult
def getOSInfo(self):
version = self.sshCommand("cat /etc/os-release")
if version:
reFamily = re.search("^ID=\"?(\w+)\"?",version,re.MULTILINE)
if reFamily:
osFamily = reFamily.group(1).lower()
else:
return
reVersion = re.search("^VERSION_ID=\"?(\w+)\"?",version,re.MULTILINE)
if reVersion:
osVersion = reVersion.group(1).lower()
else:
return
return (osFamily, osVersion)
def getPackages(self, osName):
if osName in ('debian','ubuntu', 'kali'):
cmd = "dpkg-query -W -f='${Package} ${Version} ${Architecture}\n'"
elif osName in ('rhel', 'centos', 'oraclelinux', 'suse', 'fedora'):
cmd = "rpm -qa"
else:
cmd = None
return self.sshCommand(cmd).splitlines() if cmd else None
def auditSystem(self):
osInfo = self.getOSInfo()
if not osInfo:
print("Can't detect OS, try linuxScanner.py instead")
return
print("OS Name - %s, OS Version - %s" % (osInfo[0], osInfo[1]))
installedPackages = self.getPackages(osInfo[0])
if not installedPackages:
print("Couldn't find packages")
return
print("Total provided packages: %s" % len(installedPackages))
# Get vulnerability information
payload = {'os':osInfo[0],
'version':osInfo[1],
'package':installedPackages}
req = urllib2.Request(VULNERS_LINKS.get('pkgChecker'))
req.add_header('Content-Type', 'application/json')
req.add_header('User-Agent', 'vulners-lazy-scanner-v0.1')
response = urllib2.urlopen(req, json.dumps(payload).encode('utf-8'))
responseData = response.read()
if isinstance(responseData, bytes):
responseData = responseData.decode('utf8')
responseData = json.loads(responseData)
resultCode = responseData.get("result")
if resultCode == "OK":
print(json.dumps(responseData, indent=4))
print("Vulnerabilities:\n%s" % "\n".join(responseData.get('data').get('vulnerabilities')))
else:
print("Error - %s" % responseData.get('data').get('error'))
return
if __name__ == "__main__":
scanner = LazyScanner()
scanner.auditSystem()