forked from YannChich/Http_Redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
289 lines (235 loc) · 10.6 KB
/
client.py
File metadata and controls
289 lines (235 loc) · 10.6 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import dns
import sys
from dns import resolver
from scapy.all import *
from scapy.layers.dhcp import DHCP, BOOTP
from scapy.layers.inet import IP, UDP
from scapy.layers.l2 import Ether
import socket
import threading
import time
import subprocess
import sys
import time
import requests
from bs4 import BeautifulSoup
from queue import Queue
# DHCP Server.
client_ip = "0.0.0.0"
dns_server_ip = "127.0.0.1"
domain = "example.com"
resolved_ip = "0.0.0.0"
connection_open = True
pause_client = False
list_queue = Queue()
List = {}
# Downloading files.
def download_file(list):
for i in range(0, len(list)):
print(f"-{i+1} : {list[i]}")
while True:
num = int(input("The number of the file you would like to download: Press 0 if u want to exit "))
if num == 0:
print("We are going to exit on the connection with the RUDP Server")
print(".................................................")
sys.exit(0)
if num <= len(list):
filename = f"{list[num - 1]}"
set_ip_command = f"wget --bind-address={client_ip} http://localhost/{filename}"
subprocess.run(set_ip_command, shell=True, check=True)
set_ip_command = f"chmod 777 {filename}"
subprocess.run(set_ip_command, shell=True, check=True)
else:
print("Wrong file number.")
# Sending DHCP discover to the server.
def send_dhcp_dis():
dhcp_packet = (Ether(dst="ff:ff:ff:ff:ff") /
IP(src='0.0.0.0', dst='255.255.255.255') /
UDP(sport=68, dport=67) /
BOOTP(op=1, chaddr=get_if_raw_hwaddr(conf.iface)[1]) /
DHCP(options=[("message-type", "discover"), "end"]))
print("[+] DHCP Discover Sent.")
sendp(dhcp_packet, verbose=False)
# DHCP offer handler function.
def dhcp_offer(client_ip, packet):
if DHCP in packet and packet[DHCP].options[0][1] == 2:
print("[+] Got A DHCP Offer.")
client_ip = packet[BOOTP].yiaddr
print("[+] DHCP Server Sent The IP:", client_ip)
dhcp_packet = (Ether(dst="ff:ff:ff:ff:ff:ff") /
IP(src="0.0.0.0", dst="255.255.255.255") /
UDP(sport=68, dport=67) /
BOOTP(op=1, chaddr=get_if_raw_hwaddr(conf.iface)[1]) /
DHCP(options=[("message-type", "request"),
("requested_addr", packet[BOOTP].yiaddr),
("server_id", packet[IP].src),
"end"]))
print("[+] DHCP Request Sent.")
sendp(dhcp_packet, verbose=False)
return client_ip
# Applying the actual DHCP offer.
def got_dhcp_ack(client_ip, packet):
if DHCP in packet and packet[DHCP].options[0][1] == 5:
print("[+] DHCP ack received.")
interface_name = conf.iface
subnet_mask = "255.255.255.0"
set_ip_command = f"sudo ifconfig {interface_name} {client_ip} netmask {subnet_mask}"
subprocess.run(set_ip_command, shell=True, check=True)
print("[ifconfig] Machine IP set to:", client_ip)
# DNS request sending.
def send_dns_query(dns_server_ip, domain, client_ip=None):
query = dns.message.make_query(domain, dns.rdatatype.A)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if client_ip:
sock.bind((client_ip, 0))
sock.sendto(query.to_wire(), (dns_server_ip, 53))
response_data, server_address = sock.recvfrom(1024)
response = dns.message.from_wire(response_data)
return response
# Extracting the IP from DNS.
def extract_dns_response_ip(response):
if response:
for rrset in response.answer:
if rrset.rdtype == dns.rdatatype.A:
for rdata in rrset:
resolved_ip = rdata.to_text()
return resolved_ip
return None
# RUDP FUNCTIONCS
def hostname_to_numeric(hostname):
return sum(ord(c) for c in hostname)
def handle_lost_packet_signal(client, packet_id, sent_packets):
packet_data = sent_packets.get(packet_id)
print("-----------------------------------------------")
print(f"Resending packet {packet_id}: {packet_data}")
print("-----------------------------------------------")
if packet_data == "SIGNGET":
client.sendto(f"SIGNGET:{packet_id};{packet_data}".encode(), (resolved_ip, 49152))
if packet_data == "SIGNECHO":
client.sendto(f"SIGNECHO:{packet_id};{packet_data}".encode(), (resolved_ip, 49152))
if packet_data == "SIGNSTAT":
client.sendto(f"SIGNSTAT:{packet_id};{packet_data}".encode(), (resolved_ip, 49152))
else:
client.sendto(f"SIGNEND:{packet_id};{packet_data}".encode(), (resolved_ip, 49152))
def handle_ackget(data):
global connection_open
packet_id, file_list = data.split(":", 1)[1].split(";", 1)
my_list = file_list.split(";")[:-1] # Remove the last empty element
list_queue.put(my_list)
connection_open = False
def receive(server, sent_packets):
global connection_open
global pause_client
while True:
data, addr = server.recvfrom(1024)
data = data.decode()
print(f"Received data: {data}")
if data.startswith("SIGNACK"):
print("Receive ACK from the RUDP Server.")
elif data.startswith("ACKGET:"):
print("Received ACKGET from server")
handle_ackget(data)
connection_open = False
elif data.startswith("SIGNLOST:"):
print("Receive SIGNLOST , going to send the lost packet")
packet_id = int(data.split(":", 1)[1])
handle_lost_packet_signal(server, packet_id, sent_packets)
elif data.startswith("ACKEND:"):
print("Received ACKEND from server. Closing connection.")
connection_open = False
server.close()
sys.exit(0)
elif data.startswith("ECHOREPLY:"):
packet_id, timestamp = data.split(":", 1)[1].split(";", 1)
latency = (time.time() - float(timestamp)) * 1000
print(f"Received ECHOREPLY from server. Latency: {latency:.2f} ms")
elif data.startswith("STATREPLY:"):
packet_id, stats = data.split(":", 1)[1].split(";", 1)
print(f"Received STATREPLY from server. Server statistics:\n{stats}")
elif data.startswith("SIGNFULL"):
pause_client = True
packet_id = data.split(":", 1)[1].split(";", 1)
print("Received SIGNFULL from server. Waiting for window to free up.")
def RUDP_Client(hostname):
global connection_open
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.bind((client_ip, 49152))
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print(f"Gonna try to connect to {resolved_ip}")
print(" [RUDP] Sending a SIGNAL : SIGNEW to the RUDP Server")
client.sendto(f"SIGNEW:{hostname}".encode(), (resolved_ip, 49152))
sent_packets = {}
packet_id = hostname_to_numeric(hostname)
# Ajouter un thread pour écouter les messages entrants du serveur
receive_thread = threading.Thread(target=receive, args=(client, sent_packets))
receive_thread.start()
while True:
time.sleep(2)
if pause_client:
time.sleep(10)
if not list_queue.empty():
file_list = list_queue.get()
download_file(file_list)
break
elif not connection_open:
create_new_connection = input(
"Connection closed. Do you want to send SIGNEW to create a new connection? (yes/no): ")
if create_new_connection.lower() == "yes":
connection_open = True
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print(" [RUDP] Sending a SIGNAL : SIGNEW to the RUDP Server")
client.sendto(f"SIGNEW:{hostname}".encode(), (resolved_ip, 49152))
else:
print("END OF CONNECTION")
else:
# input message to let the user what signal is going to be sent
print("-------------------------------------------------------")
print("This is the list of signals you can send :")
print("SIGNGET : to get the redirection to the server HTTP")
print("SIGNEND : to end the connection")
print("SIGNECHO : to know the ping with the server RUDP")
print("SIGNSTAT : to know the stat of your connection with the server RUDP")
print("-------------------------------------------------------")
message = input("SIGNAL :")
packet_id += 1
if message == "SIGNGET":
print(f" [RUDP] Sending a SIGNAL : n°{packet_id} SIGNGET to the RUDP Server")
client.sendto(f"SIGNGET:{packet_id};{message}".encode(), (resolved_ip, 49152))
sent_packets[packet_id] = message
elif message == "SIGNEND":
print(f" [RUDP] Sending a SIGNAL : n°{packet_id} SIGNEND to the RUDP Server")
client.sendto(f"SIGNEND:{packet_id};{message}".encode(), (resolved_ip, 49152))
sent_packets[packet_id] = message
elif message == "SIGNECHO":
timestamp = time.time()
print(f" [RUDP] Sending a SIGNAL : n°{packet_id} SIGNECHO to the RUDP Server")
client.sendto(f"SIGNECHO:{packet_id};{timestamp}".encode(), (resolved_ip, 49152))
sent_packets[packet_id] = message
elif message == "SIGNSTAT":
print(f" [RUDP] Sending a SIGNAL : n°{packet_id} SIGNSTAT to the RUDP Server")
client.sendto(f"SIGNSTAT:{packet_id}".encode(), (resolved_ip, 49152))
sent_packets[packet_id] = message
else:
print("Invalid signal, please try again.")
# Main func.
if __name__ == "__main__":
hostname = input("Enter your name : ")
# DHCP Block
send_dhcp_dis()
dhcp_packet = sniff(filter="udp and (port 67 or port 68)", count=1, timeout=10, iface="ens33")[0]
client_ip = dhcp_offer(client_ip, dhcp_packet)
dhcp_packet = sniff(filter="udp and (port 67 or port 68)", count=1, timeout=10, iface="ens33")[0]
got_dhcp_ack(client_ip, dhcp_packet)
print("")
# DNS Block
print(f"[DNS] Sending DNS request for the domain: {domain}")
dns_response = send_dns_query(dns_server_ip, domain, client_ip)
resolved_ip = extract_dns_response_ip(dns_response)
if resolved_ip:
print(f"[DNS] The domain {domain} has been resolved to {resolved_ip}")
else:
print(f"[DNS] The domain {domain} could not be resolved.")
print("")
#RUDP Block
RUDP_Client(hostname)