1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/*
*
* This is free software. You can redistribute it and/or modify under
* the terms of the GNU General Public License version 2.
*
* Copyright (C) 1998 by kra
*
*/
#include "hunt.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
/*
*
* the icmp and arp processes cannot run in parallel - we
* are using the same MODULE_HOSTUP for linking the packets
*
*/
static struct list l_icmp_packet = LIST_INIT(struct packet, p_next[MODULE_HOSTUP]);
static struct list l_arp_packet = LIST_INIT(struct packet, p_next[MODULE_HOSTUP]);
struct host_up_info {
unsigned int start_addr;
unsigned int end_addr;
int *up_ping;
int *promisc_ping;
int *up_arp;
int *promisc_arp;
unsigned int up_len;
};
static void func_icmp_packet(struct packet *p, void *arg)
{
struct iphdr *iph = p->p_iph;
struct icmphdr *icmph = p->p_hdr.p_icmph;
struct host_up_info *hui = (struct host_up_info *) arg;
if (ntohl(iph->saddr) >= ntohl(hui->start_addr) &&
ntohl(iph->saddr) <= ntohl(hui->end_addr) &&
icmph->type == 0 && icmph->code == 0) {
packet_want(p);
list_produce(&l_icmp_packet, p);
}
}
static void func_arp_packet(struct packet *p, void *arg)
{
struct host_up_info *hui = (struct host_up_info *) arg;
struct arpeth_hdr *arpethh;
unsigned int ip;
arpethh = (struct arpeth_hdr *)(p->p_arph + 1);
ip = *(unsigned int *) arpethh->ar_sip;
if (p->p_arph->ar_op == htons(ARPOP_REPLY) &&
ntohl(ip) >= ntohl(hui->start_addr) &&
ntohl(ip) <= ntohl(hui->end_addr)) {
packet_want(p);
list_produce(&l_arp_packet, p);
}
}
static void perform_ping(struct host_up_info *hui, int count, int *up, unsigned char *fake_mac)
{
struct mac_info *m;
struct packet *p;
struct timeval tv;
struct timespec timeout;
unsigned int ip, src_addr;
unsigned int idx;
int j;
printf("ping");
fflush(stdout);
for (j = 1; j <= count; j++) {
for (ip = hui->start_addr;
ntohl(ip) <= ntohl(hui->end_addr);
ip = htonl(ntohl(ip) + 1)) {
idx = ntohl(ip) - ntohl(hui->start_addr);
if (!up[idx]) {
if (!fake_mac) {
if ((m = mac_info_get(ip))) {
send_icmp_request(my_eth_ip,
ip, my_eth_mac,
m->mac, htons(j + 2000));
mac_info_release(m);
}
} else {
send_icmp_request(my_eth_ip, ip,
my_eth_mac, fake_mac, htons(j + 2000));
}
}
}
/* listen some time for reply */
gettimeofday(&tv, NULL);
timeout.tv_sec = tv.tv_sec + 2;
timeout.tv_nsec = tv.tv_usec * 1000;
while ((p = list_consume(&l_icmp_packet, &timeout))) {
/* src addr is in the range start_addr .. end_addr -
look to func_icmp_packet */
src_addr = p->p_iph->saddr;
if (is_icmp_reply(p, src_addr, my_eth_ip,
p->p_ethh->h_source, my_eth_mac) ||
(fake_mac && is_icmp_reply(p, src_addr, my_eth_ip,
fake_mac, my_eth_mac))) {
idx = ntohl(src_addr) - ntohl(hui->start_addr);
up[idx] = 1;
host_lookup(src_addr, HL_MODE_DEFERRED);
}
packet_free(p);
}
printf(".");
fflush(stdout);
}
printf("\n");
}
static void send_arp_message(unsigned int ip, char *dst_mac)
{
struct arp_spec as;
as.src_mac = my_eth_mac;
as.dst_mac = dst_mac;
as.oper = htons(ARPOP_REQUEST);
as.sender_mac = my_eth_mac;
as.sender_addr = my_eth_ip;
as.target_mac = mac_zero;
as.target_addr = ip;
send_arp_packet(&as);
}
/*
* it will be good idea to turn off the mac discoverer daemon
*/
static void perform_arp(struct host_up_info *hui, int count, int *up, unsigned char *fake_mac)
{
int j, idx;
unsigned int ip, src_addr;
struct arpeth_hdr *arpethh;
struct timeval tv;
struct timespec timeout;
struct packet *p;
printf("arp");
fflush(stdout);
for (j = 1; j <= count; j++) {
for (ip = hui->start_addr;
ntohl(ip) <= ntohl(hui->end_addr);
ip = htonl(ntohl(ip) + 1)) {
idx = ntohl(ip) - ntohl(hui->start_addr);
if (!up[idx]) {
if (!fake_mac)
send_arp_message(ip, mac_broadcast);
else
send_arp_message(ip, fake_mac);
}
}
/* listen some time for reply */
gettimeofday(&tv, NULL);
timeout.tv_sec = tv.tv_sec + 2;
timeout.tv_nsec = tv.tv_usec * 1000;
/* the received packets are ARP_REPLY - and from expected range
from func_arp_packet */
while ((p = list_consume(&l_arp_packet, &timeout))) {
arpethh = (struct arpeth_hdr *)(p->p_arph + 1);
src_addr = *(unsigned int *) arpethh->ar_sip;
if (memcmp(arpethh->ar_sha, p->p_ethh->h_source, ETH_ALEN) == 0 &&
memcmp(my_eth_mac, p->p_ethh->h_dest, ETH_ALEN) == 0 &&
memcmp(my_eth_mac, arpethh->ar_tha, ETH_ALEN) == 0 &&
*(unsigned int *) arpethh->ar_tip == my_eth_ip
) { /* sanity check that it was triggered by as */
idx = ntohl(src_addr) - ntohl(hui->start_addr);
up[idx] = 1;
host_lookup(src_addr, HL_MODE_DEFERRED);
}
packet_free(p);
}
printf(".");
fflush(stdout);
}
printf("\n");
}
static void list_host_up(struct host_up_info *hui, int *up)
{
unsigned int addr, idx;
for (addr = hui->start_addr;
ntohl(addr) <= ntohl(hui->end_addr);
addr = htonl(ntohl(addr) + 1)) {
idx = ntohl(addr) - ntohl(hui->start_addr);
if ((up && up[idx]) ||
(!up && (hui->up_ping[idx] || hui->up_arp[idx]))) {
printf("UP %s\n", host_lookup(addr, HL_MODE_DEFERRED));
}
}
}
static void list_host_promisc(struct host_up_info *hui, int *promisc)
{
unsigned int addr, idx;
for (addr = hui->start_addr;
ntohl(addr) <= ntohl(hui->end_addr);
addr = htonl(ntohl(addr) + 1)) {
idx = ntohl(addr) - ntohl(hui->start_addr);
if ((promisc && promisc[idx]) ||
(!promisc && (hui->promisc_ping[idx] || hui->promisc_arp[idx]))) {
printf("in PROMISC MODE %s\n",
host_lookup(addr, HL_MODE_DEFERRED));
}
}
}
void host_up(void)
{
static unsigned int start_ip_def = 0, end_ip_def = 0;
unsigned int start_ip, end_ip;
struct ifunc_item ifunc_icmp;
struct ifunc_item ifunc_arp;
struct host_up_info *hui;
struct timespec ts;
unsigned int len;
unsigned char buf_mac[BUFSIZE], fake_mac[ETH_ALEN];
if ((start_ip = menu_choose_hostname("start ip addr", host_lookup(start_ip_def, HL_MODE_NR))) == -1)
return;
if ((end_ip = menu_choose_hostname("end ip addr", host_lookup(end_ip_def, HL_MODE_NR))) == -1)
return;
if ((len = ntohl(end_ip) - ntohl(start_ip) + 1) < 0) {
printf("bad addresses\n");
return;
}
start_ip_def = start_ip;
end_ip_def = end_ip;
hui = malloc(sizeof(struct host_up_info));
hui->start_addr = start_ip;
hui->end_addr = end_ip;
hui->up_ping = malloc(sizeof(int) * len);
hui->promisc_ping = malloc(sizeof(int) * len);
hui->up_arp = malloc(sizeof(int) * len);
hui->promisc_arp = malloc(sizeof(int) * len);
hui->up_len = len;
memset(hui->up_ping, 0, sizeof(int) * len);
memset(hui->promisc_ping, 0, sizeof(int) * len);
memset(hui->up_arp, 0, sizeof(int) * len);
memset(hui->promisc_arp, 0, sizeof(int) * len);
switch (menu_choose_char("host up test (arp method) y/n", "yn", 'y')) {
case 'y':
ifunc_arp.func = func_arp_packet;
ifunc_arp.arg = hui;
list_enqueue(&l_ifunc_arp, &ifunc_arp);
perform_arp(hui, 3, hui->up_arp, NULL);
list_remove(&l_ifunc_arp, &ifunc_arp);
packet_flush(&l_arp_packet);
list_host_up(hui, hui->up_arp);
break;
}
switch (menu_choose_char("host up test (ping method) y/n", "yn", 'y')) {
case 'y':
printf("mac discovery\n");
mac_discover_range(hui->start_addr, hui->end_addr, 2);
ts.tv_sec = 1;
ts.tv_nsec = 0;
nanosleep(&ts, NULL);
ifunc_icmp.func = func_icmp_packet;
ifunc_icmp.arg = hui;
list_enqueue(&l_ifunc_icmp, &ifunc_icmp);
perform_ping(hui, 3, hui->up_ping, NULL);
list_remove(&l_ifunc_icmp, &ifunc_icmp);
packet_flush(&l_icmp_packet);
list_host_up(hui, hui->up_ping);
break;
}
switch (menu_choose_char("net ifc promisc test (arp method) y/n", "yn", 'y')) {
case 'y':
sprintf_eth_mac(buf_mac, suggest_mac());
if (menu_choose_mac("choose unused MAC in your network", fake_mac, buf_mac) >= 0) {
ifunc_arp.func = func_arp_packet;
ifunc_arp.arg = hui;
list_enqueue(&l_ifunc_arp, &ifunc_arp);
perform_arp(hui, 3, hui->promisc_arp, fake_mac);
list_remove(&l_ifunc_arp, &ifunc_arp);
packet_flush(&l_arp_packet);
list_host_promisc(hui, hui->promisc_arp);
}
break;
}
switch (menu_choose_char("net ifc promisc test (ping method) y/n", "yn", 'y')) {
case 'y':
sprintf_eth_mac(buf_mac, suggest_mac());
if (menu_choose_mac("choose unused MAC in your network", fake_mac, buf_mac) >= 0) {
ifunc_icmp.func = func_icmp_packet;
ifunc_icmp.arg = hui;
list_enqueue(&l_ifunc_icmp, &ifunc_icmp);
perform_ping(hui, 3, hui->promisc_ping, fake_mac);
list_remove(&l_ifunc_icmp, &ifunc_icmp);
packet_flush(&l_icmp_packet);
list_host_promisc(hui, hui->promisc_ping);
}
break;
}
free(hui->up_ping);
free(hui->promisc_ping);
free(hui->up_arp);
free(hui->promisc_arp);
free(hui);
}
|