-
-
Save AaronRuB1o/7dcbb1a6418f1b6da99ddbc60757bf9f to your computer and use it in GitHub Desktop.
Revisions
-
leonid-ed revised this gist
Nov 21, 2015 . 1 changed file with 0 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,21 +0,0 @@ -
leonid-ed renamed this gist
Nov 21, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
leonid-ed created this gist
Nov 21, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ The MIT License Copyright (c) 2015, Leonid Edrenkin. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,106 @@ /* An example of using raw sockets. You can capture packets by tcpdump: tcpdump -X -s0 -i lo -p udp */ #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <linux/ip.h> #include <linux/udp.h> #define PCKT_LEN 8192 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 (unsigned short)(~sum); } int main(int argc, char const *argv[]) { if (argc != 5) { printf("Error: Invalid parameters!\n"); printf("Usage: %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]); exit(1); } u_int16_t src_port, dst_port; u_int32_t src_addr, dst_addr; src_addr = inet_addr(argv[1]); dst_addr = inet_addr(argv[3]); src_port = atoi(argv[2]); dst_port = atoi(argv[4]); int sd; char buffer[PCKT_LEN]; struct iphdr *ip = (struct iphdr *) buffer; struct udphdr *udp = (struct udphdr *) (buffer + sizeof(struct iphdr)); struct sockaddr_in sin; int one = 1; const int *val = &one; memset(buffer, 0, PCKT_LEN); // create a raw socket with UDP protocol sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP); if (sd < 0) { perror("socket() error"); exit(2); } printf("OK: a raw socket is created.\n"); // inform the kernel do not fill up the packet structure, we will build our own if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0) { perror("setsockopt() error"); exit(2); } printf("OK: socket option IP_HDRINCL is set.\n"); sin.sin_family = AF_INET; sin.sin_port = htons(dst_port); sin.sin_addr.s_addr = inet_addr("127.0.0.1"); // fabricate the IP header ip->ihl = 5; ip->version = 4; ip->tos = 16; // low delay ip->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr); ip->id = htons(54321); ip->ttl = 64; // hops ip->protocol = 17; // UDP // source IP address, can use spoofed address here ip->saddr = src_addr; ip->daddr = dst_addr; // fabricate the UDP header udp->source = htons(src_port); // destination port number udp->dest = htons(dst_port); udp->len = htons(sizeof(struct udphdr)); // calculate the checksum for integrity ip->check = csum((unsigned short *)buffer, sizeof(struct iphdr) + sizeof(struct udphdr)); if (sendto(sd, buffer, ip->tot_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("sendto()"); exit(3); } printf("OK: one packet is sent.\n"); close(sd); return 0; } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,106 @@ /* An example of using raw sockets. You can capture packets by tcpdump: tcpdump -X -s0 -i eth0 -p udp */ #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <linux/ip.h> #include <linux/udp.h> #define PCKT_LEN 8192 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 (unsigned short)(~sum); } int main(int argc, char const *argv[]) { if (argc != 5) { printf("Error: Invalid parameters!\n"); printf("Usage: %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]); exit(1); } u_int16_t src_port, dst_port; u_int32_t src_addr, dst_addr; src_addr = inet_addr(argv[1]); dst_addr = inet_addr(argv[3]); src_port = atoi(argv[2]); dst_port = atoi(argv[4]); int sd; char buffer[PCKT_LEN]; struct iphdr *ip = (struct iphdr *) buffer; struct udphdr *udp = (struct udphdr *) (buffer + sizeof(struct iphdr)); struct sockaddr_in sin; int one = 1; const int *val = &one; memset(buffer, 0, PCKT_LEN); // create a raw socket with UDP protocol sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP); if (sd < 0) { perror("socket() error"); exit(2); } printf("OK: a raw socket is created.\n"); // inform the kernel do not fill up the packet structure, we will build our own if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0) { perror("setsockopt() error"); exit(2); } printf("OK: socket option IP_HDRINCL is set.\n"); sin.sin_family = AF_INET; sin.sin_port = htons(dst_port); sin.sin_addr.s_addr = dst_addr; // fabricate the IP header ip->ihl = 5; ip->version = 4; ip->tos = 16; // low delay ip->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr); ip->id = htons(54321); ip->ttl = 64; // hops ip->protocol = 17; // UDP // source IP address, can use spoofed address here ip->saddr = src_addr; ip->daddr = dst_addr; // fabricate the UDP header udp->source = htons(src_port); // destination port number udp->dest = htons(dst_port); udp->len = htons(sizeof(struct udphdr)); // calculate the checksum for integrity ip->check = csum((unsigned short *)buffer, sizeof(struct iphdr) + sizeof(struct udphdr)); if (sendto(sd, buffer, ip->tot_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("sendto()"); exit(3); } printf("OK: one packet is sent.\n"); close(sd); return 0; }