Your Ad Here

---[ Phrack Magazine Volume 7, Issue 51 September 01, 1997, article 11 of 17

-------------------------[ The Art of Port Scanning

--------[ Fyodor fyodor@dhp.com

[ Abstract ]

This paper details many of the techniques used to determine what ports (or similar protocol abstraction) of a host are listening for connections. These ports represent potential communication channels. Mapping their existence facilitates the exchange of information with the host, and thus it is quite useful for anyone wishing to explore their networked environment, including hackers. Despite what you have heard from the media, the Internet is NOT all about TCP port 80. Anyone who relies exclusively on the WWW for information gathering is likely to gain the same level of proficiency as your average AOLer, who does the same. This paper is also meant to serve as an introduction to and ancillary documentation for a coding project I have been working on. It is a full featured, robust port scanner which (I hope) solves some of the problems I have encountered when dealing with other scanners and when working to scan massive networks. The tool, nmap, supports the following:

- vanilla TCP connect() scanning, 
- TCP SYN (half open) scanning,
- TCP FIN (stealth) scanning, 
- TCP ftp proxy (bounce attack) scanning
- SYN/FIN scanning using IP fragments (bypasses packet filters),
- UDP recvfrom() scanning, 
- UDP raw ICMP port unreachable scanning, 
- ICMP scanning (ping-sweep), and
- reverse-ident scanning.

The freely distributable source code is appended to this paper.

[ Introduction ]

Scanning, as a method for discovering exploitable communication channels, has been around for ages. The idea is to probe as many listeners as possible, and keep track of the ones that are receptive or useful to your particular need. Much of the field of advertising is based on this paradigm, and the "to current resident" brute force style of bulk mail is an almost perfect parallel to what we will discuss. Just stick a message in every mailbox and wait for the responses to trickle back.

Scanning entered the h/p world along with the phone systems. Here we have this tremendous global telecommunications network, all reachable through codes on our telephone. Millions of numbers are reachable locally, yet we may only be interested in 0.5% of these numbers, perhaps those that answer with a carrier.

The logical solution to finding those numbers that interest us is to try them all. Thus the field of "wardialing" arose. Excellent programs like Toneloc were developed to facilitate the probing of entire exchanges and more. The basic idea is simple. If you dial a number and your modem gives you a CONNECT, you record it. Otherwise the computer hangs up and tirelessly dials the next one.

While wardialing is still useful, we are now finding that many of the computers we wish to communicate with are connected through networks such as the Internet rather than analog phone dialups. Scanning these machines involves the same brute force technique. We send a blizzard of packets for various protocols, and we deduce which services are listening from the responses we receive (or don't receive).

[ Techniques ]

Over time, a number of techniques have been developed for surveying the protocols and ports on which a target machine is listening. They all offer different benefits and problems. Here is a line up of the most common:

For port scanning, our technique is to use the PORT command to declare that our passive "User-DTP" is listening on the target box at a certain port number. Then we try to LIST the current directory, and the result is sent over the Server-DTP channel. If our target host is listening on the specified port, the transfer will be successful (generating a 150 and a 226 response). Otherwise we will get "425 Can't build data connection: Connection refused." Then we issue another PORT command to try the next port on the target host. The advantages to this approach are obvious (harder to trace, potential to bypass firewalls). The main disadvantages are that it is slow, and that some FTP servers have finally got a clue and disabled the proxy "feature". For what it is worth, here is a list of banners from sites where it does/doesn't work:

Bounce attacks worked:

220 xxxxxxx.com FTP server (Version wu-2.4(3) Wed Dec 14 ...) ready. 220 xxx.xxx.xxx.edu FTP server ready. 220 xx.Telcom.xxxx.EDU FTP server (Version wu-2.4(3) Tue Jun 11 ...) ready. 220 lem FTP server (SunOS 4.1) ready. 220 xxx.xxx.es FTP server (Version wu-2.4(11) Sat Apr 27 ...) ready. 220 elios FTP server (SunOS 4.1) ready

Bounce attack failed:

220 wcarchive.cdrom.com FTP server (Version DG-2.0.39 Sun May 4 ...) ready. 220 xxx.xx.xxxxx.EDU Version wu-2.4.2-academBETA-12 Fri Feb 7 220 ftp Microsoft FTP Service (Version 3.0). 220 xxx FTP server (Version wu-2.4.2-academBETA-11 Tue Sep 3 ...) ready. 220 xxx.unc.edu FTP server (Version wu-2.4.2-academBETA-13 ...) ready.

The 'x's are partly there to protect those guilty of running a flawed server, but mostly just to make the lines fit in 80 columns. Same thing with the ellipse points. The bounce attack is available with the -b option of nmap. proxy_server can be specified in standard URL format, username:password@server:port , with everything but server being optional.

Some people think UDP scanning is lame and pointless. I usually remind them of the recent Solaris rcpbind hole. Rpcbind can be found hiding on an undocumented UDP port somewhere above 32770. So it doesn't matter that 111 is blocked by the firewall. But can you find which of the more than 30,000 high ports it is listening on? With a UDP scanner you can!

[ Features ]

Prior to writing nmap, I spent a lot of time with other scanners exploring the Internet and various private networks (note the avoidance of the "intranet" buzzword). I have used many of the top scanners available today, including strobe by Julian Assange, netcat by Hobbit, stcp by Uriel Maimon, pscan by Pluvius, ident-scan by Dave Goldsmith, and the SATAN tcp/udp scanners by Wietse Venema. These are all excellent scanners! In fact, I ended up hacking most of them to support the best features of the others. Finally I decided to write a whole new scanner, rather than rely on hacked versions of a dozen different scanners in my /usr/local/sbin. While I wrote all the code, nmap uses a lot of good ideas from its predecessors. I also incorporated some new stuff like fragmentation scanning and options that were on my "wish list" for other scanners. Here are some of the (IMHO) useful features of nmap:

Some other, more minor options:

-v (verbose): This is highly recommended for interactive use. Among other useful messages, you will see ports come up as they are found, rather than having to wait for the sorted summary list.

-r (randomize): This will randomize the order in which the target host's ports are scanned.

-q (quash argv): This changes argv[0] to FAKE_ARGV ("pine" by default). It also eliminates all other arguments, so you won't look too suspicious in 'w' or 'ps' listings.

-h for an options summary.

Also look for http://www.dhp.com/~fyodor/nmap/, which is the web site I plan to put future versions and more information on. In fact, you would be well advised to check there right now.

[ Greets ]

Of course this paper would not be complete without a shout out to all the people who made it possible.

And finally, we get to ...

[ The code ]

This should compile fine on any Linux box with 'gcc -O6 -o nmap nmap.c -lm'. It is distrubuted under the terms of the GNU GENERAL PUBLIC LICENSE. If you have problems or comments, feel free to mail me (fyodor@dhp.com).

<++> nmap/Makefile

A trivial makefile for Network Mapper

nmap: nmap.c nmap.h gcc -Wall -O6 -o nmap nmap.c -lm <-->

<++> nmap/nmap.h

ifndef NMAP_H

define NMAP_H

/*******INCLUDES***********/

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include

include //

include //

include

include

include

include

include

include

/*******DEFINES***********/

/* #define to zero if you don't want to ignore hosts of the form xxx.xxx.xxx.{0,255} (usually network and broadcast addresses) */

define IGNOREZEROAND255HOSTS 1

define DEBUGGING 0

/* Default number of ports in paralell. Doesn't always involve actual sockets. Can also adjust with the -M command line option. */

define MAX_SOCKETS 36

/* If reads of a UDP port keep returning EAGAIN (errno 13), do we want to count the port as valid? */

define RISKYUDPSCAN 0

/* This ideally should be a port that isn't in use for any protocol on our machine or on the target */

define MAGIC_PORT 49724

/* How many udp sends without a ICMP port unreachable error does it take before we consider the port open? */

define UDPMAXPORT_RETRIES 4

/*How many seconds before we give up on a host being alive? */

define PING_TIMEOUT 2

define FAKE_ARGV "pine" /* What ps and w should show if you use -q */

/* How do we want to log into ftp sites for */

define FTPUSER "anonymous"

define FTPPASS "-wwwuser@"

define FTP_RETRIES 2 /* How many times should we relogin if we lose control

                     connection? */

define UC(b) (((int)b)&0xff)

define MORE_FRAGMENTS 8192 /NOT a user serviceable parameter/

define fatal(x) { fprintf(stderr, "%s\n", x); exit(-1); }

define error(x) fprintf(stderr, "%s\n", x);

/******STRUCTURES***********/

typedef struct port { unsigned short portno; unsigned char proto; char *owner; struct port *next; } port;

struct ftpinfo { char user[64]; char pass[256]; /* methinks you're paranoid if you need this much space / char server_name[MAXHOSTNAMELEN + 1]; struct in_addr server; unsigned short port; int sd; / socket descriptor */ };

typedef port *portlist;

/******PROTOTYPES***********/

/* print usage information */ void printusage(char *name);

/* our scanning functions */ portlist tcpscan(struct inaddr target, unsigned short *portarray, portlist *ports); portlist synscan(struct inaddr target, unsigned short *portarray, struct inaddr *source, int fragment, portlist *ports); portlist finscan(struct inaddr target, unsigned short *portarray, struct inaddr *source, int fragment, portlist *ports); portlist udpscan(struct inaddr target, unsigned short *portarray, portlist *ports); portlist lamerudpscan(struct inaddr target, unsigned short *portarray, portlist *ports); portlist bouncescan(struct in_addr target, unsigned short *portarray, struct ftpinfo *ftp, portlist *ports);

/* Scan helper functions */ unsigned long calculatesleep(struct inaddr target); int checkidentport(struct inaddr target); int getidentinfoz(struct inaddr target, int localport, int remoteport, char *owner); int parsebounce(struct ftpinfo *ftp, char *url); int ftpanon_connect(struct ftpinfo *ftp);

/* port manipulators / unsigned short *getpts(char *expr); / someone stole the name getports()! */ unsigned short *getfastports(int tcpscan, int udpscan); int addport(portlist *ports, unsigned short portno, unsigned short protocol, char *owner); int deleteport(portlist *ports, unsigned short portno, unsigned short protocol); void printandfreeports(portlist ports); int shortfry(unsigned short *ports);

/* socket manipulation functions */ void initsocket(int sd); int unblocksocket(int sd); int block_socket(int sd); int recvtime(int sd, char *buf, int len, int seconds);

/* RAW packet building/dissasembling stuff */ int sendtcpraw( int sd, struct inaddr *source, struct inaddr *victim, unsigned short sport, unsigned short dport, unsigned long seq, unsigned long ack, unsigned char flags, unsigned short window, char *data, unsigned short datalen); int isup(struct inaddr target); unsigned short incksum(unsigned short *ptr,int nbytes); int sendsmallfragz(int sd, struct inaddr *source, struct inaddr *victim, int sport, int dport, int flags); int readtcppacket(char *packet, int readdata); int listenicmp(int icmpsock, unsigned short outports[], unsigned short numtries[], int *numout, struct in_addr target, portlist *ports);

/* general helper functions */ void hdump(unsigned char *packet, int len); void *safe_malloc(int size);

endif /* NMAP_H */

<-->

<++> nmap/nmap.c

include "nmap.h"

/* global options / short debugging = DEBUGGING; short verbose = 0; int number_of_ports = 0; / How many ports do we scan per machine? */ int maxparallelsockets = MAXSOCKETS; extern char *optarg; extern int optind; short isr00t = 0; short identscan = 0; char currentname[MAXHOSTNAMELEN + 1]; unsigned long globaldelay = 0; unsigned long globalrtt = 0; struct in_addr ouraddr = { 0 };

int main(int argc, char *argv[]) { int i, j, arg, argvlen; short fastscan=0, tcpscan=0, udpscan=0, synscan=0, randomize=0; short fragscan = 0, finscan = 0, quashargv = 0, pingscan = 0, lamerscan = 0; short bouncescan = 0; short *ports = NULL, mask; struct ftpinfo ftp = { FTPUSER, FTPPASS, "", { 0 }, 21, 0}; portlist openports = NULL; struct hostent *target = 0; unsigned long int lastip, currentip, longtmp; char *targetnet, *p; struct inaddr current_in, *source=NULL; int hostup = 0; char *fakeargv[argc + 1];

/* argv faking silliness */ for(i=0; i < argc; i++) { fakeargv[i] = safe_malloc(strlen(argv[i]) + 1); strncpy(fakeargv[i], argv[i], strlen(argv[i]) + 1); } fakeargv[argc] = NULL;

if (argc < 2 ) printusage(argv[0]);

/* OK, lets parse these args! */ while((arg = getopt(argc,fakeargv,"b:dFfhilM:Pp:qrS:stUuw:v")) != EOF) { switch(arg) { case 'b': bouncescan++; if (parsebounce(&ftp, optarg) < 0 ) { fprintf(stderr, "Your argument to -b is fucked up. Use the normal url style: user:pass@server:port or just use server and use default anon login\n Use -h for help\n"); } break; case 'd': debugging++; break; case 'F': fastscan++; break; case 'f': fragscan++; break; case 'h': case '?': printusage(argv[0]); case 'i': identscan++; break; case 'l': lamerscan++; udpscan++; break; case 'M': maxparallelsockets = atoi(optarg); break; case 'P': pingscan++; break; case 'p': if (ports) fatal("Only 1 -p option allowed, seperate multiple ranges with commas."); ports = getpts(optarg); break; case 'r': randomize++; break; case 's': synscan++; break; case 'S': if (source) fatal("You can only use the source option once!\n"); source = safemalloc(sizeof(struct inaddr)); if (!inetaton(optarg, source)) fatal("You must give the source address in dotted deciman, currently.\n"); break; case 't': tcpscan++; break; case 'U': finscan++; break; case 'u': udpscan++; break; case 'q': quashargv++; break; case 'w': global_delay = atoi(optarg); break; case 'v': verbose++; } }

/* Take care of user wierdness / isr00t = !(geteuid()|geteuid()); if (tcpscan && synscan) fatal("The -t and -s options can't be used together.\ If you are trying to do TCP SYN scanning, just use -s.\ For normal connect() style scanning, use -t"); if ((synscan || finscan || fragscan || pingscan) && !isr00t) fatal("Options specified require r00t privileges. You don't have them!"); if (!tcpscan && !udpscan && !synscan && !finscan && !bouncescan && !pingscan) { tcpscan++; if (verbose) error("No scantype specified, assuming vanilla tcp connect()\ scan. Use -P if you really don't want to portscan."); if (fastscan && ports) fatal("You can use -F (fastscan) OR -p for explicit port specification.\ Not both!\n"); } / If he wants to bounce of an ftp site, that site better damn well be reachable! */ if (bouncescan) { if (!inetaton(ftp.servername, &ftp.server)) { if ((target = gethostbyname(ftp.servername))) memcpy(&ftp.server, target->haddrlist[0], 4); else { fprintf(stderr, "Failed to resolve ftp bounce proxy hostname/IP: %s\n", ftp.servername); exit(1); } } else if (verbose) printf("Resolved ftp bounce attack proxy to %s (%s).\n", target->hname, inetntoa(ftp.server)); } printf("\nStarting nmap V 1.21 by Fyodor (fyodor@dhp.com, www.dhp.com/~fyodor/nmap/\n"); if (!verbose) error("Hint: The -v option notifies you of open ports as they are found.\n"); if (fastscan) ports = getfastports(synscan|tcpscan|fragscan|finscan|bouncescan, udpscan|lamerscan); if (!ports) ports = getpts("1-1024");

/* more fakeargv junk, BTW malloc'ing extra space in argv[0] doesn't work */ if (quashargv) { argvlen = strlen(argv[0]); if (argvlen < strlen(FAKEARGV)) fatal("If you want me to fake your argv, you need to call the program with a longer name. Try the full pathname, or rename it fyodorssuperdedouperportscanner"); strncpy(argv[0], FAKEARGV, strlen(FAKEARGV)); for(i = strlen(FAKEARGV); i < argvlen; i++) argv[0][i] = '\0'; for(i=1; i < argc; i++) { argvlen = strlen(argv[i]); for(j=0; j <= argvlen; j++) argv[i][j] = '\0'; } }

srand(time(NULL));

while(optind < argc) {

/* Time to parse the allowed mask */ target = NULL; targetnet = strtok(strdup(fakeargv[optind]), "/"); mask = (p = strtok(NULL,""))? atoi(p) : 32; if (debugging) printf("Target network is %s, scanmask is %d\n", targetnet, mask);

if (!inetaton(targetnet, &currentin)) { if ((target = gethostbyname(targetnet))) memcpy(&currentip, target->haddrlist[0], 4); else { fprintf(stderr, "Failed to resolve given hostname/IP: %s\n", targetnet); } } else currentip = currentin.s_addr;

longtmp = ntohl(currentip); currentip = longtmp & (unsigned long) (0 - pow(2,32 - mask)); lastip = longtmp | (unsigned long) (pow(2,32 - mask) - 1); while (currentip <= lastip) { openports = NULL; longtmp = htonl(currentip); target = gethostbyaddr((char *) &longtmp, 4, AFINET); currentin.saddr = longtmp; if (target) strncpy(currentname, target->hname, MAXHOSTNAMELEN); else currentname[0] = '\0'; current_name[MAXHOSTNAMELEN + 1] = '\0'; if (randomize) shortfry(ports);

ifdef IGNOREZEROAND255HOSTS

if (IGNORE_ZERO_AND_255_HOSTS 
&& (!(currentip % 256) || currentip % 256 == 255))
  {
printf("Skipping host %s because IGNORE_ZERO_AND_255_HOSTS is set in the source.\n", inet_ntoa(current_in));
hostup = 0;
  }
else{

endif

  if (isr00t) {
if (!(hostup = isup(current_in))) {
  if (!pingscan)
    printf("Host %s (%s) appears to be down, skipping scan.\n",
       current_name, inet_ntoa(current_in));
  else
    printf("Host %s (%s) appears to be  down\n",
       current_name, inet_ntoa(current_in));
} else if (debugging || pingscan)
  printf("Host %s (%s) appears to be up ... good.\n",
             current_name, inet_ntoa(current_in));
  }
  else hostup = 1; /* We don't really check because the lamer isn't root.*/
}

/* Time for some actual scanning! */    
if (hostup) {
  if (tcpscan) tcp_scan(current_in, ports, &openports);

  if (synscan) syn_scan(current_in, ports, source, fragscan, &openports);

  if (finscan) fin_scan(current_in, ports, source, fragscan, &openports);

  if (bouncescan) {
if (ftp.sd <= 0) ftp_anon_connect(&ftp);
if (ftp.sd > 0) bounce_scan(current_in, ports, &ftp, &openports);
  }
  if (udpscan) {
if (!isr00t || lamerscan) 
  lamer_udp_scan(current_in, ports, &openports);

else udp_scan(current_in, ports, &openports);
  }

  if (!openports && !pingscan)
printf("No ports open for host %s (%s)\n", current_name,
       inet_ntoa(current_in));
  if (openports) {
printf("Open ports on %s (%s):\n", current_name, 
       inet_ntoa(current_in));
printandfreeports(openports);
  }
}
currentip++;

} optind++; }

return 0; }

inline int unblocksocket(int sd) { int options; /*Unblock our socket to prevent recvfrom from blocking forever on certain target ports. */ options = ONONBLOCK | fcntl(sd, FGETFL); fcntl(sd, FSETFL, options); return 1; }

inline int blocksocket(int sd) { int options; options = (~ONONBLOCK) & fcntl(sd, FGETFL); fcntl(sd, FSETFL, options); return 1; }

/* Currently only sets SOLINGER, I haven't seen any evidence that this helps. I'll do more testing before dumping it. */ inline void initsocket(int sd) { struct linger l;

l.lonoff = 1; l.llinger = 0;

if (setsockopt(sd, SOLSOCKET, SOLINGER, &l, sizeof(struct linger))) { fprintf(stderr, "Problem setting socket SO_LINGER, errno: %d\n", errno); perror("setsockopt"); } }

/* Convert a string like "-100,200-1024,3000-4000,60000-" into an array of port numbers/ unsigned short *getpts(char *origexpr) { int exlen = strlen(origexpr); char *p,q; unsigned short tmp, *ports; int i=0, j=0,start,end; char *expr = strdup(origexpr); ports = safe_malloc(65536 * sizeof(short)); i++; i--; for(;j < exlen; j++) if (expr[j] != ' ') expr[i++] = expr[j]; expr[i] = '\0'; exlen = i + 1; i=0; while((p = strchr(expr,','))) { *p = '\0'; if (expr == '-') {start = 1; end = atoi(expr+ 1);} else { start = end = atoi(expr); if ((q = strchr(expr,'-')) && (q+1) ) end = atoi(q + 1); else if (q && !(q+1)) end = 65535; } if (debugging) printf("The first port is %d, and the last one is %d\n", start, end); if (start < 1 || start > end) fatal("Your port specifications are illegal!"); for(j=start; j <= end; j++) ports[i++] = j; expr = p + 1; } if (expr == '-') { start = 1; end = atoi(expr+ 1); } else { start = end = atoi(expr); if ((q = strchr(expr,'-')) && *(q+1) ) end = atoi(q+1); else if (q && !(q+1)) end = 65535; } if (debugging) printf("The first port is %d, and the last one is %d\n", start, end); if (start < 1 || start > end) fatal("Your port specifications are illegal!"); for(j=start; j <= end; j++) ports[i++] = j; numberofports = i; ports[i++] = 0; tmp = realloc(ports, i * sizeof(short)); free(expr); return tmp; }

unsigned short *getfastports(int tcpscan, int udpscan) { int portindex = 0, res, lastport = 0; unsigned int portno = 0; unsigned short *ports; char proto[10]; char line[81]; FILE *fp; ports = safe_malloc(65535 * sizeof(unsigned short)); proto[0] = '\0'; if (!(fp = fopen("/etc/services", "r"))) { printf("We can't open /etc/services for reading! Fix your system or don't use -f\n"); perror("fopen"); exit(1); }

while(fgets(line, 80, fp)) { res = sscanf(line, "%*s %u/%s", &portno, proto); if (res == 2 && portno != 0 && portno != lastport) { lastport = portno; if (tcpscan && proto[0] == 't') ports[portindex++] = portno; else if (udpscan && proto[0] == 'u') ports[portindex++] = portno; } }

numberofports = portindex; ports[portindex++] = 0; return realloc(ports, portindex * sizeof(unsigned short)); }

void printusage(char *name) { printf("%s [options] [hostname[/mask] . . .] options (none are required, most can be combined): -t tcp connect() port scan -s tcp SYN stealth port scan (must be root) -u UDP port scan, will use MUCH better version if you are root -U Uriel Maimon (P49-15) style FIN stealth scan. -l Do the lamer UDP scan even if root. Less accurate. -P ping \"scan\". Find which hosts on specified network(s) are up. -b ftp \"bounce attack\" port scan -f use tiny fragmented packets for SYN or FIN scan. -i Get identd (rfc 1413) info on listening TCP processes. -p ports: ex: \'-p 23\' will only try port 23 of the host(s) \'-p 20-30,63000-\' scans 20-30 and 63000-65535 default: 1-1024 -F fast scan. Only scans ports in /etc/services, a la strobe(1). -r randomize target port scanning order. -h help, print this junk. Also see http://www.dhp.com/~fyodor/nmap/ -S If you want to specify the source address of SYN or FYN scan. -v Verbose. Its use is recommended. Use twice for greater effect. -w delay. n microsecond delay. Not recommended unless needed. -M maximum number of parallel sockets. Larger isn't always better. -q quash argv to something benign, currently set to \"%s\". Hostnames specified as internet hostname or IP address. Optional '/mask' specifies subnet. cert.org/24 or 192.88.209.5/24 scan CERT's Class C.\n", name, FAKE_ARGV); exit(1); }

portlist tcpscan(struct inaddr target, unsigned short *portarray, portlist *ports) {

int starttime, currentout = 0, res , deadindex = 0, i=0, j=0, k=0, max=0; struct sockaddrin sock, stranger, mysock; int sockaddrinlen = sizeof(struct sockaddrin); int sockets[maxparallelsockets], deadstack[maxparallelsockets]; unsigned short portno[maxparallelsockets]; char owner[513], buf[65536]; int tryident = identscan, currentsocket /actually it is a socket INDEX/; fdset fdsread, fds_write; struct timeval nowait = {0,0}, longwait = {7,0};

signal(SIGPIPE, SIGIGN); /* ignore SIGPIPE so our 'write 0 bytes' test doesn't crash our program!*/ owner[0] = '\0'; starttime = time(NULL); bzero((char *)&sock,sizeof(struct sockaddrin)); sock.sinaddr.saddr = target.saddr; if (verbose || debugging) printf("Initiating TCP connect() scan against %s (%s)\n", currentname, inetntoa(sock.sinaddr)); sock.sinfamily=AFINET; FDZERO(&fdsread); FDZERO(&fdswrite);

if (tryident) tryident = checkidentport(target);

/* Initially, all of our sockets are "dead" */ for(i = 0 ; i < maxparallelsockets; i++) { deadstack[deadindex++] = i; portno[i] = 0; }

deadindex--; /* deadindex always points to the most recently added dead socket index */

while(portarray[j]) { longwait.tvsec = 7; longwait.tvusec = nowait.tvsec = nowait.tvusec = 0;

for(i=currentout; i < maxparallelsockets && portarray[j]; i++, j++) { currentsocket = deadstack[deadindex--]; if ((sockets[currentsocket] = socket(AFINET, SOCKSTREAM, IPPROTOTCP)) == -1) {perror("Socket troubles"); exit(1);} if (sockets[currentsocket] > max) max = sockets[currentsocket]; currentout++; unblocksocket(sockets[currentsocket]); initsocket(sockets[currentsocket]); portno[currentsocket] = portarray[j]; sock.sinport = htons(portarray[j]); if ((res = connect(sockets[currentsocket],(struct sockaddr )&sock,sizeof(struct sockaddr)))!=-1) printf("WTF???? I think we got a successful connection in non-blocking!!@#$\n"); else { switch(errno) { case EINPROGRESS: / The one I always see / case EAGAIN: block_socket(sockets[current_socket]); FD_SET(sockets[current_socket], &fds_write); FD_SET(sockets[current_socket], &fds_read); break; default: printf("Strange error from connect: (%d)", errno); perror(""); /falling through intentionally/ case ECONNREFUSED: if (max == sockets[current_socket]) max--; deadstack[++deadindex] = current_socket; current_out--; portno[current_socket] = 0; close(sockets[current_socket]); break; } } } if (!portarray[j]) sleep(1); /wait a second for any last packets/ while((res = select(max + 1, &fds_read, &fds_write, NULL, (current_out < max_parallel_sockets)? &nowait : &longwait)) > 0) { for(k=0; k < max_parallel_sockets; k++) if (portno[k]) { if (FD_ISSET(sockets[k], &fds_write) && FD_ISSET(sockets[k], &fds_read)) { /printf("Socket at port %hi is selectable for r & w.", portno[k]);/ res = recvfrom(sockets[k], buf, 65536, 0, (struct sockaddr *) & stranger, &sockaddr_in_len); if (res >= 0) { if (debugging || verbose) printf("Adding TCP port %hi due to successful read.\n", portno[k]); if (tryident) { if ( getsockname(sockets[k], (struct sockaddr *) &mysock, &sockaddr_in_len ) ) { perror("getsockname"); exit(1); } tryident = getidentinfoz(target, ntohs(mysock.sin_port), portno[k], owner); }
addport(ports, portno[k], IPPROTO_TCP, owner); } if (max == sockets[k]) max--; FD_CLR(sockets[k], &fds_read); FD_CLR(sockets[k], &fds_write); deadstack[++deadindex] = k; current_out--; portno[k] = 0; close(sockets[k]); } else if(FD_ISSET(sockets[k], &fds_write)) { /
printf("Socket at port %hi is selectable for w only.VERIFYING\n", portno[k]);/ res = send(sockets[k], buf, 0, 0); if (res < 0 ) { signal(SIGPIPE, SIG_IGN); if (debugging > 1) printf("Bad port %hi caught by 0-byte write!\n", portno[k]); } else { if (debugging || verbose) printf("Adding TCP port %hi due to successful 0-byte write!\n", portno[k]); if (tryident) { if ( getsockname(sockets[k], (struct sockaddr *) &mysock , &sockaddr_in_len ) ) { perror("getsockname"); exit(1); } tryident = getidentinfoz(target, ntohs(mysock.sin_port), portno[k], owner); }
addport(ports, portno[k], IPPROTO_TCP, owner);
} if (max == sockets[k]) max--; FD_CLR(sockets[k], &fds_write); deadstack[++deadindex] = k; current_out--; portno[k] = 0; close(sockets[k]); } else if ( FD_ISSET(sockets[k], &fds_read) ) {
printf("Socket at port %hi is selectable for r only. This is very wierd.\n", portno[k]); if (max == sockets[k]) max--; FD_CLR(sockets[k], &fds_read); deadstack[++deadindex] = k; current_out--; portno[k] = 0; close(sockets[k]); } else { /
printf("Socket at port %hi not selecting, readding.\n",portno[k]);*/ FDSET(sockets[k], &fdswrite); FDSET(sockets[k], &fdsread); } } } }

if (debugging || verbose) printf("Scanned %d ports in %ld seconds with %d parallel sockets.\n", numberofports, time(NULL) - starttime, maxparallelsockets); return *ports; }

/* gawd, my next project will be in c++ so I don't have to deal with this crap ... simple linked list implementation */ int addport(portlist *ports, unsigned short portno, unsigned short protocol, char *owner) { struct port *current, *tmp; int len;

if (ports) { current = *ports; / case 1: we add to the front of the list / if (portno <= current->portno) { if (current->portno == portno && current->proto == protocol) { if (debugging || verbose) printf("Duplicate port (%hi/%s)\n", portno , (protocol == IPPROTO_TCP)? "tcp": "udp"); return -1; }
tmp = current; *ports = safe_malloc(sizeof(struct port)); (
ports)->next = tmp; current = ports; current->portno = portno; current->proto = protocol; if (owner && *owner) { len = strlen(owner); current->owner = malloc(sizeof(char) * (len + 1)); strncpy(current->owner, owner, len + 1); } else current->owner = NULL; } else { / case 2: we add somewhere in the middle or end of the list */ while( current->next && current->next->portno < portno) current = current->next; if (current->next && current->next->portno == portno && current->next->proto == protocol) { if (debugging || verbose) printf("Duplicate port (%hi/%s)\n", portno , (protocol == IPPROTOTCP)? "tcp": "udp"); return -1; } tmp = current->next; current->next = safemalloc(sizeof(struct port)); current->next->next = tmp; tmp = current->next; tmp->portno = portno; tmp->proto = protocol; if (owner && *owner) { len = strlen(owner); tmp->owner = malloc(sizeof(char) * (len + 1)); strncpy(tmp->owner, owner, len + 1); } else tmp->owner = NULL; } }

else { /* Case 3, list is null / *ports = safe_malloc(sizeof(struct port)); tmp = *ports; tmp->portno = portno; tmp->proto = protocol; if (owner && *owner) { len = strlen(owner); tmp->owner = safe_malloc(sizeof(char) * (len + 1)); strncpy(tmp->owner, owner, len + 1); } else tmp->owner = NULL; tmp->next = NULL; } return 0; /success */ }

int deleteport(portlist *ports, unsigned short portno, unsigned short protocol) { portlist current, tmp;

if (!ports) { if (debugging > 1) error("Tried to delete from empty port list!"); return -1; } / Case 1, deletion from front of list/ if ((ports)->portno == portno && (ports)->proto == protocol) { tmp = (ports)->next; if ((ports)->owner) free((ports)->owner); free(ports); *ports = tmp; } else { current = *ports; for(;current->next && (current->next->portno != portno || current->next->proto != protocol); current = current->next); if (!current->next) return -1; tmp = current->next; current->next = tmp->next; if (tmp->owner) free(tmp->owner); free(tmp); } return 0; / success */ }

void *safe_malloc(int size) { void *mymem; if (size < 0) fatal("Tried to malloc negative amount of memmory!!!"); if ((mymem = malloc(size)) == NULL) fatal("Malloc Failed! Probably out of space."); return mymem; }

void printandfreeports(portlist ports) { char protocol[4]; struct servent *service; port *current = ports, *tmp;

printf("Port Number Protocol Service"); printf("%s", (identscan)?" Owner\n":"\n"); while(current != NULL) { strcpy(protocol,(current->proto == IPPROTOTCP)? "tcp": "udp"); service = getservbyport(htons(current->portno), protocol); printf("%-13d%-11s%-16s%s\n", current->portno, protocol, (service)? service->sname: "unknown", (current->owner)? current->owner : ""); tmp = current; current = current->next; if (tmp->owner) free(tmp->owner); free(tmp); } printf("\n"); }

/* This is the version of udpscan that uses raw ICMP sockets and requires root priviliges.*/ portlist udpscan(struct inaddr target, unsigned short *portarray, portlist *ports) { int icmpsock, udpsock, tmp, done=0, retries, bytes = 0, res, numout = 0; int i=0,j=0, k=0, icmperrlimittime, maxtries = UDPMAXPORTRETRIES; unsigned short outports[maxparallelsockets], numtries[maxparallelsockets]; struct sockaddrin her; char senddata[] = "blah\n"; unsigned long starttime, sleeptime; struct timeval shortwait = {1, 0 }; fdset fdsread, fdswrite;

bzero(outports, maxparallelsockets * sizeof(unsigned short)); bzero(numtries, maxparallelsockets * sizeof(unsigned short));

/* Some systems (like linux) follow the advice of rfc1812 and limit * the rate at which they will respons with icmp error messages * (like port unreachable). icmperrlimittime is to compensate for that. */ icmperrlimittime = 60000;

sleeptime = (globaldelay)? globaldelay : (globalrtt)? (1.2 * globalrtt) + 30000 : 1e5; if (globaldelay) icmperrlimittime = globaldelay;

starttime = time(NULL);

FDZERO(&fdsread); FDZERO(&fdswrite);

if (verbose || debugging) printf("Initiating UDP (raw ICMP version) scan against %s (%s) using wait delay of %li usecs.\n", currentname, inetntoa(target), sleeptime);

if ((icmpsock = socket(AFINET, SOCKRAW, IPPROTOICMP)) < 0) perror("Opening ICMP RAW socket"); if ((udpsock = socket(AFINET, SOCKDGRAM, IPPROTOUDP)) < 0) perror("Opening datagram socket");

unblocksocket(icmpsock); her.sinaddr = target; her.sinfamily = AFINET;

while(!done) { tmp = numout; for(i=0; (i < maxparallelsockets && portarray[j]) || i < tmp; i++) { close(udpsock); if ((udpsock = socket(AFINET, SOCKDGRAM, IPPROTOUDP)) < 0) perror("Opening datagram socket"); if ((i > tmp && portarray[j]) || numtries[i] > 1) { if (i > tmp) her.sinport = htons(portarray[j++]); else her.sinport = htons(outports[i]); FDSET(udpsock, &fdswrite); FDSET(icmpsock, &fdsread); shortwait.tvsec = 1; shortwait.tvusec = 0; usleep(icmperrlimittime); res = select(udpsock + 1, NULL, &fdswrite, NULL, &shortwait); if (FDISSET(udpsock, &fdswrite)) bytes = sendto(udpsock, senddata, sizeof(senddata), 0, (struct sockaddr *) &her, sizeof(struct sockaddrin)); else { printf("udpsock not set for writing port %d!", ntohs(her.sinport)); return *ports; } if (bytes <= 0) { if (errno == ECONNREFUSED) { retries = 10; do {
/* This is from when I was using the same socket and would * (rather often) get strange connection refused errors, it * shouldn't happen now that I create a new udp socket for each * port. At some point I will probably go back to 1 socket again. */ printf("sendto said connection refused on port %d but trying again anyway.\n", ntohs(her.sin
port)); usleep(icmperrlimittime); bytes = sendto(udpsock, senddata, sizeof(senddata), 0, (struct sockaddr ) &her, sizeof(struct sockaddr_in)); printf("This time it returned %d\n", bytes); } while(bytes <= 0 && retries-- > 0); } if (bytes <= 0) { printf("sendto returned %d.", bytes); fflush(stdout); perror("sendto"); } } if (bytes > 0 && i > tmp) { num_out++; outports[i] = portarray[j-1]; } } } usleep(sleeptime); tmp = listen_icmp(icmpsock, outports, numtries, &num_out, target, ports); if (debugging) printf("listen_icmp caught %d bad ports.\n", tmp); done = !portarray[j]; for (i=0,k=0; i < max_parallel_sockets; i++) if (outports[i]) { if (++numtries[i] > max_tries - 1) { if (debugging || verbose) printf("Adding port %d for 0 unreachable port generations\n", outports[i]); addport(ports, outports[i], IPPROTO_UDP, NULL); num_out--; outports[i] = numtries[i] = 0;
} else { done = 0; outports[k] = outports[i]; numtries[k] = numtries[i]; if (k != i) outports[i] = numtries[i] = 0; k++; } } if (num_out == max_parallel_sockets) { printf("Numout is max sockets, that is a problem!\n"); sleep(1); /
Give some time for responses to trickle back, and possibly to reset the hosts ICMP error limit */ } }

if (debugging || verbose) printf("The UDP raw ICMP scanned %d ports in %ld seconds with %d parallel sockets.\n", numberofports, time(NULL) - starttime, maxparallelsockets); close(icmpsock); close(udpsock); return *ports; }

int listenicmp(int icmpsock, unsigned short outports[], unsigned short numtries[], int *numout, struct inaddr target, portlist *ports) { char response[1024]; struct sockaddrin stranger; int sockaddrinsize = sizeof(struct sockaddrin); struct inaddr bs; struct iphdr *ip = (struct iphdr *) response; struct icmphdr *icmp = (struct icmphdr *) (response + sizeof(struct iphdr)); struct iphdr *ip2; unsigned short *data; int badport, numcaught=0, bytes, i, tmptry=0, found=0;

while ((bytes = recvfrom(icmpsock, response, 1024, 0, (struct sockaddr ) &stranger, &sockaddr_in_size)) > 0) { numcaught++; bs.s_addr = ip->saddr; if (ip->saddr == target.s_addr && ip->protocol == IPPROTO_ICMP && icmp->type == 3 && icmp->code == 3) {
ip2 = (struct iphdr *) (response + 4 * ip->ihl + sizeof(struct icmphdr)); data = (unsigned short *) ((char *)ip2 + 4 * ip2->ihl); badport = ntohs(data[1]); /
delete it from our outports array / found = 0; for(i=0; i < max_parallel_sockets; i++) if (outports[i] == badport) { found = 1; tmptry = numtries[i]; outports[i] = numtries[i] = 0; (numout)--; break; } if (debugging && found && tmptry > 0) printf("Badport: %d on try number %d\n", badport, tmptry); if (!found) { if (debugging) printf("Badport %d came in late, deleting from portlist.\n", badport); if (deleteport(ports, badport, IPPROTOUDP) < 0) if (debugging) printf("Port deletion failed.\n"); } } else { printf("Funked up packet!\n"); } } return numcaught; }

/* This fucntion is nonsens. I wrote it all, really optimized etc. Then found out that many hosts limit the rate at which they send icmp errors :( I will probably totally rewrite it to be much simpler at some point. For now I won't worry about it since it isn't a very important functions (UDP is lame, plus there is already a much better function for people who are r00t */ portlist lamerudpscan(struct inaddr target, unsigned short *portarray, portlist *ports) { int sockaddrinsize = sizeof(struct sockaddrin),i=0,j=0,k=0, bytes; int sockets[maxparallelsockets], trynum[maxparallelsockets]; unsigned short portno[maxparallelsockets]; int lastopen = 0; char response[1024]; struct sockaddrin her, stranger; char data[] = "\nhelp\nquit\n"; unsigned long sleeptime; unsigned int starttime;

/* Initialize our target sockaddrin */ bzero((char *) &her, sizeof(struct sockaddrin)); her.sinfamily = AFINET; her.sin_addr = target;

if (globaldelay) sleeptime = globaldelay; else sleeptime = calculate_sleep(target) + 60000; /*large to be on the safe side */

if (verbose || debugging) printf("Initiating UDP scan against %s (%s), sleeptime: %li\n", currentname, inetntoa(target), sleeptime);

starttime = time(NULL);

for(i = 0 ; i < maxparallelsockets; i++) trynum[i] = portno[i] = 0;

while(portarray[j]) { for(i=0; i < maxparallelsockets && portarray[j]; i++, j++) { if (i >= lastopen) { if ((sockets[i] = socket(AFINET, SOCKDGRAM, IPPROTOUDP)) == -1) {perror("datagram socket troubles"); exit(1);} blocksocket(sockets[i]); portno[i] = portarray[j]; } her.sinport = htons(portarray[j]); bytes = sendto(sockets[i], data, sizeof(data), 0, (struct sockaddr ) &her, sizeof(struct sockaddr_in)); usleep(5000); if (debugging > 1) printf("Sent %d bytes on socket %d to port %hi, try number %d.\n", bytes, sockets[i], portno[i], trynum[i]); if (bytes < 0 ) { printf("Sendto returned %d the FIRST TIME!@#$!, errno %d\n", bytes, errno); perror(""); trynum[i] = portno[i] = 0; close(sockets[i]); } } last_open = i; / Might need to change this to 1e6 if you are having problems/ usleep(sleeptime + 5e5); for(i=0; i < last_open ; i++) { if (portno[i]) { unblock_socket(sockets[i]); if ((bytes = recvfrom(sockets[i], response, 1024, 0, (struct sockaddr *) &stranger, &sockaddr_in_size)) == -1) { if (debugging > 1) printf("2nd recvfrom on port %d returned %d with errno %d.\n", portno[i], bytes, errno); if (errno == EAGAIN /11/) { if (trynum[i] < 2) trynum[i]++; else { if (RISKY_UDP_SCAN) {
printf("Adding port %d after 3 EAGAIN errors.\n", portno[i]); addport(ports, portno[i], IPPROTO_UDP, NULL); } else if (debugging) printf("Skipping possible false positive, port %d\n", portno[i]); trynum[i] = portno[i] = 0; close(sockets[i]); } } else if (errno == ECONNREFUSED /
111/) { if (debugging > 1) printf("Closing socket for port %d, ECONNREFUSED received.\n", portno[i]); trynum[i] = portno[i] = 0; close(sockets[i]); } else { printf("Curious recvfrom error (%d) on port %hi: ", errno, portno[i]); perror(""); trynum[i] = portno[i] = 0; close(sockets[i]); } } else /bytes is positive/ { if (debugging || verbose) printf("Adding UDP port %d due to positive read!\n", portno[i]); addport(ports,portno[i], IPPROTO_UDP, NULL); trynum[i] = portno[i] = 0; close(sockets[i]); } } } / Update lastopen, we need to create new sockets.*/ for(i=0, k=0; i < lastopen; i++) if (portno[i]) { close(sockets[i]); sockets[k] = socket(AFINET, SOCKDGRAM, IPPROTOUDP); /* unblocksocket(sockets[k]);*/ portno[k] = portno[i]; trynum[k] = trynum[i]; k++; } lastopen = k; for(i=k; i < maxparallelsockets; i++) trynum[i] = sockets[i] = portno[i] = 0; } if (debugging) printf("UDP scanned %d ports in %ld seconds with %d parallel sockets\n", numberofports, time(NULL) - starttime, maxparallel_sockets); return *ports; }

/* This attempts to calculate the round trip time (rtt) to a host by timing a connect() to a port which isn't listening. A better approach is to time a ping (since it is more likely to get through firewalls. This is now implemented in isup() for users who are root. */ unsigned long calculatesleep(struct inaddr target) { struct timeval begin, end; int sd; struct sockaddr_in sock; int res;

if ((sd = socket(AFINET, SOCKSTREAM, IPPROTO_TCP)) == -1) {perror("Socket troubles"); exit(1);}

sock.sinfamily = AFINET; sock.sinaddr.saddr = target.saddr; sock.sinport = htons(MAGIC_PORT);

gettimeofday(&begin, NULL); if ((res = connect(sd, (struct sockaddr ) &sock, sizeof(struct sockaddr_in))) != -1) printf("You might want to change MAGIC_PORT in the include file, it seems to be listening on the target host!\n"); close(sd); gettimeofday(&end, NULL); if (end.tv_sec - begin.tv_sec > 5 ) /uh-oh!*/ return 0; return (end.tvsec - begin.tvsec) * 1000000 + (end.tvusec - begin.tvusec); }

/* Checks whether the identd port (113) is open on the target machine. No sense wasting time trying it for each good port if it is down! */ int checkidentport(struct inaddr target) { int sd; struct sockaddrin sock; int res;

if ((sd = socket(AFINET, SOCKSTREAM, IPPROTO_TCP)) == -1) {perror("Socket troubles"); exit(1);}

sock.sinfamily = AFINET; sock.sinaddr.saddr = target.saddr; sock.sinport = htons(113); /*should use getservbyname(3), yeah, yeah */ res = connect(sd, (struct sockaddr *) &sock, sizeof(struct sockaddr_in)); close(sd); if (res < 0 ) { if (debugging || verbose) printf("identd port not active\n"); return 0; } if (debugging || verbose) printf("identd port is active\n"); return 1; }

int getidentinfoz(struct inaddr target, int localport, int remoteport, char *owner) { int sd; struct sockaddrin sock; int res; char request[15]; char response[1024]; char p,q; char *os;

owner[0] = '\0'; if ((sd = socket(AFINET, SOCKSTREAM, IPPROTO_TCP)) == -1) {perror("Socket troubles"); exit(1);}

sock.sinfamily = AFINET; sock.sinaddr.saddr = target.saddr; sock.sinport = htons(113); usleep(50000); /* If we aren't careful, we really MIGHT take out inetd, some are very fragile */ res = connect(sd, (struct sockaddr *) &sock, sizeof(struct sockaddr_in));

if (res < 0 ) { if (debugging || verbose) printf("identd port not active now for some reason ... hope we didn't break it!\n"); close(sd); return 0; } sprintf(request,"%hi,%hi\r\n", remoteport, localport); if (debugging > 1) printf("Connected to identd, sending request: %s", request); if (write(sd, request, strlen(request) + 1) == -1) { perror("identd write"); close(sd); return 0; } else if ((res = read(sd, response, 1024)) == -1) { perror("reading from identd"); close(sd); return 0; } else { close(sd); if (debugging > 1) printf("Read %d bytes from identd: %s\n", res, response); if ((p = strchr(response, ':'))) { p++; if ((q = strtok(p, " :"))) { if (!strcasecmp( q, "error")) { if (debugging || verbose) printf("ERROR returned from identd\n"); return 0; } if ((os = strtok(NULL, " :"))) { if ((p = strtok(NULL, " :"))) { if ((q = strchr(p, '\r'))) *q = '\0'; if ((q = strchr(p, '\n'))) *q = '\0'; strncpy(owner, p, 512); owner[512] = '\0'; } } } }
} return 1; }

/* A relatively fast (or at least short ;) ping function. Doesn't require a seperate checksum function / int isup(struct in_addr target) { int res, retries = 3; struct sockaddr_in sock; /type(8bit)=8, code(8)=0 (echo REQUEST), checksum(16)=34190, id(16)=31337 */

ifdef _LITTLEENDIAN_BITFIELD

unsigned char ping[64] = { 0x8, 0x0, 0x8e, 0x85, 0x69, 0x7A };

else

unsigned char ping[64] = { 0x8, 0x0, 0x85, 0x8e, 0x7A, 0x69 };

endif

int sd; struct timeval tv; struct timeval start, end; fdset fdread; struct { struct iphdr ip; unsigned char type; unsigned char code; unsigned short checksum; unsigned short identifier; char crap[16536]; } response;

sd = socket(AFINET, SOCKRAW, IPPROTO_ICMP);

bzero((char *)&sock,sizeof(struct sockaddrin)); sock.sinfamily=AFINET; sock.sinaddr = target; if (debugging > 1) printf(" Sending 3 64 byte raw pings to host.\n"); gettimeofday(&start, NULL); while(--retries) { if ((res = sendto(sd,(char *) ping,64,0,(struct sockaddr *)&sock, sizeof(struct sockaddr))) != 64) { fprintf(stderr, "sendto in isup returned %d! skipping host.\n", res); return 0; } FDZERO(&fdread); FDSET(sd, &fdread); tv.tvsec = 0; tv.tvusec = 1e6 * (PINGTIMEOUT / 3.0); while(1) { if ((res = select(sd + 1, &fdread, NULL, NULL, &tv)) != 1) break; else { read(sd,&response,sizeof(response)); if (response.ip.saddr == target.saddr && !response.type && !response.code && response.identifier == 31337) { gettimeofday(&end, NULL); globalrtt = (end.tvsec - start.tvsec) * 1e6 + end.tvusec - start.tvusec; ouraddr.s_addr = response.ip.daddr; close(sd); return 1;
} } } } close(sd); return 0; }

portlist synscan(struct inaddr target, unsigned short *portarray, struct inaddr *source, int fragment, portlist *ports) { int i=0, j=0, received, bytes, starttime; struct sockaddrin from; int fromsize = sizeof(struct sockaddrin); int sockets[maxparallelsockets]; struct timeval tv; char packet[65535]; struct iphdr *ip = (struct iphdr *) packet; struct tcphdr *tcp = (struct tcphdr *) (packet + sizeof(struct iphdr)); fdset fdread, fdwrite; int res; struct hostent *myhostent; char myname[MAXHOSTNAMELEN + 1]; int source_malloc = 0;

FDZERO(&fdread); FDZERO(&fdwrite);

tv.tvsec = 7; tv.tvusec = 0;

if ((received = socket(AFINET, SOCKRAW, IPPROTOTCP)) < 0 ) perror("socket trobles in synscan"); unblocksocket(received); FDSET(received, &fd_read);

/* First we take what is given to us as source. If that isn't valid, we take what should have swiped from the echo reply in our ping function. If THAT doesn't work either, we try to determine our address with gethostname and gethostbyname. Whew! / if (!source) { if (ouraddr.s_addr) { source = &ouraddr; } else { source = safe_malloc(sizeof(struct in_addr)); source_malloc = 1; if (gethostname(myname, MAXHOSTNAMELEN) || !(myhostent = gethostbyname(myname))) fatal("Your system is fucked up.\n"); memcpy(source, myhostent->h_addr_list[0], sizeof(struct in_addr)); } if (debugging) printf("We skillfully deduced that your address is %s\n", inet_ntoa(source)); }

starttime = time(NULL);

do { for(i=0; i < maxparallelsockets && portarray[j]; i++) { if ((sockets[i] = socket(AFINET, SOCKRAW, IPPROTORAW)) < 0 ) perror("socket trobles in synscan"); else { if (fragment) sendsmallfragz(sockets[i], source, &target, MAGICPORT, portarray[j++], THSYN); else sendtcpraw(sockets[i], source , &target, MAGICPORT, portarray[j++],0,0,THSYN,0,0,0); usleep(10000); } } if ((res = select(received + 1, &fdread, NULL, NULL, &tv)) < 0) perror("select problems in synscan"); else if (res > 0) { while ((bytes = recvfrom(received, packet, 65535, 0, (struct sockaddr )&from, &fromsize)) > 0 ) { if (ip->saddr == target.s_addr) { if (tcp->th_flags & TH_RST) { if (debugging > 1) printf("Nothing open on port %d\n", ntohs(tcp->th_sport)); } else /if (tcp->thflags & THSYN && tcp->thflags & THACK)*/ { if (debugging || verbose) {
printf("Possible catch on port %d! Here it is:\n", ntohs(tcp->thsport)); readtcppacket(packet,1); } addport(ports, ntohs(tcp->thsport), IPPROTOTCP, NULL);
} } } } for(i=0; i < max
parallel_sockets && portarray[j]; i++) close(sockets[i]);

} while (portarray[j]); if (debugging || verbose) printf("The TCP SYN scan took %ld seconds to scan %d ports.\n", time(NULL) - starttime, numberofports); if (source_malloc) free(source); /* Gotta save those 4 bytes! ;) */ close(received); return *ports; }

