我创建了两个脚本,其中服务器向连接的客户端发送声音文件。虽然如果脚本在运行 Windows 10 的两台机器上运行时它工作得很好,但如果服务器端脚本在 Raspberry Pi 4 上运行它就不起作用。相反,只发送一个 8KB 的大声音文件(它应该是约 430KB)。我只是不知道为什么会失败,我在客户端或服务器端都没有收到任何错误。唯一的区别是在 Windows 上两者都在 192.168.178.0/24 的网络上,而在 Raspberry Pi 上,Wifi 子网是 101.101.101.1/24。但最初的连接是有效的,所以我真的不认为这应该是一个问题。有人可以帮我吗?
服务端代码:
from scapy.all import *
import socket
import hashlib
import random
# Set up Host and Port
host = "101.101.101.1"
port = 6000
# Set up the server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen()
# Wait for a client to connect
client_socket, client_address = server_socket.accept()
print(f"Connection from {client_address}")
# Store client address in proper format
client_address_str = str(client_address)
client_ip = client_address_str[1:-1].split(',')[0]
client_ip = client_ip.replace("'", '"')
print(client_ip)
# Authentication
# Send the identity challenge to the client
identity_challenge = "Who are you?"
client_socket.sendall(identity_challenge.encode())
# Receive the identity response from the client
identity_response = client_socket.recv(500000).decode()
# Check if the identity is valid
valid_identity = "client1"
if identity_response != valid_identity:
client_socket.close()
print("Invalid identity")
exit(0)
# Generate a random nonce
nonce = str(random.randint(0, 100000))
# Send the nonce to the client
client_socket.sendall(nonce.encode())
# Receive the nonce response from the client
nonce_response = client_socket.recv(500000).decode()
# Send the identity and nonce back to the client
identity_and_nonce = f"I am the server, your identity is {identity_response} and my nonce is {nonce}. Your nonce is {nonce_response}. I will send you now the file"
client_socket.sendall(identity_and_nonce.encode())
# Wait for a message from the client
message = client_socket.recv(500000).decode()
print(f"Received message from client: {message}")
# Create a custom header
header = "hint"
# Padding the header to a specific length
header = header + " " * (32 - len(header))
# Send the identity and nonce back to the client
sending_soundfile = f"Sending Sound File"
client_socket.sendall(sending_soundfile.encode())
# Read the file and create a Scapy Packet
with open('soundchallenge.wav', 'rb') as sound_file:
sound_data = sound_file.read()
packet = IP(src=host, dst=host) / Padding(load=header) / Raw(load=sound_data)
# Send the packet to the client
client_socket.sendall(sound_data)
# Send custom packet with hint
send(IP(src=host, dst="101.101.101.42")/ICMP()/"Hint")
terminate = False
while not terminate:
# Wait for a message from the client
message = client_socket.recv(500000).decode()
print(f"Received message from client: {message}")
# Check if hint is corrected and send the decoding code
if message == "1337":
senddata = "You seem worthy!"
with open('lsbaudio_decode.py', 'rb') as hint_file:
hint_data = hint_file.read()
client_socket.sendall(hint_data)
# Check if the message is the solution
elif message == "WifiSec":
senddata = "YOU KILLED ME!"
client_socket.sendall(senddata.encode())
terminate = True
else:
senddata = "Cool story, bro"
client_socket.sendall(senddata.encode())
客户端:
import socket
import random
# Set up Host and Port
host = "101.101.101.1"
port = 6000
# Set up the client socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
# Wait for the server's response
server_response = client_socket.recv(500000).decode(errors='ignore')
# Create an identity for the client
identity = "client1"
# Generate a random nonce for the client
nonce = str(random.randint(0, 100000))
# Send the identity and nonce to the server
client_socket.sendall(identity.encode())
client_socket.sendall(nonce.encode())
# Wait for the server's response
server_response = client_socket.recv(500000).decode(errors='ignore')
terminate = False
while not terminate:
# Wait for a message from the server
server_response = client_socket.recv(500000).decode(errors='ignore')
if server_response == "Sending Sound File":
# Wait for the packet from the server
sound_data = client_socket.recv(500000)
# Write the received data to a file
with open('sound_from_server.wav', 'wb') as f:
f.write(sound_data)
elif server_response == "You seem worthy!":
code = client_socket.recv(500000)
exec(code)
else:
print(f"Server: {server_response}")
if server_response == "YOU KILLED ME!":
terminate = True
client_socket.close() # close the connection
exit(0)
message = input(" -> ")
# Send the message to the server
client_socket.sendall(message.encode())
client_socket.close() # close the connection
exit(0)
您可以使用较小的缓冲区大小,如 1024、4096。根据此更改代码(使用 while 循环发送和接收),然后重试。 我不是专家,但 50000 并不常见,我认为这对于套接字来说是一个巨大的数字。