-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCDNX.py
143 lines (132 loc) · 4.83 KB
/
CDNX.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
# /usr/bin/env python
# coding=utf-8
import os
import sys
import socket
import threading
import webbrowser
from Queue import Queue
from httplib import HTTPResponse
from libs.report import *
from libs.log import logInit
from libs.cmdline import get_args
from libs.FakeSocket import FakeSocket
mutex = threading.Lock()
timeout = 10
socket.setdefaulttimeout(timeout)
class CDNX:
def __init__(self):
self.args = get_args()
self.threads = 30
self.log = logInit(log_name="res.log")
self.result = []
self.setTask()
self.saveResult()
def sendData(self, ip):
"""
套接字通信
:param ip:
:return:
"""
addr = (ip, 80)
data = "GET / HTTP/1.1\r\nHost: " + self.args.domain + "\r\n" + "Connection: close" + "\r\n\r\n"
recvdata = ""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
res = {}
res.setdefault("ip", ip)
res.setdefault("status")
res.setdefault("body")
res.setdefault("server_header")
res.setdefault("x_powered_by_header")
res.setdefault("success")
try:
sock.connect(addr)
sock.send(data)
while True:
buffer = sock.recv(1024)
if not buffer:
break
recvdata += buffer
response = HTTPResponse(FakeSocket(recvdata))
response.begin() # begin有什么用???
res["status"] = response.status
if response.status == 200: # 这里就可以保存为html文件了
msg = ip + " seems done!!!"
self.log.info(msg)
res["body"] = response.read()
res["server"] = response.getheader("Server", default="Known")
res["x_powered_by"] = response.getheader("X-Powered-By", default="Known")
if self.args.keyword:
if self.args.keyword in res["body"]:
res["success"] = True
else:
res["success"] = False
else:
res["success"] = True
self.result.append(res)
except Exception as err:
self.log.error(err)
res["success"] = False
def setTask(self):
"""
设置任务队列
:return:
"""
thread_list = []
qsize = (len(self.args.ip)/1024+1)*1024
self.queue = Queue(qsize)
for ip in self.args.ip:
self.queue.put(ip)
for i in range(self.threads):
thread_list.append(threading.Thread(target=self.run))
for t in thread_list:
t.start()
for t in thread_list:
t.join()
def run(self):
while True:
if not self.queue.empty():
ip = self.queue.get()
self.sendData(ip)
else:
break
pass
def saveResult(self):
"""
创建单独的目录,expmaple_com/result.html
将self.result的结果存储进文件保存
:return:
"""
html = html_template.replace("{domain}", self.args.domain)
content = ""
if not self.result:
msg = "No result....Scan Complished"
self.log.warning(msg)
else:
dir = self.args.domain.replace(".", "_")
path = sys.path[0] + "\\report\\" + dir
if not os.path.exists(path):
os.mkdir(path, 0755)
for res in self.result:
if res["success"] and res["status"]:
filename = path + "\\" + res["ip"].replace(".", "_") + ".html"
tmp_content = content_template
tmp_content = tmp_content.replace("{status}", str(res["status"]))
tmp_content = tmp_content.replace("{ip}", res["ip"])
tmp_content = tmp_content.replace("{x_powered_by}", res["x_powered_by"])
tmp_content = tmp_content.replace("{server}", res["server"])
tmp_content = tmp_content.replace("{href}", "file:///" + filename)
content += tmp_content
with open(filename, "w") as f:
f.write(res["body"])
html = html.replace("{content_template}", content)
with open(path + "\\result.html", "w") as f:
f.write(html)
msg = "Scan Complished"
self.log.warning(msg)
msg = "Saved in " + path + "\\result.html"
self.log.warning(msg)
if self.args.browser:
webbrowser.open(path + "\\result.html")
if __name__ == "__main__":
test = CDNX()