int sendtcpraw( int sd, struct inaddr *source, struct inaddr *victim, unsigned short sport, unsigned short dport, unsigned long seq, unsigned long ack, unsigned char flags, unsigned short window, char *data, unsigned short datalen) {

struct pseudoheader { /*for computing TCP checksum, see TCP/IP Illustrated p. 145 */ unsigned long saddr; unsigned long daddr; char zer0; unsigned char protocol; unsigned short length; }; char packet[sizeof(struct iphdr) + sizeof(struct tcphdr) + datalen]; /*With these placement we get data and some field alignment so we aren't wasting too much in computing the checksum */ struct iphdr *ip = (struct iphdr *) packet; struct tcphdr *tcp = (struct tcphdr *) (packet + sizeof(struct iphdr)); struct pseudoheader *pseudo = (struct pseudoheader *) (packet + sizeof(struct iphdr) - sizeof(struct pseudoheader)); int res; struct sockaddrin sock; char myname[MAXHOSTNAMELEN + 1]; struct hostent *myhostent; int sourcemalloced = 0;

/* check that required fields are there and not too silly */ if ( !victim || !sport || !dport || sd < 0) { fprintf(stderr, "sendtcpraw: One or more of your parameters suck!\n"); return -1; }

/* if they didn't give a source address, fill in our first address / if (!source) { source_malloced = 1; source = safe_malloc(sizeof(struct in_addr)); if (gethostname(myname, MAXHOSTNAMELEN) || !(myhostent = gethostbyname(myname))) fatal("Your system is fucked up.\n"); memcpy(source, myhostent->h_addr_list[0], sizeof(struct in_addr)); if (debugging > 1) printf("We skillfully deduced that your address is %s\n", inet_ntoa(source)); }

/*do we even have to fill out this damn thing? This is a raw packet, after all */ sock.sinfamily = AFINET; sock.sinport = htons(dport); sock.sinaddr.saddr = victim->saddr;

bzero(packet, sizeof(struct iphdr) + sizeof(struct tcphdr));

pseudo->saddr = source->saddr; pseudo->daddr = victim->saddr; pseudo->protocol = IPPROTO_TCP; pseudo->length = htons(sizeof(struct tcphdr) + datalen);

tcp->thsport = htons(sport); tcp->thdport = htons(dport); if (seq) tcp->thseq = htonl(seq); else tcp->thseq = rand() + rand();

if (flags & THACK && ack) tcp->thack = htonl(seq); else if (flags & THACK) tcp->thack = rand() + rand();

tcp->thoff = 5 /*words*/; tcp->thflags = flags;

if (window) tcp->thwin = window; else tcp->thwin = htons(2048); /* Who cares */

tcp->thsum = incksum((unsigned short *)pseudo, sizeof(struct tcphdr) + sizeof(struct pseudo_header) + datalen);

/* Now for the ip header */ bzero(packet, sizeof(struct iphdr)); ip->version = 4; ip->ihl = 5; ip->totlen = htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + datalen); ip->id = rand(); ip->ttl = 255; ip->protocol = IPPROTOTCP; ip->saddr = source->saddr; ip->daddr = victim->saddr; ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr));

