From e75179f41c5d50dd62c60f572182b8178ed03e94 Mon Sep 17 00:00:00 2001 From: Ryan Schanzenbacher Date: Wed, 8 Mar 2023 16:26:08 -0500 Subject: Added new ping trigger programs Two executables along with the C code exist: - ping3.c - Source code - ping3 - Linux executable - ping_send - FreeBSD executable (for pfSense) --- ping3 | Bin 0 -> 22704 bytes ping3.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ping_send | Bin 0 -> 6113864 bytes 3 files changed, 110 insertions(+) create mode 100755 ping3 create mode 100644 ping3.c create mode 100644 ping_send diff --git a/ping3 b/ping3 new file mode 100755 index 0000000..edb585c Binary files /dev/null and b/ping3 differ diff --git a/ping3.c b/ping3.c new file mode 100644 index 0000000..f0dd858 --- /dev/null +++ b/ping3.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +unsigned short cksum(unsigned short *addr, int len); + +int main(int argc, char *argv[]) { + int sock; + char send_buf[400], src_ip[15], dst_ip[15], src_name[256]; + struct ip *ip = (struct ip *)send_buf; + struct icmp *icmp = (struct icmp *)(ip + 1); + struct hostent *src_hp, *dst_hp; + struct sockaddr_in src, dst; + int on = 1; + memset(send_buf, 0, sizeof(send_buf)); + + if (argc < 2) { + printf("Need arg. I\n"); + exit(EXIT_FAILURE); + } + + /**if (getuid() == 0) { + fprintf(stderr, "Need to elevate\n"); + exit(EXIT_FAILURE); + } **/ + + gethostname(src_name, sizeof(src_name)); + printf("%s\n", src_name); + src_hp = gethostbyname(src_name); + ip->ip_src = (*(struct in_addr *)src_hp->h_addr_list[0]); + + dst_hp = gethostbyname(argv[1]); + ip->ip_dst = (*(struct in_addr *)dst_hp->h_addr); + dst.sin_addr = (*(struct in_addr *)dst_hp->h_addr); + + sprintf(src_ip, "%s", inet_ntoa(ip->ip_src)); + sprintf(dst_ip, "%s", inet_ntoa(ip->ip_dst)); + printf("Src: %s -- Dst: %s\n", src_ip, dst_ip); + + // Create socket + sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); + + setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)); + + // IP Structure + ip->ip_v = 4; + ip->ip_hl = 5; + ip->ip_tos = 0; + ip->ip_len = htons(sizeof(send_buf)); + ip->ip_id = htons(321); + ip->ip_off = htons(0); + ip->ip_ttl = 255; + ip->ip_p = IPPROTO_ICMP; + ip->ip_sum = 0; + + // ICMP Structure + icmp->icmp_type = 2; + icmp->icmp_code = 0; + + dst.sin_family = AF_INET; + + ip->ip_sum = cksum((unsigned short *)send_buf, ip->ip_hl); + icmp->icmp_cksum = cksum((unsigned short *)icmp, sizeof(send_buf) - sizeof(struct icmp)); + + int dst_addr_len = sizeof(dst); + int bytes_sent; + + if((bytes_sent = sendto(sock, send_buf, sizeof(send_buf), 0, (struct sockaddr *)&dst, dst_addr_len)) < 0) { + perror("send err"); + fflush(stdout); + } + else { + printf("Sent %d bytes\n", bytes_sent); + } + + +} + +unsigned short cksum(unsigned short *addr, int len) { + int nleft = len; + int sum = 0; + unsigned short *w = addr; + unsigned short answer = 0; + + while (nleft > 1) { + sum += *w++; + nleft -= 2; + } + + if (nleft == 1) { + *(unsigned char *)(&answer) = *(unsigned char *)w; + sum += answer; + } + + sum = (sum >> 16) + (sum & 0xffff); + sum += (sum >> 16); + answer = ~sum; + + return answer; +} diff --git a/ping_send b/ping_send new file mode 100644 index 0000000..37a2396 Binary files /dev/null and b/ping_send differ -- cgit v1.2.3