-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetection.py
More file actions
189 lines (149 loc) · 6.94 KB
/
Copy pathdetection.py
File metadata and controls
189 lines (149 loc) · 6.94 KB
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
"""
TransitMeasure
Copyright (C) 2024 Atheesh Thirumalairajan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import re
import ipaddress
import nltk
import requests
import dns.resolver
from cli import debug
from easynmt import EasyNMT
from langdetect import detect
from bs4 import BeautifulSoup
# Initialize EasyNMT model for translation
nltk.download('punkt_tab')
model = EasyNMT('opus-mt')
# Confidence Scores: Should Add up to 1
CONFIDENCE_SCORE_IPCHECK_WEIGHT = 0.0
CONFIDENCE_SCORE_BLKPAGE_WEIGHT = 1.0
# Base Censorship Phrases
BASE_CENSOR_PHRASES = [
# Should be transit country specific, future impl.
r"gov.ru",
r"Russian Federation",
r"Belgian Legislation",
# Other Common Phrases
r"restricted",
r"denied",
r"blocked",
r"illegal"
]
def nslookup_v4(hostname):
# Check if hostname is an IP Address
try:
ipaddress.ip_address(hostname)
return hostname
# If not an IP address, proceed with DNS resolution
except ValueError:
try:
# Query for A records (IPv4 addresses)
answers = dns.resolver.resolve(hostname, 'A')
return answers[0].to_text()
except Exception as e:
return "0.0.0.0"
def geolocate_ipv4(address):
if not address:
raise "Invalid IP Address"
# Using the ip2c.org API (Change Backend, if needed)
response = requests.get(f'https://ip2c.org/{address}')
response.raise_for_status()
# Parse Result. Sample Format: 1;RU;RUS;Russian Federation (the)
geo_result = response.text.split(';')
country_code = geo_result[1]
return country_code
def detect_transit_censorship(raw_measurements, httpOnly=True):
# Statistics
tc_confidence_score = 0
total_measurements = 0
transit_censored = 0
for raw_measurement in raw_measurements:
# Test Parameters
destination = raw_measurement['input']
report_id = raw_measurement['report_id']
origin_country = raw_measurement['probe_cc']
measurement_requests = raw_measurement['test_keys']['requests']
for req in measurement_requests:
try:
# Apply TCP Transport Filter
if req['request']['x_transport'] != 'tcp':
continue
# Apply HTTP Only Filters
if httpOnly and (not destination.startswith("http://")):
continue
# We're using this measurement
total_measurements += 1
# DNS and IP Checks First
request_ip = req['request']['headers'].get('Host', '')
response_ip = req['response']['headers'].get('Location', '')
serverhost_country = geolocate_ipv4(nslookup_v4(request_ip))
actualresponse_country = geolocate_ipv4(nslookup_v4(response_ip))
# Transit Detected State
transit_detected = False
# Check if redirection IPs belong to a different country than origin_country
if serverhost_country != actualresponse_country:
transit_detected = True
if CONFIDENCE_SCORE_IPCHECK_WEIGHT != 0:
transit_censored += 1
tc_confidence_score += CONFIDENCE_SCORE_IPCHECK_WEIGHT
# Log the Results, if needed
debug(
f"Potential Transit Tampering: {origin_country} -> {serverhost_country} | {actualresponse_country} -> {origin_country} " +
f"instead of {origin_country} -> {serverhost_country} -> {origin_country}"
)
# If we got a censorship hint from the previous step, Prove it!
if transit_detected:
# Extract response body and parse HTML
response_body = req['response'].get('body', '')
soup = BeautifulSoup(response_body, 'html.parser', from_encoding='utf-8')
# Extract text from paragraph tags
pagebody_text_raw = soup.get_text()
pagebody_text = ' '.join(pagebody_text_raw.split()) # Remove Extra Spaces
# Translate each paragraph text up to 400 characters into English
translated_text = pagebody_text # Default is English
if len(pagebody_text) > 2000:
pagebody_text = pagebody_text[:2000] # Limit to 2000 characters
# Detect language of the text, Translate if not English
if len(pagebody_text) > 50:
detected_lang = detect(pagebody_text)
if detected_lang != 'en':
translated_text = model.translate(
pagebody_text,
source_lang=detected_lang,
target_lang='en'
)
# Log Translated text for debugging, Improving Base Phrases
debug(f"Translated Text: [{destination}] {translated_text}", level=3)
matches = [phrase for phrase in BASE_CENSOR_PHRASES if
re.search(phrase, translated_text, re.IGNORECASE)]
# If matches are found, Update Blockpage Scores
if len(matches) > 0:
debug(f"Blockpage Parsing Detected Phrases: {', '.join(matches)}\n")
tc_confidence_score += (len(matches) / len(
BASE_CENSOR_PHRASES)) * CONFIDENCE_SCORE_BLKPAGE_WEIGHT
if CONFIDENCE_SCORE_IPCHECK_WEIGHT == 0:
transit_censored += 1
except:
print(f"Parse Exception: {destination} [{report_id}]")
if total_measurements < 1:
print("No Measurements matched Applied Filters")
else:
# Output Censored Request Count
print(
f"Transit Censored Requests: {transit_censored}/{total_measurements} " +
f"({(transit_censored / total_measurements) * 100}%)"
)
# Output Confidence Score
print(f"Confidence Score: {tc_confidence_score}/{transit_censored} ({(tc_confidence_score / transit_censored) * 100}%)")
# Return a JSON-like object lol
return {total_measurements, transit_censored, tc_confidence_score}