if (debugging > 1) { printf("Raw TCP packet creation completed! Here it is:\n"); readtcppacket(packet,ntohs(ip->totlen)); } if (debugging > 1) printf("\nTrying sendto(%d , packet, %d, 0 , %s , %d)\n", sd, ntohs(ip->totlen), inetntoa(*victim), sizeof(struct sockaddrin)); if ((res = sendto(sd, packet, ntohs(ip->totlen), 0, (struct sockaddr *)&sock, sizeof(struct sockaddrin))) == -1) { perror("sendto in sendtcpraw"); if (sourcemalloced) free(source); return -1; } if (debugging > 1) printf("successfully sent %d bytes of rawtcp!\n", res);

if (source_malloced) free(source); return res; }

/* A simple program I wrote to help in debugging, shows the important fields of a TCP packet*/ int readtcppacket(char *packet, int readdata) { struct iphdr *ip = (struct iphdr *) packet; struct tcphdr *tcp = (struct tcphdr *) (packet + sizeof(struct iphdr)); char *data = packet + sizeof(struct iphdr) + sizeof(struct tcphdr); int totlen; struct inaddr bullshit, bullshit2; char sourcehost[16]; int i;

if (!packet) { fprintf(stderr, "readtcppacket: packet is NULL!\n"); return -1; } bullshit.saddr = ip->saddr; bullshit2.saddr = ip->daddr; totlen = ntohs(ip->totlen); strncpy(sourcehost, inetntoa(bullshit), 16); i = 4 * (ntohs(ip->ihl) + ntohs(tcp->thoff)); if (ip->protocol == IPPROTOTCP) if (ip->fragoff) printf("Packet is fragmented, offset field: %u", ip->fragoff); else { printf("TCP packet: %s:%d -> %s:%d (total: %d bytes)\n", sourcehost, ntohs(tcp->thsport), inetntoa(bullshit2), ntohs(tcp->thdport), totlen); printf("Flags: "); if (!tcp->thflags) printf("(none)"); if (tcp->thflags & THRST) printf("RST "); if (tcp->thflags & THSYN) printf("SYN "); if (tcp->thflags & THACK) printf("ACK "); if (tcp->thflags & THPUSH) printf("PSH "); if (tcp->thflags & THFIN) printf("FIN "); if (tcp->thflags & THURG) printf("URG "); printf("\n"); printf("ttl: %hi ", ip->ttl); if (tcp->thflags & (THSYN | THACK)) printf("Seq: %lu\tAck: %lu\n", tcp->thseq, tcp->thack); else if (tcp->thflags & THSYN) printf("Seq: %lu\n", ntohl(tcp->thseq)); else if (tcp->thflags & THACK) printf("Ack: %lu\n", ntohl(tcp->thack)); } if (readdata && i < totlen) { printf("Data portion:\n"); while(i < tot_len) printf("%2X%c", data[i], (++i%16)? ' ' : '\n'); printf("\n"); } return 0; }

