0
votes

I have this code:

import socket
import socks

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, "IP_SOCK", PORT_SOCK, True)
s = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((target,port))
s.close()
print("Connection tested.")

It works, but the lenght of the packets is 0. I'd like to add a payload data, how can I do that?

For example, here I have this working code with scapy library:

import socket
import socks
from scapy.all import *


def send(src_ip, dst_ip, dst_port):
    ip = IP(src=src_ip, dst=dst_ip)
    src_port = random.randint(20, 65000)
    transport = TCP(sport=src_port, dport=dst_port, flags="S")
    send(ip/transport/b"mydata")


if __name__ == '__main__':
    send('SRC_IP', 'DST_IP', DST_PORT)

And it send indeed the "mydata" payload. If I intercept it with tcpdump, I can see the lenght of data field and "mydata" in the syn packet.

I would like to send it through a sock or a http proxy, but I don't know how can I do it.

The fact is that I don't need to establish the connection, because I want to send packets even if the port is closed.

Some people continue to tell me that I can't use socks to send tcp packets. It's not true. SOCKS acts in layer 4, so it's perfectly doeable, the thing is that I don't know how to do it in Python since I'm new to it.

2
Did you consider the possibility that the people who "continue to tell" you that you cannot send raw tcp packets through socks might be right? Here is the relevant specification. - spectras
SOCKS does not act in layer 4. It is an application protocol. - user207421

2 Answers

2
votes

Some people continue to tell me that I can't use socks to send tcp packets. It's not true.

It is true even if you don't like it. A proxy (HTTP proxy or Socks proxy) is not a packet forwarder on the network level. It is instead a payload forwarder at the application level. If you want a packet forwarder you need a real VPN (with the focus on "N", i.e. network level) instead.

When using a Socks or HTTP proxy one needs to establish a TCP connection to the proxy first. Inside this TCP connection one then does some initial handshaking like authentication against the proxy and specification of the target one likes to reach. The proxy then connects to the target and once this is done there are two TCP connections: one between client and proxy and another between proxy and server. The proxy will then read on both of these connections and forward the data received to the peer, i.e. data received from the client will be forwarded to the server etc. The proxy might also modify data in transit.

Since the proxy only forwards the data at the application level there is no way to send crafted TCP packets trough the proxy. And since the proxy maintains two independent TCP connections and just forwards application level data even the incoming and outgoing packet sizes might be different, i.e. two incoming packets can be merged in one outgoing packet or might result in three etc. Since TCP is just byte stream packet sizes don't matter at the application level.

-1
votes

you can use sendall(); for example: s.sendall("GET / HTTP/1.1 ...")

For more details, see https://pypi.org/project/PySocks/ , where I obtained this example from, as well as https://docs.python.org/3/library/socket.html (socks mimics python's built in sockets)