Use Python scapy to send TCP RST to prevent client access problems - python

I used SCAPY to write a program deployed in the WEB server and would like to send TCP RST using SCAPY to block some specific HTTP client access.
After running the program, the client uses Telnet to connect to the server WEB listening port can be SCAPY program interrupt, but the use of browser access can not interrupt. Why is it so?
The code is as follows:
# coding: utf-8
# web server : 10.28.16.20 ;
# http client : 10.28.1.70;
from scapy.all import *
def pkgs(pkg):
if pkg.getlayer(TCP) and pkg[IP].dst=="10.28.16.20" and "10.28.1.70" in pkg[IP].src:
resp=IP(dst=pkg[IP].src,src=pkg[IP].dst)/TCP(dport=pkg[TCP].sport,sport=pkg[TCP].dport,flags="RA",seq=pkg[TCP].ack,ack=pkg[TCP].seq+(len(pkg[TCP].payload) if pkg.getlayer(Raw) else 1))
send(resp,count=2,verbose=0)
if __name__=="__main__":
conf.L3socket=L3RawSocket
sniff(filter="tcp",prn=pkgs,store=0)
Program code screenshot

I think the best way is to get into "Scapy"
then paste that code and enter:
#!/usr/bin/python
from scapy.all import *
ip = IP(src="10.28.16.20", dst="10.28.1.70")
tcp = TCP(sport=80, dport=####, flags=####, seq=####, ack=####)
pkt = ip/tcp
ls(pkt)
send(pkt,verbose=0)
You need to change dport to the source port from the PC, flags to the action you want to do, seq&ack follow the packets you started from.

I have solved it.import threading.

Related

pySerial - login prompt being written to buffer when writting a message to tty serial

I am trying to use a GSM modem on a RPi4 to do a simple TCP connection to a remote server. I have this working, but when I go to write a message to the server, the login prompt for the pi is included in the byte stream sent to the server.
My objective is to have only what my code sends to be sent to the server :)
My relevant python3 code is:
ser = serial.Serial('/dev/ttyS0', 115200)
ser.write((f'AT+CIPOPEN=0,"TCP","{config.server_host}",{config.server_port}').encode())
ser.write(b'AT+CIPSEND=0,')
time.sleep(5)
ser.write(b'GET / HTTP/1.1\r\n')
ser.write(b'\x1A')
And what I receive on the server is:
connection from ('x.x.x.x', yyyyy)
received b'\r\n\r\n>garage logi'
received b'n: Password: GET'
received b' / HTTP/1.1\r\r\n'
no more data from ('x.x.x.x', yyyyy)
Can someone help me understand where this received b'\r\n\r\n>garage logi'... is being injected into the write buffer of the serial connection?
I have tried using ser.reset_input_buffer() and ser.reset_output_buffer() but no changes. I have increased, removed, etc the sleeps. No changes ever.
The problem was that the serial port had a login shell attached to it by the Raspberry Pi. I disabled this login from raspi-config and that resolved the issue.

Connecting to Python socket from iOS Safari browser

I have a python socket listening on my computer's ip address on a specific port. I am using the standard python socket library with something like the following code:
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind(server_address) # Server address is a tuple (HOST, PORT) with host being
# something like 123.456.789.00 and port being 4000
listen_socket.listen()
client_connection, client_address = listen_socket.accept()
request_data = client_connection.recv(1024).decode('utf-8')
print(request_data)
When I use Chrome on my iPhone to connect to the address, the rest of my code runs fine and processes the incoming request by sending an html page. When I use Safari, it loads for a while until it fails and a says "Safari cannot open the page because it could not connect to the server."
Funny enough, if I stay on the page, restart the server, and then reload the page using the refresh button, it connects fine. But trying to access the address through the url address does not work. Refreshing using the refresh button seems to work. Why is Safari having trouble connecting to my Python socket?
Try increasing the backlog count in the listen_socket.listen() call.
I had a similar issue with a python 2 socket server and macOS Safari. Safari would say it could not connect to the server, but Chrome/Firefox/IE/curl all worked without any errors. I changed this:
listen_socket.listen(1)
To this:
listen_socket.listen(5)
Safari then worked without any errors. I am now using 128 instead of 5, not sure what the default is in Python 3.
When set to 1, in the python server I would see Safari connect, but then socket.recv() would never return.