/* We don't exactly need real crypto here (thank god!)\n"*/ int shortfry(unsigned short *ports) { int num; unsigned short tmp; int i;

for(i=0; i < numberofports; i++) { num = rand() % (numberofports); tmp = ports[i]; ports[i] = ports[num]; ports[num] = tmp; } return 1; }

/* Much of this is swiped from my sendtcpraw function above, which doesn't support fragmentation */ int sendsmallfragz(int sd, struct inaddr *source, struct inaddr *victim, int sport, int dport, int flags) {

struct pseudoheader { /*for computing TCP checksum, see TCP/IP Illustrated p. 145 */ unsigned long saddr; unsigned long daddr; char zer0; unsigned char protocol; unsigned short length; }; /*In this placement we get data and some field alignment so we aren't wasting too much to compute the TCP checksum.*/ char packet[sizeof(struct iphdr) + sizeof(struct tcphdr) + 100]; struct iphdr *ip = (struct iphdr *) packet; struct tcphdr *tcp = (struct tcphdr *) (packet + sizeof(struct iphdr)); struct pseudoheader *pseudo = (struct pseudoheader *) (packet + sizeof(struct iphdr) - sizeof(struct pseudoheader)); char *frag2 = packet + sizeof(struct iphdr) + 16; struct iphdr *ip2 = (struct iphdr *) (frag2 - sizeof(struct iphdr)); int res; struct sockaddr_in sock; int id;

/*Why do we have to fill out this damn thing? This is a raw packet, after all */ sock.sinfamily = AFINET; sock.sinport = htons(dport); sock.sinaddr.saddr = victim->saddr;

bzero(packet, sizeof(struct iphdr) + sizeof(struct tcphdr));

pseudo->saddr = source->saddr; pseudo->daddr = victim->saddr; pseudo->protocol = IPPROTO_TCP; pseudo->length = htons(sizeof(struct tcphdr));

tcp->thsport = htons(sport); tcp->thdport = htons(dport); tcp->th_seq = rand() + rand();

tcp->thoff = 5 /*words*/; tcp->thflags = flags;

tcp->th_win = htons(2048); /* Who cares */

tcp->thsum = incksum((unsigned short *)pseudo, sizeof(struct tcphdr) + sizeof(struct pseudo_header));

/* Now for the ip header of frag1 / bzero(packet, sizeof(struct iphdr)); ip->version = 4; ip->ihl = 5; /RFC 791 allows 8 octet frags, but I get "operation not permitted" (EPERM) when I try that. */ ip->totlen = htons(sizeof(struct iphdr) + 16); id = ip->id = rand(); ip->fragoff = htons(MOREFRAGMENTS); ip->ttl = 255; ip->protocol = IPPROTOTCP; ip->saddr = source->saddr; ip->daddr = victim->saddr; ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr));

