Compare commits

...

15 Commits

6 changed files with 292 additions and 94 deletions

View File

@@ -3,9 +3,9 @@ stages:
- build - build
- publish - publish
variables: variables:
DEBIAN_FRONTEND: noninteractive DEBIAN_FRONTEND: noninteractive
VERSION: 0.5
build-bookworm: build-bookworm:
stage: build stage: build
@@ -15,7 +15,7 @@ build-bookworm:
tags: tags:
- bookworm - bookworm
script: script:
- tar -czf ../pcapmirror_0.3.orig.tar.gz --exclude=debian . - tar -czf ../pcapmirror_$VERSION.orig.tar.gz --exclude=debian .
- apt-get update && apt-get install -y libpcap-dev - apt-get update && apt-get install -y libpcap-dev
- dpkg-buildpackage -uc -us - dpkg-buildpackage -uc -us
- mkdir -p build - mkdir -p build
@@ -38,8 +38,7 @@ publish-bookworm:
script: script:
- apt-get update && apt-get install -y curl - apt-get update && apt-get install -y curl
- ls -la build - ls -la build
- 'echo "Uploading to: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/bookworm/pcapmirror_0.3-1_amd64.deb"' - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror_$VERSION-1_amd64.deb ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/bookworm/pcapmirror_$VERSION-1_amd64.deb'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror_0.3-1_amd64.deb ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/bookworm/pcapmirror_0.3-1_amd64.deb'
build-sid: build-sid:
stage: build stage: build
@@ -49,7 +48,7 @@ build-sid:
tags: tags:
- sid - sid
script: script:
- tar -czf ../pcapmirror_0.3.orig.tar.gz --exclude=debian . - tar -czf ../pcapmirror_$VERSION.orig.tar.gz --exclude=debian .
- apt-get update && apt-get install -y libpcap-dev - apt-get update && apt-get install -y libpcap-dev
- dpkg-buildpackage -uc -us - dpkg-buildpackage -uc -us
- mkdir -p build - mkdir -p build
@@ -72,8 +71,7 @@ publish-sid:
script: script:
- apt-get update && apt-get install -y curl - apt-get update && apt-get install -y curl
- ls -la build - ls -la build
- 'echo "Uploading to: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/sid/pcapmirror_0.3-1_amd64.deb"' - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror_$VERSION-1_amd64.deb ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/sid/pcapmirror_$VERSION-1_amd64.deb'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror_0.3-1_amd64.deb ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/sid/pcapmirror_0.3-1_amd64.deb'
build-rocky9: build-rocky9:
stage: build stage: build
@@ -85,7 +83,7 @@ build-rocky9:
script: script:
- dnf install -y libpcap-devel - dnf install -y libpcap-devel
- mkdir -p /root/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS} - mkdir -p /root/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
- tar -czf /root/rpmbuild/SOURCES/pcapmirror-v0.3.tar.gz --exclude=debian --exclude=.git . - tar -czf /root/rpmbuild/SOURCES/pcapmirror-v$VERSION.tar.gz --exclude=debian --exclude=.git .
- cp -r * /root/rpmbuild/BUILD - cp -r * /root/rpmbuild/BUILD
- rpmbuild -ba pcapmirror.spec - rpmbuild -ba pcapmirror.spec
- mkdir -p build - mkdir -p build
@@ -109,8 +107,7 @@ publish-rocky9:
script: script:
- apt-get update && apt-get install -y curl - apt-get update && apt-get install -y curl
- ls -la build - ls -la build
- 'echo "Uploading to: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/rockylinux9/pcapmirror-0.3-1.el8.x86_64.rpm"' - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror-$VERSION-*.el9.x86_64.rpm ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/rockylinux9/pcapmirror-$VERSION-1.el8.x86_64.rpm'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror-0.3-*.el9.x86_64.rpm ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/rockylinux9/pcapmirror-0.3-1.el8.x86_64.rpm'
build-rocky8: build-rocky8:
stage: build stage: build
@@ -122,7 +119,7 @@ build-rocky8:
script: script:
- dnf install -y libpcap-devel - dnf install -y libpcap-devel
- mkdir -p /root/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS} - mkdir -p /root/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
- tar -czf /root/rpmbuild/SOURCES/pcapmirror-v0.3.tar.gz --exclude=debian --exclude=.git . - tar -czf /root/rpmbuild/SOURCES/pcapmirror-v$VERSION.tar.gz --exclude=debian --exclude=.git .
- cp -r * /root/rpmbuild/BUILD - cp -r * /root/rpmbuild/BUILD
- rpmbuild -ba pcapmirror.spec - rpmbuild -ba pcapmirror.spec
- mkdir -p build - mkdir -p build
@@ -146,8 +143,7 @@ publish-rocky8:
script: script:
- apt-get update && apt-get install -y curl - apt-get update && apt-get install -y curl
- ls -la build - ls -la build
- 'echo "Uploading to: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/rockylinux8/pcapmirror-0.3-1.el8.x86_64.rpm"' - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror-$VERSION-*.el8.x86_64.rpm ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/rockylinux8/pcapmirror-$VERSION-1.el8.x86_64.rpm'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror-0.3-*.el8.x86_64.rpm ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/rockylinux8/pcapmirror-0.3-1.el8.x86_64.rpm'
build-pios12: build-pios12:
stage: build stage: build
@@ -157,7 +153,7 @@ build-pios12:
tags: tags:
- pios12 - pios12
script: script:
- tar -czf ../pcapmirror_0.3.orig.tar.gz --exclude=debian . - tar -czf ../pcapmirror_$VERSION.orig.tar.gz --exclude=debian .
- apt-get update && apt-get install -y libpcap-dev - apt-get update && apt-get install -y libpcap-dev
- dpkg-buildpackage -uc -us - dpkg-buildpackage -uc -us
- mkdir -p build - mkdir -p build
@@ -180,5 +176,37 @@ publish-pios12:
script: script:
- apt-get update && apt-get install -y curl - apt-get update && apt-get install -y curl
- ls -la build - ls -la build
- 'echo "Uploading to: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/bookworm/pcapmirror_0.3-1_armhf.deb"' - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror_$VERSION-1_armhf.deb ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/bookworm/pcapmirror_$VERSION-1_armhf.deb'
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror_0.3-1_armhf.deb ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/bookworm/pcapmirror_0.3-1_armhf.deb'
build-pios12-64:
stage: build
needs: []
only:
- tags
tags:
- pios12-64
script:
- tar -czf ../pcapmirror_$VERSION.orig.tar.gz --exclude=debian .
- apt-get update && apt-get install -y libpcap-dev
- dpkg-buildpackage -uc -us
- mkdir -p build
- mv ../pcapmirror*.* build/
artifacts:
paths:
- build
publish-pios12-64:
stage: publish
needs:
- build-pios12-64
dependencies:
- build-pios12-64
only:
- tags
tags:
- bookworm
script:
- apt-get update && apt-get install -y curl
- ls -la build
- 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file build/pcapmirror_$VERSION-1_arm64.deb ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/pcapmirror/bookworm/pcapmirror_$VERSION-1_arm64.deb'

