-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
131 lines (104 loc) · 4 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
import os
import nmap
import sys
import re
import subprocess
import argparse
network = "192.168.0.0/24"
logfile = "log/pihole.log"
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
"""
https://serverfault.com/a/665337
nmap -sn 192.168.0.0/24
-sn No port scan
-n No DNS resolution
-T5 Faster execution
Example: nmap -sn -T5 -n --min-parallelism 100 192.168.0.0/24
"""
def ping_sweep():
hosts = {}
nm = nmap.PortScanner()
nm.scan(hosts=network, arguments='-sn -T5')
zaehler = 1
for x in nm.all_hosts():
hosts[zaehler] = x + " " + nm[x].hostname()
zaehler += 1
# TODO: Loop
for key, value in hosts.items():
print(key, ' : ', value)
try:
host_number = int(input("\nNumber of host: "))
except ValueError:
sys.exit("Ungueltige Eingabe")
# https://www.geeksforgeeks.org/extract-ip-address-from-file-using-python/
return re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})').match(hosts[host_number]).group(1)
def tail_blocked(ip):
match = [ip, 'query[A]']
blocked = ['blacklisted', 'blocked']
f = subprocess.Popen(['tail', '-f', logfile], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
line1 = f.stdout.readline().decode("utf-8")
if all(c in line1 for c in match):
line2 = f.stdout.readline().decode("utf-8")
if any(c in line2 for c in blocked):
start = 'query[A]'
end = 'from'
print(bcolors.FAIL + line1[0:15] + ':' + line1[line1.find(start) + len(start):line1.rfind(
end)] + bcolors.ENDC)
def tail_unblocked(ip):
match = [ip, 'query[A]']
allowed = ['forwarded', 'cached']
f = subprocess.Popen(['tail', '-f', logfile], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
line1 = f.stdout.readline().decode("utf-8")
if all(c in line1 for c in match):
line2 = f.stdout.readline().decode("utf-8")
if any(c in line2 for c in allowed):
start = 'query[A]'
end = 'from'
print(bcolors.OKGREEN + line1[0:15] + ':' + line1[line1.find(start) + len(start):line1.rfind(
end)] + bcolors.ENDC)
def tail_both(ip):
match = [ip, 'query[A]']
allowed = ['forwarded', 'cached']
blocked = ['blacklisted', 'blocked']
f = subprocess.Popen(['tail', '-f', logfile], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
line1 = f.stdout.readline().decode("utf-8")
if all(c in line1 for c in match):
line2 = f.stdout.readline().decode("utf-8")
if any(c in line2 for c in blocked):
start = 'query[A]'
end = 'from'
print(bcolors.FAIL + line1[0:15] + ':' + line1[line1.find(start) + len(start):line1.rfind(
end)] + bcolors.ENDC)
if any(c in line2 for c in allowed):
start = 'query[A]'
end = 'from'
print(bcolors.OKGREEN + line1[0:15] + ':' + line1[line1.find(start) + len(start):line1.rfind(
end)] + bcolors.ENDC)
def check_access():
if os.access(logfile, os.R_OK) is False:
sys.exit("Kann Logfile nicht öffnen!")
if __name__ == "__main__":
argparser = argparse.ArgumentParser(prog='TailPiHoleClient', description='Display requested domains from one '
'Pi-Hole user')
argparser.add_argument("-u", action='store_true', required=False, help="Only allowed domains")
argparser.add_argument("-b", action='store_true', required=False, help="Only blocked domains")
args = argparser.parse_args()
check_access()
if args.b:
tail_blocked(ping_sweep())
elif args.u:
tail_unblocked(ping_sweep())
else:
tail_both(ping_sweep())