if (debugging > 1) { printf("Raw TCP packet fragment #1 creation completed! Here it is:\n"); hdump(packet,20); } if (debugging > 1) printf("\nTrying sendto(%d , packet, %d, 0 , %s , %d)\n", sd, ntohs(ip->totlen), inetntoa(*victim), sizeof(struct sockaddrin)); if ((res = sendto(sd, packet, ntohs(ip->totlen), 0, (struct sockaddr *)&sock, sizeof(struct sockaddrin))) == -1) { perror("sendto in sendsynfragz"); return -1; } if (debugging > 1) printf("successfully sent %d bytes of rawtcp!\n", res);

/* Create the second fragment / bzero(ip2, sizeof(struct iphdr)); ip2->version = 4; ip2->ihl = 5; ip2->tot_len = htons(sizeof(struct iphdr) + 4); / the rest of our TCP packet / ip2->id = id; ip2->frag_off = htons(2); ip2->ttl = 255; ip2->protocol = IPPROTO_TCP; ip2->saddr = source->s_addr; ip2->daddr = victim->s_addr; ip2->check = in_cksum((unsigned short *)ip2, sizeof(struct iphdr)); if (debugging > 1) { printf("Raw TCP packet fragment creation completed! Here it is:\n"); hdump(packet,20); } if (debugging > 1) printf("\nTrying sendto(%d , ip2, %d, 0 , %s , %d)\n", sd, ntohs(ip2->tot_len), inet_ntoa(victim), sizeof(struct sockaddrin)); if ((res = sendto(sd, ip2, ntohs(ip2->totlen), 0, (struct sockaddr *)&sock, sizeof(struct sockaddrin))) == -1) { perror("sendto in sendtcp_raw"); return -1; } return 1; }

/* Hex dump */ void hdump(unsigned char *packet, int len) { unsigned int i=0, j=0;

printf("Here it is:\n");

for(i=0; i < len; i++){ j = (unsigned) (packet[i]); printf("%-2X ", j); if (!((i+1)%16)) printf("\n"); else if (!((i+1)%4)) printf(" "); } printf("\n"); }

