I am establishing a TFTP connection using UDP.
My client is able to successfully send a request to server
cSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cSocket.bind(('', clientPort))
cSocket.settimeout(1)
cSocket.sendto(key, (Serverip, 69))
And server is able to receive it
serverSock.bind((serverIP, 69))
request, clientIP = serverSock.recvfrom(1024)
Now from anthother port server is responding back to client with an acknowledgement
aSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
aSocket.settimeout(0.1)
aSocket.sendto(byteArray, (clientIP, clientPort))
From Wireshark I am able to see the acknoledgement send from server but
data, addr = cSocket.recvfrom(1024)
Leads to a timeout exception. Where did i went wrong?
Related
I'm working on a messaging app and I need to run my python application on a specific port.
I need to be able to connect to the application directly on the server's IP and said port using PuTTY.
OS: Ubuntu 20.04 Server
I tried connecting to a screen session running the application via SSH but there was no information on how to do this.
Since you already have a server, you can simply bind server to specific IP addr and port and connect using PuTTY.
Bind the IP addr 0.0.0.0 and port 8080. Connect to server using PuTTY by specific port and ip. process_data is where you add your code to handle incoming data and send response to client.
import socket
def start_server():
# Create a TCP socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a specific IP address and port
server.bind(('0.0.0.0', 8080))
# Listen for incoming connections
server.listen(1)
# Accept incoming connections
conn, addr = server.accept()
print('Connected by', addr)
# Handle the incoming connection
while True:
# Receive data from the client
data = conn.recv(1024)
# If no data was received, the connection was closed
if not data:
break
# Process the data and send a response back to the client
response = process_data(data)
conn.send(response)
# Close the connection
conn.close()
def process_data(data):
# Add your code here to process the data received from the client
# ...
# Return the response to be sent back to the client
return response
if __name__ == '__main__':
start_server()
I am trying to set up a Raspberry pi to transmit data to my PC via UDP. To do this, both devices are connected to my mobile hotspot.
PC IP: 192.168.78.1
RasPi IP: 192.168.78.57
There are two scripts:
UDP Server
import socket
UDP_IP = '192.168.78.1' #Used when PC is server
#UDP_IP = '192.168.78.57' #Used when RasPi is server
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((UDP_IP,UDP_PORT))
data,addr = sock.recvfrom(4096)
print(str(data))
message = "Hello, I am the UDP Server"
sock.sendto(message.encode("utf-8"), addr)
sock.close()
UDP Client
import socket
UDP_IP = '192.168.78.1' #Used when PC is server
#UDP_IP = '192.168.78.57' #Used when RasPi is server
UDP_PORT = 5005
client_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
message = 'Hi, this is a client'
client_socket.sendto(message.encode("utf-8"),(UDP_IP,UDP_PORT))
data,addr = client_socket.recvfrom(4096)
print('Server Says')
print(str(data))
client_socket.close()
The client sends a message to the server and receives a message in response.
When I run the server code on the RasPi and the client code on the PC, everything works fine.
However, when I run the server code on the PC and the client code on the Raspi, it does not work.
The server gets stuck on the line data,addr = sock.recvfrom(4096) presumably waiting for a message, while the client gets stuck on the line client_socket.sendto(message.encode("utf-8"),(UDP_IP,UDP_PORT)) presumably trying to send the message.
Can anyone help me explain why the connection works with the RasPi as the server but not with the PC as the server?
I have a UDP communication between a server and client on localhost
according to this code:
https://pymotw.com/2/socket/udp.html
Echo Server:
import socket
import sys
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('127.0.0.1', 12321)
sock.bind(server_address)
while True:
data, address = sock.recvfrom(4096)
if data:
sent = sock.sendto(data, address)
echo Client
import socket
import sys
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('127.0.0.1', 12321)
message = 'This is the message. It will be repeated.'
try:
for i in range 4:
sent = sock.sendto(message, server_address)
data, server = sock.recvfrom(4096)
finally:
sock.close()
now let's say I got some MITM attack, and a specific packet doesn't arrive at the server, and the client is still waiting for a response from the server,
I get a deadlock.
how can I overcome this? is there some timeout parameter for UDP socket?
Yes, there is a timeout for UDP sockets. See socket.settimeout() in https://docs.python.org/2/library/socket.html and read up on non-blocking sockets in general.
Note that UDP packets can be dropped, duplicated, and/or re-ordered, even if there is no man-in-the-middle attacker. This is because UDP is (by design) an unreliable datagram protocol.
If you need a reliable protocol use TCP (or QUIC).
If you need assurance that no man-in-the-middle can modify or (optionally) observe the data, use TLS (or QUIC).
Two computers in a LAN connecting to a wireless router, one IP address is 192.168.1.106 (server), the other one is 192.168.1.107 (client), the gateway on both computer is 192.168.1.1 (the router itself).
The two computer can ping each in two directions which means there should be no problem with routing and the router itself. But I failed when I tried to use Python UDP socket, the server cannot get any information from the client, and same happened when I change the ip address. (But it works fine when server and client are on a same computer using local ip address, so the code is should be ok)
I am using the following code:
server:
import socket
address = ('192.168.1.106', 5678) # the server listening on address 192.168.1.106
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(address)
while True:
data, addr = s.recvfrom(2048)
if data == "empty":
print "no data from client"
else:
print "received:", data, "from", addr
s.close()
client:
import socket
address = ('192.168.1.106', 5678) # the client send to address 192.168.1.106
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
msg = raw_input()
if not msg:
msg = "empty"
s.sendto(msg, address)
s.close()
Did you open the UDP port on the firewall on both comoutera?
I have set up a server socket (plain raw socket) listening on port A. A client now connects to this server. OS opens up a port for the client for this purpose. Say port B is allocated to this client. Now my question is, can a 3rd script connect to this port B and send data. Or in other words can I spoof a response to the client as if it was coming from the server? I tried spoofing it using scapy, but it wasnt working.
server.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost", A))
s.listen(10)
ns, cli_addr = s.accept()
time.sleep(30) # so that i can trigger my 3rd script
goodclient.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", A))
print s.getsockname() # to get the local port of client - B
s.recv(1024)
badboy.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", B)) # connection refused error
s.send("hihihi")
scapybadboy.py
pack = IP(src="localhost", dst="localhost") / TCP(sport=A, dport=B) / "Hello"
send(pack) # Packet sent but not received by the client
Because server and client using SOCK_STREAM sockets, they both aware of TCP session(including port, IP and (SEQ_NUMBER,ACK_NUMBER)), so when session is already in process, you will have to perform TCP hikacking and IP spoofing in order to send messages in stream.
In other words, you will have to guess(or steal) ACK number of server in order to send fake messages to client using badclient.
However, if you will make somehow goodclient answer you and not a server you should run the following:
iptables -A FORWARD -j NFQUEUE --queue-num 1 , because your operating system doesn't know about session that you just "opened" with goodclient and it will send RST packet. This command will prevent it.