fix: various bug fixes on allocations and stuff (still wip)

This commit is contained in:
0x35c 2025-05-26 15:46:58 +02:00
parent 1ce702d169
commit 675248cbff
6 changed files with 12 additions and 13 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ ft_nmap
.cache .cache
compile_commands.json compile_commands.json
tags tags
dlopen.supp

View File

@ -8,7 +8,7 @@
#include "scan.h" #include "scan.h"
typedef enum { typedef enum {
OPEN, OPEN = 1,
CLOSE, CLOSE,
FILTERED, FILTERED,
UNFILTERED, UNFILTERED,

View File

@ -10,7 +10,7 @@ int dns_lookup(char *ip_addr, char *hostname, struct sockaddr_in *addr_con)
{ {
struct hostent *host = gethostbyname2(hostname, AF_INET); struct hostent *host = gethostbyname2(hostname, AF_INET);
if (!host) { if (!host) {
dprintf(2, "Hostname %s doesn't exist or has invalid format.", dprintf(2, "Hostname %s doesn't exist or has invalid format.\n",
hostname); hostname);
return -1; return -1;
} }

View File

@ -3,6 +3,7 @@
#include <pcap.h> #include <pcap.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "dns.h" #include "dns.h"
#include "interface.h" #include "interface.h"
@ -29,7 +30,7 @@ int main(int ac, char **av)
return 1; return 1;
} }
struct response responses[1024]; struct response responses[1024] = {0};
struct thread *single_thread = malloc(sizeof(struct thread)); struct thread *single_thread = malloc(sizeof(struct thread));
if (get_interface_name(&single_thread->host) < 0) if (get_interface_name(&single_thread->host) < 0)

View File

@ -15,7 +15,6 @@ void create_udp_packet(struct udphdr *udphdr, const struct scan *data)
udphdr->source = htons(1234); udphdr->source = htons(1234);
udphdr->dest = htons(data->port); udphdr->dest = htons(data->port);
udphdr->len = sizeof(struct udphdr); udphdr->len = sizeof(struct udphdr);
udphdr->check = 0;
udphdr->check = checksum(udphdr, sizeof(struct udphdr)); udphdr->check = checksum(udphdr, sizeof(struct udphdr));
} }
@ -23,17 +22,13 @@ int create_tcp_packet(struct tcphdr *tcphdr, const struct scan *data)
{ {
tcphdr->source = htons(1234); tcphdr->source = htons(1234);
tcphdr->dest = htons(data->port); tcphdr->dest = htons(data->port);
tcphdr->seq = 0;
tcphdr->ack_seq = 0;
tcphdr->doff = sizeof(struct tcphdr) / sizeof(int); tcphdr->doff = sizeof(struct tcphdr) / sizeof(int);
tcphdr->fin = data->type == SCAN_XMAS || data->type == SCAN_FIN; tcphdr->fin = data->type == SCAN_XMAS || data->type == SCAN_FIN;
tcphdr->syn = data->type == SCAN_SYN; tcphdr->syn = data->type == SCAN_SYN;
tcphdr->rst = 0;
tcphdr->psh = data->type == SCAN_XMAS; tcphdr->psh = data->type == SCAN_XMAS;
tcphdr->ack = data->type == SCAN_ACK; tcphdr->ack = data->type == SCAN_ACK;
tcphdr->urg = data->type == SCAN_XMAS; tcphdr->urg = data->type == SCAN_XMAS;
tcphdr->window = htons(5840); tcphdr->window = htons(5840);
tcphdr->check = 0;
tcphdr->urg_ptr = 0; tcphdr->urg_ptr = 0;
struct pshdr pshdr; struct pshdr pshdr;
@ -58,7 +53,7 @@ int create_tcp_packet(struct tcphdr *tcphdr, const struct scan *data)
static void *create_packet(const struct scan *data, size_t packet_size) static void *create_packet(const struct scan *data, size_t packet_size)
{ {
const bool isudp = data->type == SCAN_UDP; const bool isudp = data->type == SCAN_UDP;
void *packet = malloc(packet_size); void *packet = calloc(packet_size, 1);
if (!packet) { if (!packet) {
dprintf(2, dprintf(2,
"ft_nmap: allocation failed during packet creation\n"); "ft_nmap: allocation failed during packet creation\n");
@ -74,7 +69,6 @@ static void *create_packet(const struct scan *data, size_t packet_size)
iphdr->frag_off = 0; iphdr->frag_off = 0;
iphdr->ttl = 48; iphdr->ttl = 48;
iphdr->protocol = isudp ? IPPROTO_UDP : IPPROTO_TCP; iphdr->protocol = isudp ? IPPROTO_UDP : IPPROTO_TCP;
iphdr->check = 0;
iphdr->saddr = inet_addr(data->host->ip); iphdr->saddr = inet_addr(data->host->ip);
iphdr->daddr = inet_addr(data->dest_addr); iphdr->daddr = inet_addr(data->dest_addr);
@ -96,6 +90,7 @@ int send_packets(const struct scan *data, int sockfd)
{ {
struct sockaddr_in conn_addr; struct sockaddr_in conn_addr;
conn_addr.sin_family = AF_INET; conn_addr.sin_family = AF_INET;
conn_addr.sin_port = htons(80);
conn_addr.sin_addr.s_addr = inet_addr(data->dest_addr); conn_addr.sin_addr.s_addr = inet_addr(data->dest_addr);
size_t packet_size = sizeof(struct iphdr) + size_t packet_size = sizeof(struct iphdr) +

View File

@ -7,6 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h>
#include "packet.h" #include "packet.h"
#include "response.h" #include "response.h"
@ -89,10 +90,11 @@ int scan(const struct scan *data)
send_packets(data, sockfd); send_packets(data, sockfd);
// TODO test with another cnt value if (!pcap_dispatch(handle, 1, dispatch_callback, (u_char *)data)) {
if (pcap_dispatch(handle, 10, dispatch_callback, (u_char *)data)) { printf("timeout\n");
;
} }
pcap_close(handle);
close(sockfd);
return 0; return 0;
} }