portlist finscan(struct inaddr target, unsigned short *portarray, struct in_addr *source, int fragment, portlist *ports) {

int rawsd, tcpsd; int done = 0, badport, starttime, someleft, i, j=0, retries=2; int sourcemalloc = 0; int waitingperiod = retries, sockaddrinsize = sizeof(struct sockaddrin); int bytes, dupesinarow = 0; unsigned long timeout; struct hostent *myhostent; char response[65535], myname[513]; struct iphdr *ip = (struct iphdr *) response; struct tcphdr *tcp; unsigned short portno[maxparallelsockets], trynum[maxparallelsockets]; struct sockaddrin stranger;

timeout = (globaldelay)? globaldelay : (globalrtt)? (1.2 * globalrtt) + 10000 : 1e5; bzero(&stranger, sockaddrinsize); bzero(portno, maxparallelsockets * sizeof(unsigned short)); bzero(trynum, maxparallelsockets * sizeof(unsigned short)); starttime = time(NULL);

if (debugging || verbose) printf("Initiating FIN stealth scan against %s (%s), sleep delay: %ld useconds\n", currentname, inetntoa(target), timeout);

if (!source) { if (ouraddr.saddr) { source = &ouraddr; } else { source = safemalloc(sizeof(struct inaddr)); sourcemalloc = 1; if (gethostname(myname, MAXHOSTNAMELEN) || !(myhostent = gethostbyname(myname))) fatal("Your system is fucked up.\n"); memcpy(source, myhostent->haddrlist[0], sizeof(struct inaddr)); } if (debugging || verbose) printf("We skillfully deduced that your address is %s\n", inetntoa(*source)); }

if ((rawsd = socket(AFINET, SOCKRAW, IPPROTORAW)) < 0 ) perror("socket trobles in finscan");

if ((tcpsd = socket(AFINET, SOCKRAW, IPPROTOTCP)) < 0 ) perror("socket trobles in finscan");

unblocksocket(tcpsd); while(!done) { for(i=0; i < maxparallelsockets; i++) { if (!portno[i] && portarray[j]) { portno[i] = portarray[j++]; } if (portno[i]) { if (fragment) sendsmallfragz(rawsd, source, &target, MAGICPORT, portno[i], THFIN); else sendtcpraw(rawsd, source , &target, MAGICPORT, portno[i], 0, 0, TH_FIN, 0, 0, 0); usleep(10000); /* WE normally do not need this, but the target lamer often does */ } }

usleep(timeout); dupesinarow = 0; while ((bytes = recvfrom(tcpsd, response, 65535, 0, (struct sockaddr ) &stranger, &sockaddr_in_size)) > 0) if (ip->saddr == target.s_addr) { tcp = (struct tcphdr *) (response + 4 * ip->ihl); if (tcp->th_flags & TH_RST) { badport = ntohs(tcp->th_sport); if (debugging > 1) printf("Nothing open on port %d\n", badport); / delete the port from active scanning */ for(i=0; i < maxparallelsockets; i++) if (portno[i] == badport) { if (debugging && trynum[i] > 0) printf("Bad port %d caught on fin scan, try number %d\n", badport, trynum[i] + 1); trynum[i] = 0; portno[i] = 0; break; } if (i == maxparallelsockets) { if (debugging) printf("Late packet or dupe, deleting port %d.\n", badport); dupesinarow++; if (ports) deleteport(ports, badport, IPPROTOTCP); } } else if (debugging > 1) {
printf("Strange packet from target%d! Here it is:\n", ntohs(tcp->th
sport)); if (bytes >= 40) readtcppacket(response,1); else hdump(response,bytes); } }

/* adjust waiting time if neccessary */ if (dupesinarow > 6) { if (debugging || verbose) printf("Slowing down send frequency due to multiple late packets.\n"); if (timeout < 10 * ((globaldelay)? globaldelay: globalrtt + 20000)) timeout *= 1.5; else { printf("Too many late packets despite send frequency decreases, skipping scan.\n"); if (sourcemalloc) free(source); return *ports; } }

/* Ok, collect good ports (those that we haven't received responses too after all our retries */ someleft = 0; for(i=0; i < maxparallelsockets; i++) if (portno[i]) { if (++trynum[i] >= retries) { if (verbose || debugging) printf("Good port %d detected by finscan!\n", portno[i]); addport(ports, portno[i], IPPROTOTCP, NULL); sendtcpraw( rawsd, source, &target, MAGICPORT, portno[i], 0, 0, THFIN, 0, 0, 0); portno[i] = trynum[i] = 0; } else someleft = 1; }

if (!portarray[j] && (!someleft || --waiting_period <= 0)) done++; }

if (debugging || verbose) printf("The TCP stealth FIN scan took %ld seconds to scan %d ports.\n", time(NULL) - starttime, numberofports); if (source_malloc) free(source); close(tcpsd); close(rawsd); return *ports; }

