Advertisement
Guest User

rawupd.c

a guest
Oct 6th, 2015
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. // ----rawudp.c------
  2.  
  3. // Must be run by root lol! Just datagram, no payload/data
  4.  
  5. #include <unistd.h>
  6.  
  7. #include <stdio.h>
  8.  
  9. #include <sys/socket.h>
  10.  
  11. #include <netinet/ip.h>
  12.  
  13. #include <netinet/udp.h>
  14.  
  15. # include <string.h>
  16.  
  17. #include <stdlib.h>
  18.  
  19.  
  20.  
  21. // The packet length
  22.  
  23. #define PCKT_LEN 8192
  24.  
  25.  
  26.  
  27. // Can create separate header file (.h) for all headers' structure
  28.  
  29. // The IP header's structure
  30.  
  31. struct ipheader {
  32.  
  33. unsigned char iph_ihl:5, iph_ver:4;
  34.  
  35. unsigned char iph_tos;
  36.  
  37. unsigned short int iph_len;
  38.  
  39. unsigned short int iph_ident;
  40.  
  41. unsigned char iph_flag;
  42.  
  43. unsigned short int iph_offset;
  44.  
  45. unsigned char iph_ttl;
  46.  
  47. unsigned char iph_protocol;
  48.  
  49. unsigned short int iph_chksum;
  50.  
  51. unsigned int iph_sourceip;
  52.  
  53. unsigned int iph_destip;
  54.  
  55. };
  56.  
  57.  
  58.  
  59. // UDP header's structure
  60.  
  61. struct udpheader {
  62.  
  63. unsigned short int udph_srcport;
  64.  
  65. unsigned short int udph_destport;
  66.  
  67. unsigned short int udph_len;
  68.  
  69. unsigned short int udph_chksum;
  70.  
  71. };
  72.  
  73. // total udp header length: 8 bytes (=64 bits)
  74.  
  75.  
  76.  
  77. // Function for checksum calculation. From the RFC,
  78.  
  79. // the checksum algorithm is:
  80.  
  81. // "The checksum field is the 16 bit one's complement of the one's
  82.  
  83. // complement sum of all 16 bit words in the header. For purposes of
  84.  
  85. // computing the checksum, the value of the checksum field is zero."
  86.  
  87. unsigned short csum(unsigned short *buf, int nwords)
  88.  
  89. { //
  90.  
  91. unsigned long sum;
  92.  
  93. for(sum=0; nwords>0; nwords--)
  94.  
  95. sum += *buf++;
  96.  
  97. sum = (sum >> 16) + (sum &0xffff);
  98.  
  99. sum += (sum >> 16);
  100.  
  101. return (unsigned short)(~sum);
  102.  
  103. }
  104.  
  105. // Source IP, source port, target IP, target port from the command line arguments
  106.  
  107. int main(int argc, char *argv[])
  108.  
  109. {
  110.  
  111. int sd;
  112.  
  113. // No data/payload just datagram
  114.  
  115. char buffer[PCKT_LEN];
  116.  
  117. // Our own headers' structures
  118.  
  119. struct ipheader *ip = (struct ipheader *) buffer;
  120.  
  121. struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct ipheader));
  122.  
  123. // Source and destination addresses: IP and port
  124.  
  125. struct sockaddr_in sin, din;
  126.  
  127. int one = 1;
  128.  
  129. const int *val = &one;
  130.  
  131.  
  132.  
  133. memset(buffer, 0, PCKT_LEN);
  134.  
  135.  
  136.  
  137. if(argc != 5)
  138.  
  139. {
  140.  
  141. printf("- Invalid parameters!!!\n");
  142.  
  143. printf("- Usage %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]);
  144.  
  145. exit(-1);
  146.  
  147. }
  148.  
  149.  
  150.  
  151. // Create a raw socket with UDP protocol
  152.  
  153. sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
  154.  
  155. if(sd < 0)
  156.  
  157. {
  158.  
  159. perror("socket() error");
  160.  
  161. // If something wrong just exit
  162.  
  163. exit(-1);
  164.  
  165. }
  166.  
  167. else
  168.  
  169. printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");
  170.  
  171.  
  172.  
  173. // The source is redundant, may be used later if needed
  174.  
  175. // The address family
  176.  
  177. sin.sin_family = AF_INET;
  178.  
  179. din.sin_family = AF_INET;
  180.  
  181. // Port numbers
  182.  
  183. sin.sin_port = htons(atoi(argv[2]));
  184.  
  185. din.sin_port = htons(atoi(argv[4]));
  186.  
  187. // IP addresses
  188.  
  189. sin.sin_addr.s_addr = inet_addr(argv[1]);
  190.  
  191. din.sin_addr.s_addr = inet_addr(argv[3]);
  192.  
  193.  
  194.  
  195. // Fabricate the IP header or we can use the
  196.  
  197. // standard header structures but assign our own values.
  198.  
  199. ip->iph_ihl = 5;
  200.  
  201. ip->iph_ver = 4;
  202.  
  203. ip->iph_tos = 16; // Low delay
  204.  
  205. ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader);
  206.  
  207. ip->iph_ident = htons(54321);
  208.  
  209. ip->iph_ttl = 64; // hops
  210.  
  211. ip->iph_protocol = 17; // UDP
  212.  
  213. // Source IP address, can use spoofed address here!!!
  214.  
  215. ip->iph_sourceip = inet_addr(argv[1]);
  216.  
  217. // The destination IP address
  218.  
  219. ip->iph_destip = inet_addr(argv[3]);
  220.  
  221.  
  222.  
  223. // Fabricate the UDP header. Source port number, redundant
  224.  
  225. udp->udph_srcport = htons(atoi(argv[2]));
  226.  
  227. // Destination port number
  228.  
  229. udp->udph_destport = htons(atoi(argv[4]));
  230.  
  231. udp->udph_len = htons(sizeof(struct udpheader));
  232.  
  233. // Calculate the checksum for integrity
  234.  
  235. ip->iph_chksum = csum((unsigned short *)buffer, sizeof(struct ipheader) + sizeof(struct udpheader));
  236.  
  237. // Inform the kernel do not fill up the packet structure. we will build our own...
  238.  
  239. if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
  240.  
  241. {
  242.  
  243. perror("setsockopt() error");
  244.  
  245. exit(-1);
  246.  
  247. }
  248.  
  249. else
  250.  
  251. printf("setsockopt() is OK.\n");
  252.  
  253.  
  254.  
  255. // Send loop, send for every 2 second for 100 count
  256.  
  257. printf("Trying...\n");
  258.  
  259. printf("Using raw socket and UDP protocol\n");
  260.  
  261. printf("Using Source IP: %s port: %u, Target IP: %s port: %u.\n", argv[1], atoi(argv[2]), argv[3], atoi(argv[4]));
  262.  
  263.  
  264.  
  265. int count;
  266.  
  267. for(count = 1; count <=20; count++)
  268.  
  269. {
  270.  
  271. if(sendto(sd, buffer, ip->iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
  272.  
  273. // Verify
  274.  
  275. {
  276.  
  277. perror("sendto() error");
  278.  
  279. exit(-1);
  280.  
  281. }
  282.  
  283. else
  284.  
  285. {
  286.  
  287. printf("Count #%u - sendto() is OK.\n", count);
  288.  
  289. sleep(2);
  290.  
  291. }
  292.  
  293. }
  294.  
  295. close(sd);
  296.  
  297. return 0;
  298.  
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement