-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeeder.py
94 lines (67 loc) · 2.31 KB
/
feeder.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
import socket
import scapy.all as scapy
from redis import StrictRedis
import os
# Redis informations
REDIS_IP = os.environ.get("REDIS_IP", None)
REDIS_PORT = os.environ.get("REDIS_PORT", None)
REDIS_PASSWORD = os.environ.get("REDIS_PASSWORD", None)
REDIS_EXPIRATION = os.environ.get("REDIS_EXPIRATION", None)
def arp_scan(subnet):
"""
Scans the network by sending ARP packets on the given subnet
and returns a list of dictionnaries with the IPs, MACs and hostnames
"""
answered = scapy.arping(subnet)[0]
machines = []
for i in answered:
ip, mac = i[1].psrc, i[1].hwsrc
try:
host = socket.gethostbyaddr(i[1].psrc)[0]
except Exception:
host = "??"
machines.append({"ip": ip, "mac": mac, "host": host})
return machines
def strip_suffix(s, suf):
if s.endswith(suf):
return s[: -len(suf)]
return s
def strip_prefix(s, pre):
if s.startswith(pre):
return s[len(pre) :]
return s
def is_host(host):
return not (len(host) < 3 or "?" in host)
def send_mac(client, maclist):
payload = ",".join(maclist)
client.setex("incubator_pamela", REDIS_EXPIRATION, payload)
def send_hostnames(client, hosts_dict={}):
for key, value in hosts_dict.items():
client.hset("incubator_pamela_hostnames", key, value)
def format_host(host):
"""
Strip know prefixes and suffixes
"""
host = strip_suffix(host, ".lan.urlab.be")
host = strip_suffix(host, ".lan")
host = strip_suffix(host, ".local")
host = strip_suffix(host, "iPodtouch")
host = strip_suffix(host, "-PC")
host = strip_suffix(host, "-pc")
host = strip_prefix(host, "pc-")
host = strip_prefix(host, "PC-")
host = strip_prefix(host, "DESKTOP-")
host = strip_prefix(host, "LAPTOP-")
host = strip_prefix(host, "iPod-de-")
host = strip_prefix(host, "iPadde")
return host
if __name__ == "__main__":
client = StrictRedis(REDIS_IP, REDIS_PORT, password=REDIS_PASSWORD)
client.set("incubator_pamela_expiration", REDIS_EXPIRATION)
macdicts = arp_scan("192.168.1.0/24")
maclist = list({x["mac"] for x in macdicts})
send_mac(client, maclist)
hostnames = {
x["mac"]: format_host(x["host"]) for x in macdicts if is_host(x["host"])
}
send_hostnames(client, hostnames)