DNS server doesn't get data python

i built a simple DNS server.. and i'm just trying to print the data (the whole packet) but my server stuck at the recvfrom part.
i tried to open a file that i got as adminstor which changes my DNS server to 127.0.0.1 but it doesn't work.
this is my code:
i tried to write some url's on my browser but my server doesn't get nothing and stuck at the recv.
import socket
myserver = sokcet.socket(socket.AF_INET, socket.SOCKET_DGRAM)
myserver.bind(('0.0.0.0',53))
data, addr = myserver.recvfrom(1024)
print data

Python socket on Windows 7 is not accessible from another machine

I'm trying to create a Python program that will listen on a socket. I'm using Windows 7 with Python 2.7. Whatever I do, the socket seems to be accessible from the local machine but not from elsewhere on the network.
I've got the following code:
from werkzeug.wrappers import Request, Response
#Request.application
def application(request):
return Response('Hello World!')
if __name__ == '__main__':
from werkzeug.serving import run_simple
# Using empty string or the machine's real IP address here
# gives the same problem
run_simple('0.0.0.0', 4000, application)
If I connect from the local machine I see the response fine. If I execute
$ curl 'http://192.168.1.1:4000/'
from another (linux) box on the network, the curl hangs for a long time before timing out. Wireshark shows that I receive a SYN packet to port 4000 but don't see it ACKed.
I've tried making sure packets to this port are allowed through the firewall (the fact that I see the SYNs in Wireshark suggests this is not the problem). I've tried setting Python to run as administrator (and I've checked that ctypes.windll.shell32.IsUserAnAdmin() returns true). This isn't just Werkzeug, I've tried with SocketServer from the Python standard library as well.
Running Windows Apache on the same port works fine from across the network, which suggests there's no problem with the network or firewall or with my curl request.
netstat -an shows:
TCP 0.0.0.0:4000 0.0.0.0:0 LISTENING
Edit: I've tried with the following minimal code. On the server side (Windows 7):
import socket
s = socket.socket()
s.bind(('', 8080))
s.listen(1)
remotesock, addr = s.accept()
And on the linux client:
import socket
s = socket.socket()
s.connect('192.168.1.1', 8080)
This hangs until timeout, as with the curl.
I believe the problem is due to your address binding. Python does not allow sockets bound to localhost (or 0.0.0.0) to be visible from the outside world. Change the binding to your actual IP Address.
EDIT: Showing example code
Change your code to this
import socket
s = socket.socket()
s.bind(('192.168.1.1', 8080)) # assumes your machine's IP address is 192.168.1.1
s.listen(1)
remotesock, addr = s.accept()

How do I make a simple DNS server listening two ports simultaneously in python

I'm trying to build a DNS server in python. It must listen two ports (8007 - client, 8008 - admin). The client only send an URL and receives the respective IP. The admin has permissions to change the DNS table (add, remove,.. doesn't matter to this right now).
So my question is: how do I implement the server listening continuously on the two ports for any eventual request (we can have several clients at the same time but only one admin when he is operating)
my Server with one listening port:
from SocketServer import *
from threading import *
from string import *
import socket
class Server(ForkingMixIn, TCPServer): pass #fork for each client
class Handler(StreamRequestHandler):
def handle(self):
addr = self.request.getpeername()
print 'Got connection from', addr
data=(self.request.recv(1024)).strip()
if data not in dic: #dic -> dictionary with URL:IP
self.wfile.write('0.0.0.0')
else:
self.wfile.write(dic.get(data))
server = Server(('', 8007), Handler)
server.serve_forever()
No need to use threads.
Use twisted.
TwistedNames has support out of the box for a dns server. You can customize it as needed or read its source as base when you build yours.
You can use non-blocking sockets, and use the select call to read from the socket. This Sockets Programming HOWTO for Python article has a section on non-blocking sockets in Python that will help.
See also:
select (Python) | select (UNIX)
socket (Python) | socket (UNIX)

Categories