Spoofing IP address on same machine running 127.0.0.1 server - python

I am hosting a server listening for UDP packets on local host with IP address '127.0.0.1'. On the same machine, how would I be able to send packets to this server with spoofed IP address '1.2.3.4' and not '127.0.0.1'?
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
if __name__ == "__main__":
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
sizes = {}
for size in range(512):
sizes[size] = 0
while True:
data, addr = sock.recvfrom(8092)
if addr[0] != "1.2.3.4":
print("Acess denied")
#print(addr[0])
#print(len(data))
continue
else:
print("hello")
print ("length:", len(data))
sizes[len(data)] += 1

Currently this is the code I am using to send UDP packets.
import socket
import ipaddress
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
if __name__ == "__main__":
ipaddress.ip_address(unicode('1.2.3.4', "utf-8"))
Message = "H"
clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSock.sendto(Message, (UDP_IP, UDP_PORT))

Related

Read Brodcast (ff02::1) msg python socket

i have this code to read a msg from a client.
import socket
# UDP_IP = 'fd53:7cb8:383:2::10'
UDP_IP = '::'
UDP_PORT = 13400
# create socket
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# bind the socket
server_add = (UDP_IP, UDP_PORT)
print('starting up on {} on port {}'.format(*server_add))
sock.bind(server_add)
while True:
print('waiting msg')
data, addr = sock.recvfrom(1024)
print("received message:", data)
print("addr is", addr)
sock.close()
break
the client IP is : 'fd53:7cb8:383:2::c9'
the Server IP is : 'fd53:7cb8:383:2::10'
i can saw the msg on wireshark
No.
Time
Delta
Source
Destination
Protocol
Length
70
6.516768
3.631930
fd53:7cb8:383:2::c9
ff02::1
DoIP
103
but the sock.recvfrom(1024) is not getting anything

How can I list the files

i am new on python. I want to list all the files in the server , on the client. I tried to do but i couldnt.
server.py:
import socket
import os
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
port = 5050
s.bind((host,port))
s.listen(1)
print(socket.gethostbyname(socket.gethostname()))
print('[WAITING FOR NEW CONNECTIONS...]')
conn,addr = s.accept()
print(addr, ' has connected the server')
client.py:
import socket
import os
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = input(str('Please enter the host address: '))
port = 5050
s.connect((host,port))
print('[CONNECTED]')
list = os.listdir('.')
for i in list:
if i.endswith('.txt'):
print(i)

Cannot read message from client : UDP python 3

I tried to send message to server from client with manual input, with 10 limits input. its succesfully work on client side but when i tried to run server it's shows nothing
here's the code from client side
import socket
UDP_IP = "localhost"
UDP_PORT = 50026
print ("Destination IP:", UDP_IP)
print ("Destination port:", UDP_PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for x in range (10):
data = input("Message: ")
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print(data)
else :
print("lebih dari 10!!")
s.sendto(data.encode('utf-8'), (UDP_IP, UDP_PORT))
s.close()
here's result and code from server side
import socket
UDP_IP = "localhost"
UDP_PORT = 50026
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((UDP_IP, UDP_PORT))
while True:
data, address = s.recvfrom(1024)
print(data)
print(address)
s.close()
when i run the program, nothing happen. here's the running program
Your main problem is the else statement you added there which is not executing. If want to put a limit of 10 after accepting the input, you are supposed to print the statement after the loop.
This is the client code:
import socket
UDP_IP = "127.0.0.1" # It is the same as localhost.
UDP_PORT = 50026
print ("Destination IP:", UDP_IP)
print ("Destination port:", UDP_PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for x in range (10):
data = input("Message: ")
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print(data)
s.sendto(data.encode('utf-8'), (UDP_IP, UDP_PORT))
print("lebih dari 10!!")
s.close()
Edit:
I am not really understanding your problem but as far as I understand you want to show the limit on the server. Thus you can do this, try adding a loop on the server and receive input from the client's address only to avoid receiving extra messages.
Server Code:
import socket
UDP_IP = "127.0.0.1" # It is the same as localhost.
UDP_PORT = 50026
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((UDP_IP, UDP_PORT))
x = 0
while True:
data, address = s.recvfrom(1024)
# This block will make sure that the packets you are receiving are from expected address
# The address[0] returns the ip of the packet's address, address is actually = ('the ip address', port)
if address[0] != '127.0.0.1':
continue
# The logic block ends
print(data)
print(address)
x = x + 1 # This shows that one more message is received.
if x == 10:
break # This breaks out of the loop and then the remaining statements will execute ending the program
print("10 messages are received and now the socket is closing.")
s.close()
print("Socket closed")
I have commented the code so I hope you understand the code

Protect socket from DOS and DDOS using python

Here is my socket server and client:
import socket
import threading
import chardet
bind_ip = '0.0.0.0'
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(1) # max backlog of connections
print (('Listening on {}:{}').format(bind_ip, bind_port))
def handle_client_connection(client_socket):
request = client_socket.recv(4096 )
result = chardet.detect(request)
print(result)
print (request.decode(result['encoding']))
client_socket.send('ACK!'.encode(result['encoding']))
client_socket.close()
while True:
client_sock, address = server.accept()
print (('Accepted connection from {}:{}').format(address[0], address[1]))
client_handler = threading.Thread(
target=handle_client_connection,
args=(client_sock,) # without comma you'd get a... TypeError: handle_client_connection() argument after * must be a sequence, not _socketobject
)
client_handler.start()
The client is this:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 9999))
client.send(str('test data').encode("utf-16"))
response = client.recv(4096)
print(response.decode("utf-16"))
How to make the socket server secured from DOS or DDos attacks?

how to create a UDP server that will listen on multiple ports in python?

this is my server:
import socket
for port in range(33,128):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('0.0.0.0', port))
while True:
(client_name, client_adress) = server_socket.recvfrom(1024)
print chr(port)
server_socket.close()
this is my client:
import socket
message = raw_input("Enter a message: ")
for letter in message:
my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
my_socket.sendto("", ('127.0.0.1', ord(letter)))
(data, remote_adress) = my_socket.recvfrom(1024)
my_socket.close()
print 'The server sent: ' + data
I'm not very good in python, but I think you should save your sockets to list inside for and then use select function in infinite loop outside for
import socket
import select
sockets = []
for port in range(33,128):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('0.0.0.0', port))
sockets.append(server_socket)
empty = []
while True:
readable, writable, exceptional = select.select(sockets, empty, empty)
for s in readable:
(client_data, client_address) = s.recvfrom(1024)
print client_address, client_data
for s in sockets:
s.close()

Categories