检测国内DNS泄露

检测国内DNS泄露Python

新建Python文件

1
dns.py

检测国内DNS泄露Python代码

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
import requests
from bs4 import BeautifulSoup
import time
import os

# 每次执行时删除旧的日志文件
if os.path.exists("refresh_log.txt"):
os.remove("refresh_log.txt")
if os.path.exists("no_keyword_pages_log.txt"):
os.remove("no_keyword_pages_log.txt")

print("\033[34m***神棍局***天威DNS雷达系统\033[0m")

# 检查关键词函数
def check_keyword(content):
keywords = ["正确"]
for keyword in keywords:
if keyword in content:
return True
return False

# 记录日志到文件
def write_log_to_file(total_checks, keyword_detected_count, keyword_not_detected_count, log_details):
with open("refresh_log.txt", "w", encoding="utf-8") as log_file:
log_file.write("====== 检测结果统计 ======\n")
log_file.write(f"总共检测次数: {total_checks}\n")
log_file.write(f"DNS正确的次数: {keyword_detected_count}\n")
log_file.write(f"DNS异常的次数: {keyword_not_detected_count}\n")
log_file.write("\n====== 详细日志 ======\n")
for detail in log_details:
log_file.write(detail + "\n")

# 记录嵌入页面内容到另一个文件,当DNS异常时
def save_page_content(content, index):
with open("no_keyword_pages_log.txt", "a", encoding="utf-8") as page_file:
page_file.write(f"\n\n====== 第 {index+1} 次刷新DNS异常的嵌入页面内容 ======\n")
page_file.write(content)

# 主程序
def main(refresh_count=99): #检测次数
url = "http://nstool.netease.com/"
total_checks = 0
keyword_detected_count = 0
keyword_not_detected_count = 0
log_details = []

for i in range(refresh_count):
try:
total_checks += 1
print(f"第 {i+1} 次刷新:")

response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
response.encoding = 'gbk' # 设置编码
soup = BeautifulSoup(response.text, 'html.parser')

# 查找iframe标签并获取src属性
iframe = soup.find('iframe')
iframe_src = iframe['src']

# 获取嵌入页面内容
iframe_response = requests.get(iframe_src, headers={'User-Agent': 'Mozilla/5.0'})
iframe_response.encoding = 'gbk' # 设置编码
iframe_content = iframe_response.text

# 检查关键词
if check_keyword(iframe_content):
keyword_detected_count += 1
print("DNS正确,继续查询...")
log_details.append(f"第 {i+1} 次:DNS正确")
else:
keyword_not_detected_count += 1
print("DNS异常,继续查询...")
log_details.append(f"第 {i+1} 次:DNS异常")

# 保存没有关键词的嵌入页面内容到日志文件
save_page_content(iframe_content, i)

time.sleep(2) # 等待5秒后再刷新

except Exception as e:
print("发生错误:", e)
log_details.append(f"第 {i+1} 次:发生错误,{e}")
break

# 统计日志
print("\n====== 检测结果统计 ======")
print(f"总共检测次数: {total_checks}")
print(f"DNS正确的次数: {keyword_detected_count}")
print(f"DNS异常的次数: {keyword_not_detected_count}")

# 写入日志到文件
write_log_to_file(total_checks, keyword_detected_count, keyword_not_detected_count, log_details)
print("日志已写入 refresh_log.txt 文件。")

if __name__ == '__main__':
main() # 默认刷新99次

批处理脚本

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
@echo off
chcp 65001

:: 执行dns.py脚本
python dns.py

:: 询问是否查看并删除refresh_log.txt
echo 查看 DNS正确统计记录 吗?(Y/N)
set /p choice=
if /i "%choice%"=="Y" (
type refresh_log.txt
echo 请按任意键继续... & pause
echo 删除 DNS正确统计记录 吗?(Y/N)
set /p choice=
if /i "%choice%"=="Y" (
del refresh_log.txt
echo 文件已删除!
)
)

:: 询问是否查看并删除no_keyword_pages_log.txt
echo 查看 DNS错误记录 吗?(Y/N)
set /p choice=
if /i "%choice%"=="Y" (
type no_keyword_pages_log.txt
echo 请按任意键继续... & pause
echo 删除 DNS错误记录 吗?(Y/N)
set /p choice=
if /i "%choice%"=="Y" (
del no_keyword_pages_log.txt
echo 文件已删除!
)
)