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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/ip.h>
#define __FAVOR_BSD
#include <netinet/tcp.h>
struct fake_tcphdr {
struct in_addr source;
struct in_addr destination;
unsigned char useless;
unsigned char protocol;
unsigned short int length;
struct tcphdr tcp;
};
#ifndef __u16
#define __u16 u_int16_t
#endif
#ifndef __u32
#define __u32 u_int32_t
#endif
__u16 checksum(__u16 *buffer, int nbytes);
int main (void) {
srand(time(NULL));
struct ip ip;
struct tcphdr tcp;
struct sockaddr_in sin;
struct fake_tcphdr ftcp;
int sock;
const int on = 1;
u_char *packet;
packet = (u_char*)malloc(sizeof(struct ip) + sizeof(struct tcphdr));
ip.ip_hl = sizeof(struct ip) / sizeof(unsigned long);
ip.ip_v = 4;
ip.ip_tos = 0;
ip.ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
ip.ip_id = htons((rand() % (65535 - 1 + 1)) + 1);
ip.ip_off = 0;
ip.ip_ttl = 255;
ip.ip_p = 6;
ip.ip_sum = 0;
ip.ip_src.s_addr = inet_addr("127.0.0.1");
ip.ip_dst.s_addr = inet_addr("127.0.0.1");
ip.ip_sum = checksum((__u16*)&ip, sizeof(ip));
tcp.th_sport = htons((rand() % (65535 - 1 + 1) + 1));
tcp.th_dport = htons(0);
tcp.th_seq = 0;
tcp.th_off = sizeof(struct tcphdr) / 4;
tcp.th_flags = 0x02;
tcp.th_win = htons(512);
tcp.th_sum = 0;
ftcp.source = ip.ip_src;
ftcp.destination = ip.ip_dst;
ftcp.useless = 0;
ftcp.protocol = IPPROTO_TCP;
ftcp.length = htons(sizeof(struct tcphdr));
ftcp.tcp = tcp;
tcp.th_sum = checksum((__u16*)&ftcp, sizeof(ftcp));
memcpy(packet, &ip, sizeof(ip));
memcpy(packet + sizeof(ip), &tcp, sizeof(tcp));
if ((sock = socket(PF_INET, SOCK_RAW, IPPROTO_TCP)) < 0) {
perror("Socket");
exit(1);
}
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) {
perror("Hdrincl");
exit(1);
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = PF_INET;
sin.sin_addr.s_addr = ip.ip_dst.s_addr;
if (sendto(sock, packet, sizeof(struct ip) + sizeof(struct tcphdr), 0, (struct sockaddr*)&sin, sizeof(struct sockaddr)) < 0) {
perror("Send to");
exit(1);
}
memset(&packet, 0, sizeof(packet));
free(packet);
return 0;
}
__u16 checksum (__u16 *buffer, int nbytes) {
__u32 sum;
__u16 oddbyte;
sum = 0;
while (nbytes > 1) {
sum += *buffer++;
nbytes -= 2;
}
if (nbytes == 1) {
oddbyte = 0;
*((__u16*)&oddbyte) = *(__u16*)buffer;
sum += oddbyte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return (__u16)~sum;
} |
Partager