int ftpanonconnect(struct ftpinfo *ftp) { int sd; struct sockaddr_in sock; int res; char recvbuf[2048]; char command[512];

if (verbose || debugging) printf("Attempting connection to ftp://%s:%s@%s:%i\n", ftp->user, ftp->pass, ftp->server_name, ftp->port);

if ((sd = socket(AFINET, SOCKSTREAM, IPPROTOTCP)) < 0) { perror("Couldn't create ftpanon_connect socket"); return 0; }

sock.sinfamily = AFINET; sock.sinaddr.saddr = ftp->server.saddr; sock.sinport = htons(ftp->port); res = connect(sd, (struct sockaddr *) &sock, sizeof(struct sockaddr_in)); if (res < 0 ) { printf("Your ftp bounce proxy server won't talk to us!\n"); exit(1); } if (verbose || debugging) printf("Connected:"); while ((res = recvtime(sd, recvbuf, 2048,7)) > 0) if (debugging || verbose) { recvbuf[res] = '\0'; printf("%s", recvbuf); } if (res < 0) { perror("recv problem from ftp bounce server"); exit(1); }

snprintf(command, 511, "USER %s\r\n", ftp->user); send(sd, command, strlen(command), 0); res = recvtime(sd, recvbuf, 2048,12); if (res <= 0) { perror("recv problem from ftp bounce server"); exit(1); } recvbuf[res] = '\0'; if (debugging) printf("sent username, received: %s", recvbuf); if (recvbuf[0] == '5') { printf("Your ftp bounce server doesn't like the username \"%s\"\n", ftp->user); exit(1); } snprintf(command, 511, "PASS %s\r\n", ftp->pass); send(sd, command, strlen(command), 0); res = recvtime(sd, recvbuf, 2048,12); if (res < 0) { perror("recv problem from ftp bounce server\n"); exit(1); } if (!res) printf("Timeout from bounce server ..."); else { recvbuf[res] = '\0'; if (debugging) printf("sent password, received: %s", recvbuf); if (recvbuf[0] == '5') { fprintf(stderr, "Your ftp bounce server refused login combo (%s/%s)\n", ftp->user, ftp->pass); exit(1); } } while ((res = recvtime(sd, recvbuf, 2048,2)) > 0) if (debugging) { recvbuf[res] = '\0'; printf("%s", recvbuf); } if (res < 0) { perror("recv problem from ftp bounce server"); exit(1); } if (verbose) printf("Login credentials accepted by ftp server!\n");

ftp->sd = sd; return sd; }