View File

@@ -18,7 +18,9 @@ pcapmirror [options]
* -p <port> Specify the destination port (default: 37008) * -p <port> Specify the destination port (default: 37008)
* -4 Force IPv4 host lookup * -4 Force IPv4 host lookup
* -6 Force IPv6 host lookup * -6 Force IPv6 host lookup
* -l List available network interfaces.
* -v Enable verbose mode * -v Enable verbose mode
* -c Count matching packets (overrides verbose mode)
* -h Show this help message * -h Show this help message
### Example: ### Example:
@@ -36,8 +38,25 @@ With this tool, you can mirror traffic directly to a running [Wireshark](https:/
To avoid capturing traffic from your own monitoring machine, configure Wireshark with a capture filter of udp port 37008 or udp dst port 37008. Also, verify that your firewall permits this UDP traffic. To avoid capturing traffic from your own monitoring machine, configure Wireshark with a capture filter of udp port 37008 or udp dst port 37008. Also, verify that your firewall permits this UDP traffic.
## Original Download Location
[https://git.freestone.net/cramer/pcapmirror](https://git.freestone.net/cramer/pcapmirror)
## Packages
On the original download location you will also find several prebuilt packages.
## Compile and Install ## Compile and Install
### Supported Operating Systems
Source is tested to build and function on the following operating systems
* Debian Linux 12 + unstable (sid)
* Rocky Linux 8 + 9
* PiOS 12 (bookworm)
* OpenBSD 7.6
* MacOS 15
Compile the program: Compile the program:
```bash ```bash
make make

17
debian/changelog vendored
View File

@@ -1,3 +1,20 @@
pcapmirror (0.5-1) unstable; urgency=medium
* new option -c to count matching packets (overrides verbose mode)
* reworked packet decoder to also decode arp, vlan and qinq packets
* well known protocols numbers are now decoded
* works now on MacOS and OpenBSD
-- Matthias Cramer <cramer@freestone.net> Fri, 29 Mar 2025 13:40:00 +0100
pcapmirror (0.4-1) unstable; urgency=medium
* IPv6 support for remote destination
* remote destination can now also be hostname
* added option to enforce IPv4 and IPv6 for remote destination
-- Matthias Cramer <cramer@freestone.net> Fri, 24 Mar 2025 13:40:00 +0100
pcapmirror (0.3-1) unstable; urgency=medium pcapmirror (0.3-1) unstable; urgency=medium
* added manpage * added manpage

271
main.c
View File

@@ -6,21 +6,24 @@ Copyright (c) 2025, Matthias Cramer, cramer@freestone.net
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <pcap.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <pcap.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h> #include <netinet/ip6.h>
#include <net/if_arp.h>
#include <netinet/if_ether.h> // For Ethernet and ARP headers
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#define DEFAULT_DEST_PORT 37008 // Default TZSP port #define DEFAULT_DEST_PORT 37008 // Default TZSP port
#define TZSP_ENCAP_LEN 4 // Length of TZSP encapsulation header #define TZSP_ENCAP_LEN 4 // Length of TZSP encapsulation header
#define TZSP_TAGGED_LEN 1 // Length of TZSP tagged field header (type) #define TZSP_TAGGED_LEN 1 // Length of TZSP tagged field header (type)
#define ETHERNET_HEADER_LENGTH 14 #define ETHERNET_HEADER_LENGTH 14 // Assuming Ethernet header is 14 bytes
// TZSP Header Structure // TZSP Header Structure
struct tzsp_header { struct tzsp_header {
@@ -35,12 +38,80 @@ struct tzsp_tagged {
unsigned char type; // Tag type unsigned char type; // Tag type
}; };
// Add this structure for ARP header parsing
struct arp_header {
uint16_t htype; // Hardware type
uint16_t ptype; // Protocol type
uint8_t hlen; // Hardware address length
uint8_t plen; // Protocol address length
uint16_t oper; // Operation (1 = request, 2 = reply)
uint8_t sha[6]; // Sender hardware address
uint8_t spa[4]; // Sender protocol address
uint8_t tha[6]; // Target hardware address
uint8_t tpa[4]; // Target protocol address
};
// Function to check if the system is little-endian // Function to check if the system is little-endian
int is_little_endian() { int is_little_endian() {
volatile unsigned int i=0x01234567; volatile unsigned int i=0x01234567;
return (((unsigned char*)&i)[0] == 0x67); return (((unsigned char*)&i)[0] == 0x67);
} }
void list_interfaces() {
pcap_if_t *alldevs;
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&alldevs, errbuf) == -1) {
fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf);
return;
}
printf("Available network interfaces:\n");
for (pcap_if_t *d = alldevs; d != NULL; d = d->next) {
printf("%s", d->name);
if (d->description) {
printf(" (%s)", d->description);
}
printf("\n");
}
pcap_freealldevs(alldevs);
}
// Function to lookup protocol name or return protocol number as a string
const char *lookup_protocol_name(int protocol) {
static char buf[5]; // Buffer to hold protocol number as a string
switch (protocol) {
case 1:
return "ICMP";
case 2:
return "IGMP";
case 6:
return "TCP";
case 17:
return "UDP";
case 41:
return "IPv6";
case 47:
return "GRE";
case 50:
return "ESP";
case 51:
return "AH";
case 58:
return "ICMPv6";
case 89:
return "OSPF";
case 112:
return "VRRP";
case 124:
return "ISIS";
case 132:
return "SCTP";
default:
snprintf(buf, sizeof(buf), "%d", protocol); // Convert protocol number to string
return buf;
}
}
void print_usage(const char *program_name) { void print_usage(const char *program_name) {
printf("Usage: %s [options]\n", program_name); printf("Usage: %s [options]\n", program_name);
printf("Options:\n"); printf("Options:\n");
@@ -50,16 +121,17 @@ void print_usage(const char *program_name) {
printf(" -p <port> Specify the destination port (default: %d)\n", DEFAULT_DEST_PORT); printf(" -p <port> Specify the destination port (default: %d)\n", DEFAULT_DEST_PORT);
printf(" -4 Force IPv4 host lookup\n"); printf(" -4 Force IPv4 host lookup\n");
printf(" -6 Force IPv6 host lookup\n"); printf(" -6 Force IPv6 host lookup\n");
printf(" -l List available network interfaces\n");
printf(" -v Enable verbose mode\n"); printf(" -v Enable verbose mode\n");
printf(" -c Count matching packets (overrides verbose mode)\n");
printf(" -h Show this help message\n"); printf(" -h Show this help message\n");
printf("Example:\n"); printf("Example:\n");
printf(" %s -i eth0 -f 'tcp port 80' -v -r 192.168.1.100 -p 47008\n", program_name); printf(" %s -i eth0 -f 'tcp port 80' -v -r 192.168.1.100 -p 47008\n", program_name);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
pcap_if_t *alldevs;
char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE];
char *filter_exp = "tcp port 8088"; // Default filter char *filter_exp = ""; // Default filter
char *dev_name = NULL; // Device name char *dev_name = NULL; // Device name
char *mirror_host = NULL; // Destination IP, no default value char *mirror_host = NULL; // Destination IP, no default value
int dest_port = DEFAULT_DEST_PORT; // Destination port, default value int dest_port = DEFAULT_DEST_PORT; // Destination port, default value
@@ -67,11 +139,17 @@ int main(int argc, char *argv[]) {
int verbose = 0; // Verbose flag, default is false int verbose = 0; // Verbose flag, default is false
int force_ipv4 = 0; // Flag to force IPv4 lookup int force_ipv4 = 0; // Flag to force IPv4 lookup
int force_ipv6 = 0; // Flag to force IPv6 lookup int force_ipv6 = 0; // Flag to force IPv6 lookup
int list_interfaces_flag = 0; // Flag to list interfaces
// Add a variable to track the count of matching packets
int count_packets = 0; // Flag for counting packets
unsigned long long int packet_count = 0; // Counter for matching packets (64bit)
// Socket variables // Socket variables
int sockfd; int sockfd;
struct addrinfo hints, *res; struct addrinfo hints, *res;
struct sockaddr_storage dest_addr; // Declare dest_addr struct sockaddr_storage dest_addr; // Declare dest_addr
int dest_addr_size;
// Check if no arguments are given or if help is requested // Check if no arguments are given or if help is requested
if (argc == 1 || (argc == 2 && strcmp(argv[1], "-h") == 0)) { if (argc == 1 || (argc == 2 && strcmp(argv[1], "-h") == 0)) {
@@ -102,16 +180,32 @@ int main(int argc, char *argv[]) {
force_ipv4 = 1; // Force IPv4 lookup force_ipv4 = 1; // Force IPv4 lookup
} else if (strcmp(argv[i], "-6") == 0) { } else if (strcmp(argv[i], "-6") == 0) {
force_ipv6 = 1; // Force IPv6 lookup force_ipv6 = 1; // Force IPv6 lookup
} else if (strcmp(argv[i], "-l") == 0) {
list_interfaces_flag = 1; // Set flag to list interfaces
} else if (strcmp(argv[i], "-c") == 0) {
count_packets = 1; // Enable packet counting
verbose = 0; // Disable verbose mode if -c is set
} }
} }
if (list_interfaces_flag) {
list_interfaces();
return 0;
}
// Check if destination IP is provided // Check if destination IP is provided
if (mirror_host == NULL) { if (mirror_host == NULL && !list_interfaces_flag) {
fprintf(stderr, "Error: Destination IP address is required.\n"); fprintf(stderr, "Error: Destination IP address is required.\n");
print_usage(argv[0]); print_usage(argv[0]);
return 1; return 1;
} }
// Check that interface is not any
if (dev_name != NULL && strcmp(dev_name, "any") == 0) {
fprintf(stderr, "Error: Interface 'any' is not supported.\n");
return 1;
}
// Resolve the destination address // Resolve the destination address
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // Allow IPv4 or IPv6 hints.ai_family = AF_UNSPEC; // Allow IPv4 or IPv6
@@ -128,6 +222,17 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
// Calculate dest_addr size
if (res->ai_family == AF_INET) {
dest_addr_size = sizeof(struct sockaddr_in);
} else if (res->ai_family == AF_INET6) {
dest_addr_size = sizeof(struct sockaddr_in6);
} else {
fprintf(stderr, "Unknown address family\n");
freeaddrinfo(res);
return 1;
}
// Create UDP socket // Create UDP socket
sockfd = socket(res->ai_family, SOCK_DGRAM, 0); sockfd = socket(res->ai_family, SOCK_DGRAM, 0);
if (sockfd == -1) { if (sockfd == -1) {
@@ -136,6 +241,8 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
memset(&dest_addr, 0, sizeof(dest_addr));
// Set the destination address // Set the destination address
if (res->ai_family == AF_INET) { if (res->ai_family == AF_INET) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr; struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
@@ -165,34 +272,6 @@ int main(int argc, char *argv[]) {
printf("Resolved Destination IP: %s\n", resolved_ip); printf("Resolved Destination IP: %s\n", resolved_ip);
printf("Destination Port: %d\n", dest_port); printf("Destination Port: %d\n", dest_port);
// If no interface is specified, find all devices
if (dev_name == NULL) {
if (pcap_findalldevs(&alldevs, errbuf) == -1) {
fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf);
return(1);
}
// Print the available devices for debugging
/*
pcap_if_t *device;
printf("Available devices:\n");
for (device = alldevs; device != NULL; device = device->next) {
printf("%s - %s\n", device->name, (device->description != NULL) ? device->description : "No description available");
}
*/
// Use the first device if no device is specified
if (alldevs == NULL) {
fprintf(stderr, "No devices found. Make sure you have permissions to capture traffic.\n");
return 1;
}
dev_name = alldevs->name; // Use the name of the first device
} else {
// Interface specified via command line, no need to find all devices
alldevs = NULL; // Set alldevs to NULL to avoid potential issues
}
pcap_t *handle; pcap_t *handle;
struct bpf_program fp; struct bpf_program fp;
bpf_u_int32 mask; bpf_u_int32 mask;
@@ -204,29 +283,20 @@ int main(int argc, char *argv[]) {
mask = 0; mask = 0;
} }
handle = pcap_open_live(dev_name, BUFSIZ, 1, 1000, errbuf); handle = pcap_open_live(dev_name, BUFSIZ, 1, 100, errbuf);
if (handle == NULL) { if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev_name, errbuf); fprintf(stderr, "Couldn't open device %s: %s\n", dev_name, errbuf);
if (alldevs != NULL) {
pcap_freealldevs(alldevs);
}
return(2); return(2);
} }
if (pcap_compile(handle, &fp, filter_exp, 1, net) == -1) { if (pcap_compile(handle, &fp, filter_exp, 1, net) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle)); fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
if (alldevs != NULL) {
pcap_freealldevs(alldevs);
}
pcap_close(handle); pcap_close(handle);
return(2); return(2);
} }
if (pcap_setfilter(handle, &fp) == -1) { if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle)); fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
if (alldevs != NULL) {
pcap_freealldevs(alldevs);
}
pcap_close(handle); pcap_close(handle);
return(2); return(2);
} }
@@ -237,39 +307,91 @@ int main(int argc, char *argv[]) {
struct ip *ip_header; // Declare ip4_header struct ip *ip_header; // Declare ip4_header
struct ip6_hdr *ip6_header; // Declare ip6_header struct ip6_hdr *ip6_header; // Declare ip6_header
int ip_protocol = 0; int ip_protocol = 0;
struct timeval current_time, last_count;
gettimeofday(&last_count, NULL);
printf("\n");
while (1) { while (1) {
packet = pcap_next(handle, &header); packet = pcap_next(handle, &header);
if (packet == NULL) if (packet == NULL)
continue; continue;
// Assuming Ethernet header is 14 bytes if (count_packets) {
// Check IP version packet_count++;
ip_header = (struct ip*)(packet + ETHERNET_HEADER_LENGTH);
ip_protocol = ip_header->ip_v;
if (ip_protocol == 4) { gettimeofday(&current_time, NULL);
// IPv4
inet_ntop(AF_INET, &(ip_header->ip_src), source_ip_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET, &(ip_header->ip_dst), dest_ip_str, INET6_ADDRSTRLEN);
if (verbose) { long elapsed_ms = current_time.tv_sec * 1000 + (current_time.tv_usec /1000)-
printf("IPv4 Packet: %s -> %s, IP Protocol: %d\n", (last_count.tv_sec * 1000 + (last_count.tv_usec /1000));
source_ip_str, dest_ip_str, ip_header->ip_p);
if (elapsed_ms >= 500) {
printf("\rPacket count: %llu", packet_count);
fflush(stdout);
last_count = current_time; // Reset the timer
} }
} else if (ip_protocol == 6) { }
// IPv6
ip6_header = (struct ip6_hdr*)(packet + ETHERNET_HEADER_LENGTH);
inet_ntop(AF_INET6, &(ip6_header->ip6_src), source_ip_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &(ip6_header->ip6_dst), dest_ip_str, INET6_ADDRSTRLEN);
if (verbose) { if (verbose) {
printf("IPv6 Packet: %s -> %s, Next Header: %d\n",
source_ip_str, dest_ip_str, ip6_header->ip6_nxt); // Parse Ethernet header
struct ether_header *eth_header = (struct ether_header *)packet;
// Check EtherType
uint16_t ether_type = ntohs(eth_header->ether_type);
int vlan_offset = 0; // Offset for VLAN-tagged packets
// Check for VLAN tags (including Q-in-Q)
while (ether_type == 0x8100 || ether_type == 0x88A8) {
// VLAN tag is present
vlan_offset += 4; // Each VLAN tag adds 4 bytes
uint16_t vlan_tag = ntohs(*(uint16_t *)(packet + ETHERNET_HEADER_LENGTH + vlan_offset - 4));
uint16_t vlan_id = vlan_tag & 0x0FFF; // Extract VLAN ID (12 bits)
uint8_t vlan_pcp = (vlan_tag >> 13) & 0x07; // Extract Priority Code Point (3 bits)
uint8_t vlan_dei = (vlan_tag >> 12) & 0x01; // Extract Drop Eligible Indicator (1 bit)
printf("VLAN Tag: VLAN ID=%d, PCP=%d, DEI=%d\n", vlan_id, vlan_pcp, vlan_dei);
// Update EtherType to the next protocol
ether_type = ntohs(*(uint16_t *)(packet + ETHERNET_HEADER_LENGTH + vlan_offset - 2));
}
if (ether_type == ETHERTYPE_IP) {
// Handle IPv4 traffic
ip_header = (struct ip *)(packet + ETHERNET_HEADER_LENGTH + vlan_offset);
ip_protocol = ip_header->ip_v & 0x0F; // Get IP version
if (ip_protocol == 4) {
inet_ntop(AF_INET, &(ip_header->ip_src.s_addr), source_ip_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET, &(ip_header->ip_dst.s_addr), dest_ip_str, INET6_ADDRSTRLEN);
printf("IPv4 Packet: %s -> %s, IP Protocol: %s\n",
source_ip_str, dest_ip_str, lookup_protocol_name(ip_header->ip_p));
}
} else if (ether_type == ETHERTYPE_IPV6) {
// Handle IPv6 traffic
ip6_header = (struct ip6_hdr *)(packet + ETHERNET_HEADER_LENGTH + vlan_offset);
inet_ntop(AF_INET6, &(ip6_header->ip6_src), source_ip_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &(ip6_header->ip6_dst), dest_ip_str, INET6_ADDRSTRLEN);
printf("IPv6 Packet: %s -> %s, Next Header: %s\n",
source_ip_str, dest_ip_str, lookup_protocol_name(ip6_header->ip6_nxt));
} else if (ether_type == ETHERTYPE_ARP) {
// Handle ARP traffic
struct arp_header *arp = (struct arp_header *)(packet + ETHERNET_HEADER_LENGTH + vlan_offset);
printf("ARP Packet: Operation: %s\n",
(ntohs(arp->oper) == 1) ? "Request" : "Reply");
printf("Sender MAC: %02x:%02x:%02x:%02x:%02x:%02x, Sender IP: %d.%d.%d.%d\n",
arp->sha[0], arp->sha[1], arp->sha[2], arp->sha[3], arp->sha[4], arp->sha[5],
arp->spa[0], arp->spa[1], arp->spa[2], arp->spa[3]);
printf("Target MAC: %02x:%02x:%02x:%02x:%02x:%02x, Target IP: %d.%d.%d.%d\n",
arp->tha[0], arp->tha[1], arp->tha[2], arp->tha[3], arp->tha[4], arp->tha[5],
arp->tpa[0], arp->tpa[1], arp->tpa[2], arp->tpa[3]);
} else {
printf("Non-IP/ARP Packet, EtherType: 0x%04x\n", ether_type);
} }
} else {
printf("Non-IP Packet\n");
continue;
} }
// Create TZSP Header // Create TZSP Header
@@ -302,7 +424,7 @@ int main(int argc, char *argv[]) {
memcpy(ptr, packet, header.caplen); memcpy(ptr, packet, header.caplen);
// Send packet via UDP with TZSP encapsulation // Send packet via UDP with TZSP encapsulation
if (sendto(sockfd, tzsp_packet, total_length, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) == -1) { if (sendto(sockfd, tzsp_packet, total_length, 0, (struct sockaddr *)&dest_addr, dest_addr_size) == -1) {
perror("sendto"); perror("sendto");
} }
@@ -311,9 +433,6 @@ int main(int argc, char *argv[]) {
pcap_freecode(&fp); pcap_freecode(&fp);
pcap_close(handle); pcap_close(handle);
if (alldevs != NULL) {
pcap_freealldevs(alldevs); // Free the device list only if devices were found
}
close(sockfd); close(sockfd);
return(0); return(0);
} }

View File

@@ -1,4 +1,4 @@
.TH PCAPMIRROR 1 "March 22, 2025" "pcapmirror 0.3" "User Commands" .TH PCAPMIRROR 1 "March 24, 2025" "pcapmirror 0.5" "User Commands"
.SH NAME .SH NAME
pcapmirror \- A command-line tool for capturing and mirroring network traffic pcapmirror \- A command-line tool for capturing and mirroring network traffic
@@ -30,9 +30,15 @@ Force IPv4 host lookup.
.B \-6 .B \-6
Force IPv6 host lookup. Force IPv6 host lookup.
.TP .TP
.B \-l
List available network interfaces.
.TP
.B \-v .B \-v
Enable verbose mode (prints packet information). Enable verbose mode (prints packet information).
.TP .TP
.B \-c
Count matching packets (overrides verbose mode)
.TP
.B \-h .B \-h
Show this help message. Show this help message.

View File

@@ -1,5 +1,5 @@
Name: pcapmirror Name: pcapmirror
Version: 0.3 Version: 0.5
Release: %(perl -e 'print time()')%{?dist} Release: %(perl -e 'print time()')%{?dist}
Summary: A simple packet capture mirror Summary: A simple packet capture mirror
License: BSD 3-Clause License License: BSD 3-Clause License
@@ -26,6 +26,15 @@ pcapmirror is a command-line tool for capturing and mirroring network traffic us
%changelog %changelog
* Sat Mar 29 2025 Matthias Cramer <cramer@freesone.net> 0.5-1
- new option -c to count matching packets (overrides verbose mode)
- reworked packet decoder to also decode arp, vlan and qinq packets
- well known protocols numbers are now decoded
- works now on MacOS and OpenBSD
* Mon Mar 24 2025 Matthias Cramer <cramer@freesone.net> 0.4-1
- IPv6 support for remote destination
- remote destination can now also be hostname
- added option to enforce IPv4 and IPv6 for remote destination
* Sat Mar 22 2025 Matthias Cramer <cramer@freesone.net> 0.3-1 * Sat Mar 22 2025 Matthias Cramer <cramer@freesone.net> 0.3-1
- added manpage - added manpage
* Sat Mar 22 2025 Matthias Cramer <cramer@freesone.net> 0.2-1 * Sat Mar 22 2025 Matthias Cramer <cramer@freesone.net> 0.2-1