/* * * Started 03-28-05 * * By Ferbal * * Email: ferbal2004@hotmail.com * * Raw Sockets... LINUX! * * * * */ /* Includes */ #include #include #include #include #include #include #include #include #include #include #include # define TH_SYN 0x02 int main(int argc, char **argv) { printf("****Packet Crafter Tool v1.0*******\n"); printf("********(In The Making....)********\n"); printf("******** By Ferbal ********\n"); printf("******** 03-29-05 Update ********\n"); printf("******** MUST BE ROOT ********\n"); printf("Email: ferbal2004@hotmail.com\n"); /* Check the arguments */ if(argc < 2) { printf("Execute:\n"); printf("./Program HOSTNAME/IP PORT\n"); exit(0); } /* VARIABLES */ int s; /* create socket */ char packet[5000]; /* Packet */ int i = 0; int bOpt = 1; int sockOpt; /* Structures */ struct ip *iph = (struct ip *)packet; struct udphdr *udph = (struct udphdr *) (packet + sizeof(struct ip)); struct tcphdr *tcph = (struct tcphdr *) (packet + sizeof(struct ip)); struct sockaddr_in sa; struct hostent *host; /* Create the raw socket */ s = socket(AF_INET, SOCK_RAW, IPPROTO_UDP); if (s == -1) { printf("Socket Creation Failed!\n"); exit(0); } if((host = gethostbyname(argv[1])) == NULL) { printf("Host resolve failed!\n"); exit(1); } sa.sin_family = AF_INET; sa.sin_port = htons(69); sa.sin_addr = *(struct in_addr *)host->h_addr; memset(&packet, 0, sizeof(packet)); /* IP/TCP header setup */ iph->ip_hl = 5; iph->ip_v = 4; iph->ip_tos = 0; iph->ip_len = sizeof(struct ip) + sizeof(struct udphdr); iph->ip_id = htonl(54012); iph->ip_off = 0; iph->ip_ttl = 255; iph->ip_p = 17; /* IPPROTO_UDP protocol number is 17, TCP is 6 */ iph->ip_sum = 0; iph->ip_src.s_addr = inet_addr("200.200.200.200"); iph->ip_dst.s_addr = sa.sin_addr.s_addr; /* TCP header setup */ tcph->source = htons(1234); tcph->dest = htons(81); tcph->seq = random(); tcph->ack_seq = 0; tcph->syn = TH_SYN; tcph->doff = 0; tcph->window = htonl(65535); tcph->check = 0; /* UDP header setup */ udph->source = htons(2043); udph->dest = htons(69); udph->len = 0; udph->check = 0; /* Calculate Checksum */ /* 3-29-05 Warnings when checksum is used: main.c:198: warning: type mismatch with previous implicit declaration main.c:149: warning: previous implicit declaration of `checksum' main.c:198: warning: `checksum' was previously implicitly declared to return `int' */ iph->ip_sum = checksum((unsigned short *) packet, iph->ip_len >> 1); /* ENABLE IPHDRINCL - for custom headers */ printf("socket: %d, OK\n", s); sockOpt = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&bOpt, sizeof(bOpt)); if (sockOpt < 0) { printf("setsockopt(IP_HDRINCL) failed"); exit(0); } printf("SocketOptSetting: %d, OK\n", sockOpt); for(i = 0; i < 10; i++) { /* Send The Packet */ int x; x = sendto(s, packet, iph->ip_len, 0x0, (struct sockaddr *)&sa, sizeof(sa)); if (x < 0) { printf("sendto() failed"); printf("%d\n", x); exit(0); } printf("Total size = %d, ", x); printf("Packet Sent...\n"); } printf("Success!\n"); return 0; } /*checksum taken from Mixter, others can be found as well, this is short though */ unsigned short checksum(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; }