feature: default interface and basic start

This commit is contained in:
0x35c 2025-05-21 18:22:46 +02:00
parent ba6eb6c27e
commit 1361cd9f9c
13 changed files with 170 additions and 10 deletions

4
.gitignore vendored
View File

@ -1,2 +1,4 @@
obj obj
ft_nmap ft_nmap
.cache
compile_commands.json

View File

@ -4,7 +4,7 @@ CC := gcc
CFLAGS := -Wall -Wextra -Werror -iquoteinclude -g CFLAGS := -Wall -Wextra -Werror -iquoteinclude -g
LD := $(CC) LD := $(CC)
LDFLAGS := LDFLAGS := -lpcap
SRC := $(shell find src -name '*.c') SRC := $(shell find src -name '*.c')
OBJ := $(patsubst src/%.c,obj/%.o,$(SRC)) OBJ := $(patsubst src/%.c,obj/%.o,$(SRC))
@ -28,4 +28,4 @@ re:
$(MAKE) fclean $(MAKE) fclean
$(MAKE) all $(MAKE) all
.PHONY: all clean fclean re .PHONY: all clean fclean re

6
include/dns.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <netinet/in.h>
int dns_lookup(char *ip_addr, char *hostname, struct sockaddr_in *addr_con);
int reverse_dns_lookup(char *ip_addr, char *host);

3
include/error.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
int err(char *str);

3
include/interface.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
char *get_interface_name(void);

3
include/print.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void print_usage(void);

16
include/scan.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <stdint.h>
typedef enum {
SCAN_SYN,
SCAN_NULL,
SCAN_ACK,
SCAN_FIN,
SCAN_XMAS,
SCAN_UDP,
SCAN_TCP,
} e_scantype;
int routine(const char *ip_addr, e_scantype type, uint16_t min_port,
uint16_t max_port);

39
src/dns.c Normal file
View File

@ -0,0 +1,39 @@
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
int dns_lookup(char *ip_addr, char *hostname, struct sockaddr_in *addr_con)
{
struct hostent *host = gethostbyname2(hostname, AF_INET);
if (!host) {
dprintf(2, "Hostname %s doesn't exist or has invalid format.",
hostname);
return -1;
}
strcpy(ip_addr, inet_ntoa(*(struct in_addr *)host->h_addr));
(*addr_con).sin_family = host->h_addrtype;
(*addr_con).sin_port = htons(0);
(*addr_con).sin_addr.s_addr = *(long *)host->h_addr;
return 0;
}
int reverse_dns_lookup(char *ip_addr, char *host)
{
struct sockaddr_in tmp_addr;
tmp_addr.sin_family = AF_INET;
tmp_addr.sin_addr.s_addr = inet_addr(ip_addr);
if (getnameinfo((struct sockaddr *)&tmp_addr,
sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0,
NI_NAMEREQD)) {
dprintf(2, "Could not resolve reverse lookup of %s\n", ip_addr);
return -1;
}
return 0;
}

9
src/error.c Normal file
View File

@ -0,0 +1,9 @@
#include <errno.h>
#include <stdio.h>
int err(char *str)
{
int err = errno;
perror(str);
return err;
}

24
src/get_interface.c Normal file
View File

@ -0,0 +1,24 @@
#include <pcap.h>
#include <string.h>
char *get_interface_name(void)
{
pcap_if_t *alldevs;
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&alldevs, errbuf) == -1) {
dprintf(2, "Error finding devices: %s\n", errbuf);
return NULL;
}
for (pcap_if_t *it = alldevs; it != NULL; it = it->next) {
if (!(it->flags & PCAP_IF_LOOPBACK)) {
char *name = strdup(it->name);
pcap_freealldevs(alldevs);
return name;
}
}
dprintf(2, "No non-loopback interface found.\n");
return NULL;
}

View File

@ -1,12 +1,35 @@
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pcap.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "dns.h"
#include "print.h"
#include "scan.h"
int main(int ac, char **av) int main(int ac, char **av)
{ {
(void) ac; if (ac < 2) {
(void) av; dprintf(2, "Usage: ft_nmap [Scan Type(s)] [Options] {target "
printf("%s %s\n", "specification}\n");
"gros gras grand grain d'orge, quand te dégros-gras-grand-grain-d'orgeras-tu ?", print_usage();
"Je me dégros-gras-grand-grain-d'orgerai, quand tous les gros gras grains d'orge se seront dégros-gras-grand-grain- d'orgés."); return 0;
}
// TODO parse arguments
(void)av;
return 0; char ip_addr[INET_ADDRSTRLEN];
} struct sockaddr_in addr_con;
if (dns_lookup(ip_addr, av[1], &addr_con)) {
dprintf(2, "ft_nmap: failed to retrieve ip address from %s\n",
av[1]);
return 1;
}
if (routine(ip_addr, SCAN_SYN, 1, 1024) < 0) {
dprintf(2, "ft_nmap: failed to scan ports\n");
return 1;
}
return 0;
}

6
src/print.c Normal file
View File

@ -0,0 +1,6 @@
#include "print.h"
void print_usage(void)
{
// TODO print options list
}

26
src/scan.c Normal file
View File

@ -0,0 +1,26 @@
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include "error.h"
#include "interface.h"
#include "scan.h"
int routine(const char *ip_addr, e_scantype type, uint16_t min_port,
uint16_t max_port)
{
int sockfd =
socket(AF_INET, type == SCAN_UDP ? SOCK_DGRAM : SOCK_STREAM, 0);
if (sockfd < 0)
return err("Failed to create socket");
char *default_if = get_interface_name();
printf("%s\n", default_if);
(void)ip_addr;
for (int port = min_port; port <= max_port; port++) {
// scan(ip_addr, type, port);
}
return 0;
}