-
Notifications
You must be signed in to change notification settings - Fork 5
/
youtubeblock.py
191 lines (172 loc) · 6.17 KB
/
youtubeblock.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
189
190
191
#!/bin/env/python
import os
import re
try:
import pexpect as p
except ImportError:
print("[+] Pexpect not installed. Please run: 'pip install pexpect' in command line.")
exit()
raw_addlist = "youtube_raw_addlist.log"
pihole_log = "/var/log/pihole.log"
all_queries = "all_queries.log"
block_list = "blocklist.txt"
black_list = "/etc/pihole/blacklist.txt"
prefix = []
# Regex used to match relevant loglines (in this case, to create an part of an IP like r1---sn- etc.)
for i in range(1, 21):
line = ("r"+str(i)+"---sn-")
prefix.append(line)
class colors:
"""
Class specifically for message fuction below.
Contains colors and fonts.
"""
UNDERLINE = '\033[4m'
RED = '\033[91m'
GREEN = '\033[92m'
BOLD = '\033[1m'
END = '\033[0m'
def message(state, msg):
"""
Function that creates print statements with fonts and colors.
"""
if state == "underline":
print(colors.UNDERLINE + msg + colors.END)
elif state == "green":
print(colors.GREEN + msg + colors.END)
elif state == "red":
print(colors.RED + msg + colors.END)
elif state == "bold":
print(colors.BOLD + msg + colors.END)
else:
print("Wrong state. Please try: 'underline', 'green', 'red' or 'bold'")
def text():
message("red", """
Yb dP .d88b. 8 8 88888 8 8 888b. 8888
YbdP 8P Y8 8 8 8 8 8 8wwwP 8www
YP 8b d8 8b..d8 8 8b..d8 8 b 8
88 `Y88P' `Y88P' 8 `Y88P' 888P' 8888
8 8 8 8 w w
88b. 8 .d8b. .d8b 8.dP 8 w d88b w8ww
8 8 8 8' .8 8 88b 8 8 `Yb. 8
88P' 8 `Y8P' `Y8P 8 Yb 8 8 Y88P Y8P
""")
message("underline", "Pihole unique YouTube advertisement url checker")
message("underline", "Created by eLVee")
message("underline", "Version 1.1")
print("")
def check_raw():
"""
Checks the pihole logs for the prefix var above.
Saves the raw data that needs to be sorting to 'youtube_raw_addlist.log'
"""
output_filename = os.path.normpath(raw_addlist)
message("green", "[+] Checking for youtube fingerprint..")
for url in prefix:
line_regex = re.compile(r".*"+url+".*$")
with open(output_filename, "a+") as out_file:
with open(pihole_log, "r") as in_file:
for line in in_file:
if (line_regex.search(line)) or ".sn-" in line:
#print line
out_file.write(line)
else:
#print(url+" not found trying again")
continue
in_file.close()
out_file.close()
def query_list():
"""
Checks 'youtube_raw_addlist.log' for the word 'query', so we know for sure it's outgoing.
Then saves that data to 'all_queries.log' for further sorting.
"""
output_filename = os.path.normpath(all_queries)
line_regex = re.compile(r".*"+"query"+".*$")
with open(output_filename, "a+") as out_file:
with open(raw_addlist, "r") as in_file:
for line in in_file:
if (line_regex.search(line)) and "googlevideo.com" in line:
if "5hne" in line:
#print("'5hne' is found in url. Probably a video. Skipping")
pass
if "5hnl" in line:
#print("'5hne' is found in url. Probably a video. Skipping")
pass
else:
thisline = line.split("A] ")
thisline = thisline[1].split(" from")
#print(thisline[0])
out_file.write(thisline[0]+"\n")
else:
#print(url+" not found trying again")
continue
in_file.close()
out_file.close()
def blocklist():
"""
Check the the file 'all_queries.log' for unique urls and sorts them.
Then check the blocklist.txt if the urls is already present. If not, adds it.
"""
uniquelines = set(open(all_queries).readlines())
for line in uniquelines:
with open(block_list, "r+") as in_file:
if line in in_file:
#print("> "+line+" Already in 'blocklist.txt'.")
pass
else:
with open(block_list, "a") as out_file:
#print(line+"\n")
out_file.write(line)
out_file.close()
in_file.close()
def add_to_pihole():
"""
Uploads the contents of 'blocklist.txt' to the pihole with the command pihole -b
"""
urls = []
with open(block_list, 'r') as in_file:
for line in in_file:
with open(black_list, "r") as in_file2:
if line in in_file2:
pass
else:
#print(line)
urls.append(line.rstrip()+" ")
all_urls = "".join(urls)
#print("[+] Adding "+line.rstrip()+" "+" to pihole.")
if all_urls:
command = p.spawnu("pihole -b "+all_urls)
command.interact()
#print("[+] Done.")
command.close()
command1 = p.spawnu("pihole restartdns")
command1.interact()
command1.close()
else:
message("bold", "[+] No urls to add.")
in_file.close()
def delete_logs():
"""
Delete the files
- youtube_raw_addlist.log
- all_queries.log
to keep it nice and clean.
"""
command = p.spawnu("rm -rf youtube_raw_addlist.log all_queries.log")
command.interact()
command.close()
text()
message("green", "[+] Checking all dns requests for YouTube queries.")
check_raw()
message("bold", "[+] Done checking requests.")
message("green", "[+] Sorting urls..")
query_list()
message("bold", "[+] Done sorting urls for queries.")
message("green", "[+] Sorting for unique urls and adding to list 'blocklist.txt'..")
blocklist()
message("bold", "[+] Done adding to blocklist.")
message("green", "[+] Adding contents of blocklist to pihole.")
add_to_pihole()
message("bold", "[+] Done adding to pihole.")
message("bold", "[+] Finished.")
delete_logs()