-
Notifications
You must be signed in to change notification settings - Fork 159
/
CTServer.py
213 lines (182 loc) · 8.89 KB
/
CTServer.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#
# CapTipper is a malicious HTTP traffic explorer tool
# By Omri Herscovici <omriher AT gmail.com>
# http://omriher.com
# @omriher
#
# This file is part of CapTipper
#
# CapTipper is a free software under the GPLv3 License
#
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler
from StringIO import StringIO
import datetime
from urlparse import urlparse
from threading import Thread
import CTCore
class HTTPRequest(BaseHTTPRequestHandler):
def __init__(self, request_text):
self.rfile = StringIO(request_text)
self.raw_requestline = self.rfile.readline()
self.error_code = self.error_message = None
self.parse_request()
def send_error(self, code, message):
self.error_code = code
self.error_message = message
class server(Thread):
def __init__(self):
super(server, self).__init__()
self.srv = SocketServer.ThreadingTCPServer((CTCore.HOST, CTCore.PORT), TCPHandler)
def run(self):
print CTCore.newLine + CTCore.colors.GREEN + "[+]" + CTCore.colors.END + " Started Web Server on http://" + CTCore.HOST + ":" + str(CTCore.PORT)
print CTCore.colors.GREEN + "[+]" + CTCore.colors.END + " Listening to requests..." + CTCore.newLine
self.srv.serve_forever()
def shutdown(self):
if (CTCore.web_server_turned_on):
self.srv.shutdown()
self.srv.server_close()
print "WebServer Shutdown."
CTCore.web_server_turned_on = False
class TCPHandler(SocketServer.BaseRequestHandler):
def build_index(self):
index_page = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Index of CapTipper Server</title>
</head>
<body>
<h1>Index of CapTipper Server</h1>
<hr>"""
for host, ip in CTCore.hosts.keys():
index_page += " " + host + " ({})<br>".format(ip)
hostkey = (host, ip)
for host_uri,obj_num in CTCore.hosts[hostkey]:
#chr_num = 195 # Extended ASCII tree symbol
chr_num = 9500 # UNICODE tree symbol
# Checks if last one
if (host_uri == CTCore.hosts[hostkey][len(CTCore.hosts[hostkey]) - 1]):
#chr_num = 192 # Extended ASCII tree symbol
chr_num = 9492 # UNICODE tree symbol
index_page += " " + "-- <a href='/{}".format(host) + host_uri.encode('utf8') \
+ "'>{}</a><br>\r\n".format(host_uri.encode('utf8') + " [{}]".format(obj_num))
index_page += "<br>\r\n"
index_page += "</body></html>"
return index_page
def check_request(self, conv_req, get_uri):
if (get_uri.lower() == conv_req.lower()):
return True
# ignore variables
if (get_uri.find('?') > 0 and conv_req.find('?') > 0) and \
(get_uri.lower()[:get_uri.find('?')] == conv_req.lower()[:conv_req.find('?')]):
return True
else:
return False
def get_clear_uri(self):
try:
get_request = self.data.splitlines()[0]
now_s = datetime.datetime.now()
CTCore.request_logs.append("[" + str(now_s.isoformat()) + "] " + self.client_address[0] + " : " + get_request)
get_start = get_request.find(' ') + 1
get_end = get_request.rfind(' ')
loop_start = get_start + 1
for i in range(loop_start, get_end):
if get_request[i] == '/':
get_start += 1
else:
break
get_uri = get_request[get_start:get_end]
return get_uri
except Exception,e:
print "[-] Error parsing data: " + self.data + ":" + str(e)
def get_domain_folder(self, get_uri):
folder = get_uri.split("/")[1]
return folder
def log(self, uri):
now_s = datetime.datetime.now()
CTCore.request_logs.append("[" + str(now_s.isoformat()) + "] " + self.client_address[0] + " : " + uri)
def handle(self):
try:
self.data = self.request.recv(1024).strip()
request = HTTPRequest(self.data)
if self.data != "":
host_folder = self.get_domain_folder(request.path)
using_host_folder = False
using_host_header = False
for chost,ip_port in CTCore.hosts.keys():
if chost.lower() == host_folder.lower():
req_host = chost
using_host_folder = True
break
if not using_host_folder:
req_host = request.headers['host']
#check if host header is in domains list
for chost, ip_port in CTCore.hosts.keys():
if chost.lower() == req_host.lower():
req_host = chost
using_host_header = True
break
if not using_host_header:
if req_host.split(":")[0] == "127.0.0.1":
localhost = "http://127.0.0.1/"
try:
# set req_host to be referer
if request.headers.has_key('referer'):
referrer = request.headers['referer']
else:
referrer = ""
# if referer isn't 127.0.0.1
if referrer.find(localhost) == 0:
end_of_host = referrer.find("/",len(localhost) + 1)
req_host = referrer[len(localhost):end_of_host]
except:
pass
# set req_host to be the last request
if (len(CTCore.request_logs) > 0) and (request.path.find(req_host) == 1 or req_host.split(":")[0] == "127.0.0.1"):
last_req = CTCore.request_logs[-1]
last_url = last_req[last_req.find(' : ') + 3:]
last_req_parsed = urlparse("http://" + last_url)
req_host = last_req_parsed.netloc
else:
try:
# 'try' for the case no referer exists ("/")
referrer = request.headers['referer']
start_of_uri = referrer.find("/", len("http://") + 1)
if (start_of_uri > 0):
end_of_host = referrer.find("/", start_of_uri + 1)
req_host = referrer[start_of_uri + 1:end_of_host]
except:
pass
get_uri = request.path
else:
get_uri = '/' + '/'.join(request.path.split('/')[2:])
try:
req_sent = False
for conv in CTCore.conversations:
if conv.host == req_host:
if (self.check_request(conv.uri, get_uri) == True):
resp = conv.res_head
if conv.orig_chunked_resp != "":
resp = resp + "\r\n\r\n" + conv.orig_chunked_resp
else:
resp = resp + "\r\n\r\n"
if conv.orig_resp:
resp += conv.orig_resp
self.request.send(resp)
req_sent = True
res = conv.res_num
break
if not req_sent:
if get_uri == "/":
dir_response = b"HTTP/1.1 200 OK\r\n\r\n" + self.build_index()
self.request.send(dir_response)
res = "200 OK [Main Dir]"
else:
self.request.send("HTTP/1.1 404 Not Found")
res = "404 Not Found"
except Exception, e:
res = str(e)
finally:
self.log(req_host + get_uri + " - " + res)
except Exception, e:
print e