21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The first step in restricting access from the network based on hostname or IP address<br />

is to ensure that the remote connection is not engaging in a DNS spoofing attack. No<br />

foolproof method exists for guaranteeing that the address is not being spoofed,<br />

though the code presented here can provide a reasonable assurance for most cases. In<br />

particular, if the DNS server for the domain that an IP address reverse-maps to has<br />

been compromised, there is no way to know.<br />

The first code listing that we present implements a worker function, check_spoofdns( ),<br />

which performs a set of DNS lookups and compares the results. The first lookup<br />

retrieves the hostname to which an IP address maps. An IP address does not necessarily<br />

have to reverse-map to a hostname, so if this first lookup yields no mapping, it is generally<br />

safe to assume that no spoofing is taking place.<br />

If the IP address does map to a hostname, a lookup is performed on that hostname to<br />

retrieve the IP address or addresses to which it maps. The hostname should exist, but<br />

if it does not, the connection should be considered suspect. Although it is possible<br />

that something funny is going on with the remote connection, the lack of a name-toaddress<br />

mapping could be innocent.<br />

Each of the addresses returned by the hostname lookup is compared against the IP<br />

address of the remote connection. If the IP address of the remote connection is not<br />

matched, the likelihood of a spoofing attack is high, though still not guaranteed. If<br />

the IP address of the remote connection is matched, the code assumes that no spoofing<br />

attack is taking place.<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

#define SPC_ERROR_NOREVERSE 1 /* IP address does not map to a hostname */<br />

#define SPC_ERROR_NOHOSTNAME 2 /* Reversed hostname does not exist */<br />

#define SPC_ERROR_BADHOSTNAME 3 /* IP addresses do not match */<br />

#define SPC_ERROR_HOSTDENIED 4 /* TCP/SPC Wrappers denied host access */<br />

static int check_spoofdns(int sockfd, struct sockaddr_in *addr, char **name) {<br />

int addrlen, i;<br />

char *hostname;<br />

struct hostent *he;<br />

*name = 0;<br />

for (;;) {<br />

addrlen = sizeof(struct sockaddr_in);<br />

if (getpeername(sockfd, (struct sockaddr *)addr, &addrlen) != -1) break;<br />

380 | Chapter 8: Authentication and Key Exchange<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!