int recvtime(int sd, char *buf, int len, int seconds) {

int res; struct timeval timeout = {seconds, 0}; fd_set readfd;

FDZERO(&readfd); FDSET(sd, &readfd); res = select(sd + 1, &readfd, NULL, NULL, &timeout); if (res > 0 ) { res = recv(sd, buf, len, 0); if (res >= 0) return res; perror("recv in recvtime"); return 0; } else if (!res) return 0; perror("select() in recvtime"); return -1; }

portlist bouncescan(struct inaddr target, unsigned short portarray, struct ftpinfo *ftp, portlist *ports) { int starttime, res , sd = ftp->sd, i=0; char *t = (char *)⌖ int retriesleft = FTP_RETRIES; char recvbuf[2048]; char targetstr[20]; char command[512]; snprintf(targetstr, 20, "%d,%d,%d,%d,0,", UC(t[0]), UC(t[1]), UC(t[2]), UC(t[3])); starttime = time(NULL); if (verbose || debugging) printf("Initiating TCP ftp bounce scan against %s (%s)\n", current_name, inet_ntoa(target)); for(i=0; portarray[i]; i++) { snprintf(command, 512, "PORT %s%i\r\n", targetstr, portarray[i]); if (send(sd, command, strlen(command), 0) < 0 ) { perror("send in bounce_scan"); if (retriesleft) { if (verbose || debugging) printf("Our ftp proxy server hung up on us! retrying\n"); retriesleft--; close(sd); ftp->sd = ftp_anon_connect(ftp); if (ftp->sd < 0) return *ports; sd = ftp->sd; i--; } else { fprintf(stderr, "Our socket descriptor is dead and we are out of retries. Giving up.\n"); close(sd); ftp->sd = -1; return *ports; } } else { / Our send is good */ res = recvtime(sd, recvbuf, 2048,15); if (res <= 0) perror("recv problem from ftp bounce server\n");

else { /* our recv is good */
  recvbuf[res] = '\0';
  if (debugging) printf("result of port query on port %i: %s", 
            portarray[i],  recvbuf);
  if (recvbuf[0] == '5') {
if (portarray[i] > 1023) {
fprintf(stderr, "Your ftp bounce server sucks, it won't let us feed bogus ports!\n");
exit(1);
  }
  else {
fprintf(stderr, "Your ftp bounce server doesn't allow priviliged ports, skipping them.\n");
while(portarray[i] && portarray[i] < 1024) i++;
if (!portarray[i]) {
  fprintf(stderr, "And you didn't want to scan any unpriviliged ports.  Giving up.\n");
  /*      close(sd);
  ftp->sd = -1;
  return *ports;*/
  /* screw this gentle return crap!  This is an emergency! */
  exit(1);
}
  }  
  }
else  /* Not an error message */
  if (send(sd, "LIST\r\n", 6, 0) > 0 ) {
res = recvtime(sd, recvbuf, 2048,12);
if (res <= 0)  perror("recv problem from ftp bounce server\n");
else {
  recvbuf[res] = '\0';
  if (debugging) printf("result of LIST: %s", recvbuf);
  if (!strncmp(recvbuf, "500", 3)) {
    /* fuck, we are not aligned properly */
    if (verbose || debugging)
      printf("misalignment detected ... correcting.\n");
     res = recvtime(sd, recvbuf, 2048,10);
  }
  if (recvbuf[0] == '1' || recvbuf[0] == '2') {
    if (verbose || debugging) printf("Port number %i appears good.\n",
            portarray[i]);
    addport(ports, portarray[i], IPPROTO_TCP, NULL);
    if (recvbuf[0] == '1') {
    res = recvtime(sd, recvbuf, 2048,5);
    recvbuf[res] = '\0';
    if ((res > 0) && debugging) printf("nxt line: %s", recvbuf);
    }
  }
}
  }
}

} } if (debugging || verbose) printf("Scanned %d ports in %ld seconds via the Bounce scan.\n", numberofports, time(NULL) - starttime); return *ports; }

/* parse a URL stype ftp string of the form user:pass@server:portno / int parse_bounce(struct ftpinfo *ftp, char *url) { char *p = url,q, *s;

if ((q = strrchr(url, '@'))) /we have username and/or pass */ { *(q++) = '\0'; if ((s = strchr(q, ':'))) { / has portno */ *(s++) = '\0'; strncpy(ftp->servername, q, MAXHOSTNAMELEN); ftp->port = atoi(s); } else strncpy(ftp->servername, q, MAXHOSTNAMELEN);

if ((s = strchr(p, ':'))) { /* User AND pass given / *(s++) = '\0'; strncpy(ftp->user, p, 63); strncpy(ftp->pass, s, 255); } else { / Username ONLY given / printf("Assuming %s is a username, and using the default password: %s\n", p, ftp->pass); strncpy(ftp->user, p, 63); } } else / no username or password given / if ((s = strchr(url, ':'))) { / portno is given / *(s++) = '\0'; strncpy(ftp->server_name, url, MAXHOSTNAMELEN); ftp->port = atoi(s); } else / default case, no username, password, or portnumber */ strncpy(ftp->server_name, url, MAXHOSTNAMELEN);

ftp->user[63] = ftp->pass[255] = ftp->server_name[MAXHOSTNAMELEN] = 0;

return 1; }

/* * I'll bet you've never seen this function before (yeah right)! * standard swiped checksum routine. */ unsigned short in_cksum(unsigned short *ptr,int nbytes) {

register long sum; /* assumes long == 32 bits / u_short oddbyte; register u_short answer; / assumes u_short == 16 bits */

/* * Our algorithm is simple, using a 32-bit accumulator (sum), * we add sequential 16-bit words to it, and at the end, fold back * all the carry bits from the top 16 bits into the lower 16 bits. */

sum = 0; while (nbytes > 1) { sum += *ptr++; nbytes -= 2; }

/* mop up an odd byte, if necessary / if (nbytes == 1) { oddbyte = 0; / make sure top half is zero / *((u_char *) &oddbyte) = *(u_char *)ptr; / one byte only */ sum += oddbyte; }

/* * Add back carry outs from top 16 bits to low 16 bits. */

sum = (sum >> 16) + (sum & 0xffff); /* add high-16 to low-16 / sum += (sum >> 16); / add carry / answer = ~sum; / ones-complement, then truncate to 16 bits */ return(answer); } <-->

----[ EOF