feat: yeah lots of things
This commit is contained in:
parent
36a83fa185
commit
62ff1a8efb
5 changed files with 225 additions and 0 deletions
124
npcap.c
Normal file
124
npcap.c
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
//
|
||||
// Created by ako on 8/22/25.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/if_ether.h>
|
||||
#include <netpacket/packet.h>
|
||||
#include <net/ethernet.h>
|
||||
#include <errno.h>
|
||||
#include "nethdr.h"
|
||||
#include "npcap_handle.h"
|
||||
#include <netpacket/packet.h>
|
||||
#include <net/if.h> // if_nametoindex()
|
||||
|
||||
void cleanup(int socket, unsigned char* buffer) {
|
||||
close(socket);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
if (geteuid() != 0)
|
||||
{
|
||||
printf("Please run me as root.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sock_raw = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
|
||||
if (sock_raw < 0)
|
||||
{
|
||||
fprintf(stderr, "Unable to connect to network socket.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char *buffer = (unsigned char *) calloc(65536, sizeof(unsigned char));
|
||||
|
||||
if (buffer == NULL) {
|
||||
fprintf(stderr, "Unable to connect to allocate memory.\n");
|
||||
cleanup(sock_raw, buffer);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct sockaddr_ll sourceaddr;
|
||||
socklen_t sourceaddr_len = sizeof(sourceaddr);
|
||||
|
||||
int loopback_ifindex = if_nametoindex("lo");
|
||||
if (loopback_ifindex == 0) {
|
||||
perror("if_nametoindex for lo");
|
||||
cleanup(sock_raw, buffer);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
ssize_t buflen = recvfrom(sock_raw, buffer, 65536, 0, (struct sockaddr *)&sourceaddr, &sourceaddr_len);
|
||||
|
||||
if (buflen == -1) {
|
||||
perror("Couldn't read from socket");
|
||||
cleanup(sock_raw, buffer);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (sourceaddr.sll_ifindex == loopback_ifindex) {
|
||||
continue;
|
||||
}
|
||||
printf("\n===== Paquet reçu =====\n");
|
||||
printf("Paquet de %ld octets\n", buflen);
|
||||
|
||||
// Debug
|
||||
printf("Hexdump des 32 premiers octets:\n");
|
||||
for (int i = 0; i < 32 && i < buflen; i++) {
|
||||
printf("%02x ", buffer[i]);
|
||||
if ((i + 1) % 8 == 0) printf(" ");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
//
|
||||
// COUCHE 2
|
||||
//
|
||||
// Décodage entête Ethernet
|
||||
struct ethhdr *eth = (struct ethhdr *)buffer;
|
||||
handle_eth(eth);
|
||||
|
||||
|
||||
//
|
||||
// COUCHE 3
|
||||
//
|
||||
switch (ntohs(eth->h_proto))
|
||||
{
|
||||
// Décodage entête ARP
|
||||
case ETH_P_ARP:
|
||||
struct arphdr_c *arp = (struct arphdr_c *) (buffer + sizeof(struct ethhdr));
|
||||
handle_arp(arp);
|
||||
break;
|
||||
|
||||
case ETH_P_IP:
|
||||
struct iphdr *ip = (struct iphdr *) (buffer + sizeof(struct ethhdr));
|
||||
handle_ip(ip);
|
||||
|
||||
switch (ip->protocol) {
|
||||
case 0x01:
|
||||
handle_icmp();
|
||||
break;
|
||||
case 0x06:
|
||||
handle_tcp();
|
||||
break;
|
||||
case 0x11:
|
||||
handle_udp();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// printf("\n=======================\n");
|
||||
}
|
||||
cleanup(sock_raw, buffer);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue