-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanDevicesDaemon.py
188 lines (135 loc) · 5.3 KB
/
scanDevicesDaemon.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
import requests
import threading
import subprocess
import os
import xml.etree.ElementTree as ET
from audit.objects.Device import Device
from audit.objects.Service import Service
# Temporary files for nmap
outputTempTcp = "/PASTA-Box/audit/temp/output_tcp.xml"
outputTempUdp = "/PASTA-Box/audit/temp/output_udp.xml"
# Get all the active devices from the network
def getActiveDevices():
data = requests.get('http://localhost/api/devices/mapDevices')
return data.json()
# Return protocol integer value depending of the service protocol
def protocolMode(protocol):
if(protocol.lower() == "tcp"):
return 1
if(protocol.lower() == "udp"):
return 2
if(protocol.lower() == "icmp"):
return 3
return 0
# Scan devices on the network with the rights parameters (TCP or UDP)
def scanDevices(ip, mode):
if mode == 1: # TCP and OS/services discovery
nmapCmd = ['sudo', 'nmap', ip, '-p-', '-sS',
'-sV', '-O', '-T5', '-oX', outputTempTcp]
else: # UDP and services discovery
nmapCmd = ['sudo', 'nmap', ip, '-p-', '-sU',
'-sV', '-T5', '-oX', outputTempUdp]
with open(os.devnull, 'wb') as devnull:
subprocess.check_call(nmapCmd, stdout=devnull, stderr=devnull)
# Parse Nmap XML output
def parseNmap(mode, ip):
if(mode == 1):
xmlFile = outputTempTcp
else:
xmlFile = outputTempUdp
xmlData = ET.parse(xmlFile).getroot()
host = xmlData.find('host')
if(host == None):
return None
services = []
ports = host.find('ports')
if(ports != None): # Create of potential services objects in services array
for port in ports.findall('port'):
if(port.find('state').attrib.get('state') == "open"):
portAttributes = port.attrib
proto = portAttributes.get('protocol')
serviceHost = port.find('service')
if(serviceHost.attrib.get('product') == None or serviceHost.attrib.get('product') == ""):
serviceName = serviceHost.attrib.get('name')
else:
serviceName = serviceHost.attrib.get('product')
service = Service(proto, serviceName, serviceHost.attrib.get(
'version'), port.attrib.get('portid'))
services.append(service)
for address in host.findall('address'):
if((address.attrib.get('addrtype') == "ipv4" or address.attrib.get('addrtype') == "ipv6") and address.attrib.get('addr') == ip):
ipAddr = ip
if(address.attrib.get('addrtype') == "mac"):
macAddr = address.attrib.get('addr')
hostnames = host.find('hostnames')
hostname = hostnames.find('hostname')
if(hostname != None):
finalHostname = hostname.attrib.get('name')
else:
finalHostname = ""
osName = ""
if(mode == 1): # We only determine the OS in TCP mode
os = host.find('os')
if(os != None):
possibleOs = os.findall('osmatch')
if(len(possibleOs) > 1):
for i in range(len(possibleOs)):
if(possibleOs[i].attrib.get('name').find('Linux') != -1):
osName = "Linux"
if(possibleOs[i].attrib.get('name').find('Windows') != -1):
osName = "Windows"
if(len(possibleOs) == 1):
osName = possibleOs[0].attrib.get('name')
return Device(finalHostname, osName, ipAddr, macAddr, services)
# Delete temporary XML files in temp folder
def deleteTempFile(mode):
if(mode == 1):
if os.path.exists(outputTempTcp):
os.remove(outputTempTcp)
else:
if os.path.exists(outputTempUdp):
os.remove(outputTempUdp)
# Update device with Nmap info
def insertDevice(newDevice: Device, mode):
addrParams = {
'ipAddr': newDevice.ipAddr,
'macAddr': newDevice.macAddr
}
data = requests.get('http://localhost/api/devices', params=addrParams)
deviceBDD = data.json()
if(mode == 1):
deviceToInsert = {
"netBios": newDevice.netBios,
"systemOS": newDevice.systemOS
}
r = requests.put('http://localhost/api/devices/' +
str(deviceBDD["id"]), json=deviceToInsert)
return deviceBDD["id"]
# Update device with Nmap info
def insertService(newDevice: Device):
for service in newDevice.services:
serviceBDD = {
"idDevice": newDevice.id,
"numberPort": int(service.number),
"type": int(protocolMode(service.proto)),
"serviceName": service.name,
"serviceVersion": service.version
}
r = requests.post('http://localhost/api/services', json=serviceBDD)
# Main function of the service
def main(nodes, mode):
for i in range(len(nodes)):
scanDevices(nodes[i]['ipAddr'], mode)
newDevice = parseNmap(mode, nodes[i]['ipAddr'])
if(newDevice == None): # No insert in BDD
continue
else:
deviceID = insertDevice(newDevice, mode)
newDevice.updateID(deviceID)
if(len(newDevice.services) != 0):
insertService(newDevice)
deleteTempFile(mode)
nodes = getActiveDevices()
t = threading.Thread(target=main, args=(nodes, 2,))
t.start()
main(nodes, 1)