Skip to content

Instantly share code, notes, and snippets.

@magisterquis
Last active November 21, 2022 21:26
Show Gist options
  • Save magisterquis/e98038e833543dcb59173d813d7c35d8 to your computer and use it in GitHub Desktop.
Save magisterquis/e98038e833543dcb59173d813d7c35d8 to your computer and use it in GitHub Desktop.
Hello, World! using pcap_inject.
/*
* pcaphelloworld.c
* Simple libpcap program
* By J. Stuart McMurray
* Created 20190527
* Last Modified 20190527
*/
#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <err.h>
#include <pcap.h>
#include <string.h>
/* HELLO is the message to put in an ethernet frame. */
#define HELLO "Hello, World!"
/* Make sure we have a capture device */
#ifndef DEVICE
#error DEVICE is not defined
#endif /* #ifdef DEVICE */
__attribute__((constructor)) void
send_hello(void)
{
char errbuf[PCAP_ERRBUF_SIZE + 1];
pcap_t *p;
uint8_t buf[sizeof(struct ether_header) + strlen(HELLO)];
struct ether_header *ep;
bzero(errbuf, sizeof(errbuf));
bzero(buf, sizeof(buf));
/* Roll a frame */
ep = (struct ether_header *)buf;
memcpy(ep->ether_shost, "\x73\x74\x75\x61\x72\x74",
sizeof(ep->ether_shost) <= 6 ?
sizeof(ep->ether_shost) : 6);
memcpy(ep->ether_dhost, "\xff\xff\xff\xff\xff\xff",
sizeof(ep->ether_dhost) <= 6 ?
sizeof(ep->ether_dhost) : 6);
ep->ether_type = htons(0x0804); /* CHAOS */
memcpy(buf + sizeof(struct ether_header), HELLO, strlen(HELLO));
/* Open the device */
if (NULL == (p = pcap_open_live(DEVICE, 65535, 0, 10, errbuf))) {
warn("pcap_open_live: %s", errbuf);
return;
}
/* Send the message */
if (sizeof(buf) != pcap_inject(p, buf, sizeof(buf))) {
warn("pcap_inject: %s", pcap_geterr(p));
}
/* Clean up */
pcap_close(p);
}
int
main(int argc, char **argv)
{
/* send_hello will be called already, so no need to call it here. */
return 0;
}
#!/bin/sh
wget -nv https://www.tcpdump.org/release/libpcap-1.9.0.tar.gz
tar xzf libpcap-1.9.0.tar.gz && cd libpcap-1.9.0 && \
./configure >/dev/null && \
make >/dev/null && cd ..
wget -nv -O pcaphelloworld.c https://git.io/fjRW9
more pcaphelloworld.c
cc -DDEVICE=\"eth0\" -Ilibpcap-1.9.0 \
-O2 --pedantic -Wall \
pcaphelloworld.c libpcap-1.9.0/libpcap.a -o pcaphelloworld
ls -l pcaphelloworld && \
file pcaphelloworld && \
ldd pcaphelloworld
cc -DDEVICE=\"eth0\" -Ilibpcap-1.9.0 -Llibpcap-1.9.0 \
-O2 --pedantic -Wall \
pcaphelloworld.c -o libpcaphelloworld.so \
-Wl,-Bstatic -lpcap -Wl,-Bdynamic -fPIC -shared
ls -l libpcaphelloworld.so && \
file libpcaphelloworld.so && \
ldd libpcaphelloworld.so
sudo tcpdump -elnni eth0 -X not ip and not ip6 &
sudo ./pcaphelloworld
echo 'print __libc_dlopen_mode("/home/stuart/libpcaphelloworld.so", 2)' | sudo gdb -p 1
sudo grep pcaphelloworld /proc/1/maps
echo 'print __libc_dlclose()' | sudo gdb -p 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment