-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebs.py
140 lines (120 loc) · 3.55 KB
/
webs.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import string
import re
import os,time,sys,socket,threading,array,re,fcntl,struct
def hex2str(num):
e="%x"%num
e=str(e)
e='0'+e if len(e)==1 else e
return e
def str2hex(char):
num=string.atoi(char, 16)
return num
def hex_split(stri):
m=len(stri)%2
stri='0'+stri if m!=0 else stri
le=len(stri)//2
li=[]
str1=stri[::2]
str2=stri[1::2]
for i in range(le):
li.append(str1[i]+str2[i])
return li
def find_num(str):
nums=''
for i in str:
nums=nums+i if i.isdigit() else nums
return int(nums)
def space_num(str):
return len(str)-len(str.replace(' ', ''))
def make_challenge(key1,key2,key3):
num1=find_num(key1)//space_num(key1) if find_num(key1)%space_num(key1)==0 else -1
num2=find_num(key2)//space_num(key2) if find_num(key2)%space_num(key2)==0 else -1
#print num2,num1
if (num1<0) or (num2<0):
raise
else:
string=hex_split(str("%x"%num1))+hex_split(str("%x"%num2))+map(hex2str,map(ord,key3))
return map(str2hex,string)
def make_respond(header):
print get_keys(header)
key1,key2,key3=get_keys(header)
challenge=make_challenge(key1,key2,key3)
print challenge
import hashlib
s=''.join(map(chr,challenge))
m=hashlib.md5(s)
sum=m.hexdigest()
return ''.join(map(chr,map(str2hex,hex_split(sum))))
def get_keys(handinfo):
read_re=re.findall(r'Sec-WebSocket-Key1: (.*)\n',handinfo)
key1=read_re[0].strip()
read_re=re.findall(r'Sec-WebSocket-Key2: (.*)',handinfo)
key2=read_re[0].strip()
read_re=re.findall(r'\r\n\r\n(.*)',handinfo,re.S)
# print read_re
# raise
key3=read_re[0].strip()
return key1,key2,key3
header="""GET /demo HTTP/1.1
Host: example.com
Connection: Upgrade
Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
Sec-WebSocket-Protocol: sample
Upgrade: WebSocket
Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
Origin: http://example.com
^n:ds[4U"""
class websocket( ):
def __init__(self,ip,port):
self.ip = ip
self.port = port
self.allreport = ''
def handshake(self,s):
ip=self.ip
port=self.port
header=s.recv(4096)
print repr(header)
resp=make_respond(header)
read_re=re.search('http.*\d*\.\d*\.\d*\.\d*',header)
orign=read_re.group(0) if read_re != None else ''
#buffer= array.array('c', '\0' * 4096)
back='''HTTP/1.1 101 Web Socket Protocol Handshake\r
Upgrade: WebSocket\r
Connection: Upgrade\r
Sec-WebSocket-Origin: %s\r
Sec-WebSocket-Location: ws://%s:%s/\r
Sec-WebSocket-Protocol: sample\r\n\r
%s\r
'''.strip()%(orign,ip,port,resp)
print repr(back)
s.send(back)
#log_file=open('1','rw',0)
#s.recv_into(buffer)
#print buffer
#print repr(s.recv(4096))
#time.sleep(5)
s.send('\x00%s\xff'%'haha')
return s
def handle(self,t):
self.handl=self.handshake(t)
self.send('dad')
def send(self,msg):
self.handl.send('\x00%s\xff'%msg)
def create(self):
s = socket.socket()
ip=self.ip
port=self.port
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((self.ip, self.port));
s.listen(1);
while 1:
t,_ = s.accept();
try:
threading.Thread(target = self.handle, args = (t,)).start()
except 'Broken pipe':
continue
a=websocket('192.168.9.83',12345)
a.create()
#print make_respond(header)