Linux: how to send TCP packet from specific port?
If you are using raw sockets, then just fill in the correct TCP source port in the packet header.
If, instead, you are using the TCP socket interface (socket()
, connect()
and friends), then you can set the source port by calling the bind()
system call for the client socket - exactly as you would to set the listening port for the server socket.
Is it possible to send TCP packet to a specific port with scapy's send function?
I have an simple idea:
port = random.randint(48620, 49150)
If you want to know what is port, go to:
https://www.techtarget.com/searchnetworking/definition/port-number
Thanks
Linux tool to send raw data to a TCP server
netcat or telnet, i have used both in the past to test simple text based protocols. netcat is more flexible.
When binding a client TCP socket to a specific local port with Winsock, SO_REUSEADDR does not have any effect
When you create a socket with socket()
, it has only a type and a protocol family. The ideal is to bind()
it to a local address:port too.
The error you mentioned normally happens when the last connection to the same host:port didn't have a graceful shutdown (FIN/ACK FIN/ACK). In these cases, the socket stays in TIME_WAIT
state for a certain period of time (OS dependent, but adjustable).
What happens then is when you try to connect()
to the same host and same port, it uses the default socket's name/address/port/etc, but this combination is already in use by your zombie socket. To avoid this, you can change the local address:port used to establish the connection by calling bind()
after the socket creation, providing the sockaddr
struct filled with your local address and a random port.
int main() {
int ret, fd;
struct sockaddr_in sa_dst;
struct sockaddr_in sa_loc;
char buffer[1024] = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
fd = socket(AF_INET, SOCK_STREAM, 0);
// Local
memset(&sa_loc, 0, sizeof(struct sockaddr_in));
sa_loc.sin_family = AF_INET;
sa_loc.sin_port = htons(LOCAL_RANDOM_PORT);
sa_loc.sin_addr.s_addr = inet_addr(LOCAL_IP_ADDRESS);
ret = bind(fd, (struct sockaddr *)&sa_loc, sizeof(struct sockaddr));
assert(ret != -1);
// Remote
memset(&sa_dst, 0, sizeof(struct sockaddr_in));
sa_dst.sin_family = AF_INET;
sa_dst.sin_port = htons(80);
sa_dst.sin_addr.s_addr = inet_addr("64.233.163.104"); // google :)
ret = connect(fd, (struct sockaddr *)&sa_dst, sizeof(struct sockaddr));
assert(ret != -1);
send(fd, buffer, strlen(buffer), 0);
recv(fd, buffer, sizeof(buffer), 0);
printf("%s\r\n", buffer);
}
UPDATE: As using a specific local port is a requirement, consider setting SO_LINGER
with l_onoff=1
and l_linger=0
so your socket won't block upon close
/closesocket
, it will just ignore queued data and (hopefully) close the fd. As a last resort you can adjust the TIME_WAIT
delay by changing the value of this registry key (highly discouraged!):
HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpTimedWaitDelay
Related Topics
Profiling Arbitrary Cuda Applications
Access Podman Rest API from Testcontainer
How to Enable Hocr Font Info in Tesseract 4
Apache/Httpd /Var/Www/HTML/ .Cgi Scripts Throw 500 Internal Server Error
Using Winscp to Grab a File Through a Tunnel
Ansible: Copying One Unique File to Each Server in a Group
Linux Configuration - Ssmtp: Cannot Open Smtp.Gmail.Com:587
How to Find The Version of a Compiled Kernel Module
Linux - Change The Hostname in The Cli
Scheduling in Linux: Run a Task When Computer Is Idle (= No User Input)
Disable CPU Caches (L1/L2) on Armv8-A Linux
Insufficient Permission Pushing to Git Shared Repo Over Smart Http
Best Linux Filesystem Filter Option
What Means "Atomic" System Call
Setgid Bit Not Preserved by Git on New Directory in '.Git' Folder
Removing File from Folder in Linux