84 lines
1.9 KiB
C
84 lines
1.9 KiB
C
// Simple TCP knock client - Linux version
|
|
// Compile: gcc -o knock_client_linux knock_client_linux.c
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <netinet/tcp.h>
|
|
#include <arpa/inet.h>
|
|
#include <time.h>
|
|
|
|
void knock_port(const char *ip, int port) {
|
|
int s = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (s < 0) {
|
|
perror("socket failed");
|
|
return;
|
|
}
|
|
|
|
struct sockaddr_in addr;
|
|
memset(&addr, 0, sizeof(addr));
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons((unsigned short)port);
|
|
inet_pton(AF_INET, ip, &addr.sin_addr);
|
|
|
|
printf("Knocking on port %d...\n", port);
|
|
|
|
int result = connect(s, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
usleep(100000);
|
|
|
|
close(s);
|
|
|
|
if (result < 0) {
|
|
printf(" Connection refused (expected - no listener)\n");
|
|
} else {
|
|
printf(" Connected!\n");
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if (argc < 2) {
|
|
printf("Usage: %s <target_ip> [port1 port2 port3 ...]\n", argv[0]);
|
|
printf("Default ports: 7000 8000 9000\n");
|
|
return 1;
|
|
}
|
|
|
|
const char *ip = argv[1];
|
|
|
|
int ports[10];
|
|
int num_ports;
|
|
|
|
if (argc > 2) {
|
|
num_ports = argc - 2;
|
|
for (int i = 0; i < num_ports && i < 10; i++) {
|
|
ports[i] = atoi(argv[i + 2]);
|
|
}
|
|
} else {
|
|
num_ports = 3;
|
|
ports[0] = 7000;
|
|
ports[1] = 8000;
|
|
ports[2] = 9000;
|
|
}
|
|
|
|
printf("Knocking on %s: ", ip);
|
|
for (int i = 0; i < num_ports; i++) {
|
|
printf("%d ", ports[i]);
|
|
}
|
|
printf("\n\n");
|
|
|
|
for (int i = 0; i < num_ports; i++) {
|
|
knock_port(ip, ports[i]);
|
|
if (i < num_ports - 1) {
|
|
usleep(500000);
|
|
}
|
|
}
|
|
|
|
printf("\nKnock sequence complete!\n");
|
|
printf("Now try connecting to port 31337\n");
|
|
|
|
return 0;
|
|
}
|