1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#define DATAGRAM_SIZE 4096
#define PORT_DST 80
#define PORT_SRC 1234
#define IP_SRC 127.0.0.1
#define IP_DST 213.246.63.35
unsigned short csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
int main(void)
{
char datagram[DATAGRAM_SIZE];
struct iphdr *iph = (struct iphdr *) datagram;
struct tcphdr *tcph = (struct tcphdr *) datagram + sizeof(struct iphdr);
struct sockaddr_in sain;
int sockfd = socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
int on;
sain.sin_family = AF_INET;
sain.sin_port = htons(PORT_DST);
sain.sin_addr.s_addr = inet_addr("IP_DST");
memset(&(sain.sin_zero),0,8);
memset(datagram,0,DATAGRAM_SIZE);
iph->version = 4;
iph->ihl = 5;
iph->tos = 0;
iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
iph->id = htonl(8147);
iph->frag_off = 0;
iph->ttl = 255;
iph->protocol = 6;
iph->check = 0;
iph->saddr = inet_addr("IP_SRC");
iph->daddr = sain.sin_addr.s_addr;
tcph->source = htons(PORT_SRC);
tcph->dest = htons(PORT_DST);
tcph->seq = 0;
tcph->ack_seq = 0;
tcph->res1 = 0;
tcph->doff = 0;
tcph->fin = 0;
tcph->syn = 1;
tcph->rst = 0;
tcph->psh = 0;
tcph->ack = 0;
tcph->urg = 0;
tcph->ece = 0;
tcph->cwr = 0;
tcph->window = htonl(65535);
tcph->check = 0;
tcph->urg_ptr = 0;
iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL,(char *)&on, sizeof (on)) < 0)
{
puts("Warning: Cannot set HDRINCL!\n");
}
if (sendto(sockfd, datagram, iph->tot_len, 0, (struct sockaddr *) &sain, sizeof (sain)) < 0)
{
perror("sendto");
}
else
{
puts("ok\n");
}
return 0;
} |
Partager