📄 osscan.cc
字号:
/*************************************************************************** * osscan.cc -- Routines used for OS detection via TCP/IP fingerprinting. * * For more information on how this works in Nmap, see my paper at * * http://www.insecure.org/osdetect/ * * * ***********************IMPORTANT NMAP LICENSE TERMS************************ * * * The Nmap Security Scanner is (C) 1996-2006 Insecure.Com LLC. Nmap is * * also a registered trademark of Insecure.Com LLC. This program is free * * software; you may redistribute and/or modify it under the terms of the * * GNU General Public License as published by the Free Software * * Foundation; Version 2 with the clarifications and exceptions described * * below. This guarantees your right to use, modify, and redistribute * * this software under certain conditions. If you wish to embed Nmap * * technology into proprietary software, we sell alternative licenses * * (contact sales@insecure.com). Dozens of software vendors already * * license Nmap technology such as host discovery, port scanning, OS * * detection, and version detection. * * * * Note that the GPL places important restrictions on "derived works", yet * * it does not provide a detailed definition of that term. To avoid * * misunderstandings, we consider an application to constitute a * * "derivative work" for the purpose of this license if it does any of the * * following: * * o Integrates source code from Nmap * * o Reads or includes Nmap copyrighted data files, such as * * nmap-os-fingerprints or nmap-service-probes. * * o Executes Nmap and parses the results (as opposed to typical shell or * * execution-menu apps, which simply display raw Nmap output and so are * * not derivative works.) * * o Integrates/includes/aggregates Nmap into a proprietary executable * * installer, such as those produced by InstallShield. * * o Links to a library or executes a program that does any of the above * * * * The term "Nmap" should be taken to also include any portions or derived * * works of Nmap. This list is not exclusive, but is just meant to * * clarify our interpretation of derived works with some common examples. * * These restrictions only apply when you actually redistribute Nmap. For * * example, nothing stops you from writing and selling a proprietary * * front-end to Nmap. Just distribute it by itself, and point people to * * http://insecure.org/nmap/ to download Nmap. * * * * We don't consider these to be added restrictions on top of the GPL, but * * just a clarification of how we interpret "derived works" as it applies * * to our GPL-licensed Nmap product. This is similar to the way Linus * * Torvalds has announced his interpretation of how "derived works" * * applies to Linux kernel modules. Our interpretation refers only to * * Nmap - we don't speak for any other GPL products. * * * * If you have any questions about the GPL licensing restrictions on using * * Nmap in non-GPL works, we would be happy to help. As mentioned above, * * we also offer alternative license to integrate Nmap into proprietary * * applications and appliances. These contracts have been sold to dozens * * of software vendors, and generally include a perpetual license as well * * as providing for priority support and updates as well as helping to * * fund the continued development of Nmap technology. Please email * * sales@insecure.com for further information. * * * * As a special exception to the GPL terms, Insecure.Com LLC grants * * permission to link the code of this program with any version of the * * OpenSSL library which is distributed under a license identical to that * * listed in the included Copying.OpenSSL file, and distribute linked * * combinations including the two. You must obey the GNU GPL in all * * respects for all of the code used other than OpenSSL. If you modify * * this file, you may extend this exception to your version of the file, * * but you are not obligated to do so. * * * * If you received these files with a written license agreement or * * contract stating terms other than the terms above, then that * * alternative license agreement takes precedence over these comments. * * * * Source is provided to this software because we believe users have a * * right to know exactly what a program is going to do before they run it. * * This also allows you to audit the software for security holes (none * * have been found so far). * * * * Source code also allows you to port Nmap to new platforms, fix bugs, * * and add new features. You are highly encouraged to send your changes * * to fyodor@insecure.org for possible incorporation into the main * * distribution. By sending these changes to Fyodor or one the * * Insecure.Org development mailing lists, it is assumed that you are * * offering Fyodor and Insecure.Com LLC the unlimited, non-exclusive right * * to reuse, modify, and relicense the code. Nmap will always be * * available Open Source, but this is important because the inability to * * relicense code has caused devastating problems for other Free Software * * projects (such as KDE and NASM). We also occasionally relicense the * * code to third parties as discussed above. If you wish to specify * * special license conditions of your contributions, just say so when you * * send them. * * * * 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 at * * http://www.gnu.org/copyleft/gpl.html , or in the COPYING file included * * with Nmap. * * * ***************************************************************************//* $Id: osscan.cc 4228 2006-12-08 03:01:08Z fyodor $ */#include "osscan.h"#include "timing.h"#include "NmapOps.h"#include "nmap_tty.h"#if TIME_WITH_SYS_TIME# include <sys/time.h># include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endifu32 fake_seqs[6] = { 0x721CF9, 0x721D5F, 0x721DC5, 0x721E2B, 0x721E91, 0x721EF7 };extern NmapOps o;/* Note that a sport of 0 really will (try to) use zero as the source port rather than choosing a random one */static struct udpprobeinfo *send_closedudp_probe(int sd, struct eth_nfo *eth, const struct in_addr *victim, u16 sport, u16 dport) {static struct udpprobeinfo upi;static int myttl = 0;static u8 patternbyte = 0;static u16 id = 0; u8 packet[328]; /* 20 IP hdr + 8 UDP hdr + 300 data */struct ip *ip = (struct ip *) packet;udphdr_bsd *udp = (udphdr_bsd *) (packet + sizeof(struct ip));struct in_addr *source;int datalen = 300;unsigned char *data = packet + 28;unsigned short realcheck; /* the REAL checksum */int res;int decoy;struct pseudo_udp_hdr { struct in_addr source; struct in_addr dest; u8 zero; u8 proto; u16 length;} *pseudo = (struct pseudo_udp_hdr *) ((char *)udp - 12) ;if (!patternbyte) patternbyte = (get_random_uint() % 60) + 65;memset(data, patternbyte, datalen);while(!id) id = get_random_uint();/* check that required fields are there and not too silly */if ( !victim || !dport || (!eth && sd < 0)) { fprintf(stderr, "send_closedudp_probe: One or more of your parameters suck!\n"); return NULL;}if (!myttl) myttl = (time(NULL) % 14) + 51;for(decoy=0; decoy < o.numdecoys; decoy++) { source = &o.decoys[decoy]; memset((char *) packet, 0, sizeof(struct ip) + sizeof(udphdr_bsd)); udp->uh_sport = htons(sport); udp->uh_dport = htons(dport); udp->uh_ulen = htons(8 + datalen); /* Now the pseudo header for checksuming */ pseudo->source.s_addr = source->s_addr; pseudo->dest.s_addr = victim->s_addr; pseudo->proto = IPPROTO_UDP; pseudo->length = htons(sizeof(udphdr_bsd) + datalen); /* OK, now we should be able to compute a valid checksum */ realcheck = in_cksum((unsigned short *)pseudo, 20 /* pseudo + UDP headers */ + datalen);#if STUPID_SOLARIS_CHECKSUM_BUG udp->uh_sum = sizeof(udphdr_bsd) + datalen;#else udp->uh_sum = realcheck;#endif if ( o.badsum ) udp->uh_sum++; /* Goodbye, pseudo header! */ memset(pseudo, 0, sizeof(*pseudo)); /* Now for the ip header */ ip->ip_v = 4; ip->ip_hl = 5; ip->ip_len = htons(sizeof(struct ip) + sizeof(udphdr_bsd) + datalen); ip->ip_id = id; ip->ip_ttl = myttl; ip->ip_p = IPPROTO_UDP; ip->ip_src.s_addr = source->s_addr; ip->ip_dst.s_addr= victim->s_addr; upi.ipck = in_cksum((unsigned short *)ip, sizeof(struct ip));#if HAVE_IP_IP_SUM ip->ip_sum = upi.ipck;#endif /* OK, now if this is the real she-bang (ie not a decoy) then we stick all the inph0 in our upi */ if (decoy == o.decoyturn) { upi.iptl = 28 + datalen; upi.ipid = id; upi.sport = sport; upi.dport = dport; upi.udpck = realcheck; upi.udplen = 8 + datalen; upi.patternbyte = patternbyte; upi.target.s_addr = ip->ip_dst.s_addr; } if ((res = send_ip_packet(sd, eth, packet, ntohs(ip->ip_len))) == -1) { perror("send_ip_packet in send_closedupd_probe"); return NULL; }}return &upi;}static struct AVal *fingerprint_iptcppacket(struct ip *ip, int mss, u32 syn) { struct AVal *AVs; int length; int opcode; u16 tmpshort; char *p,*q; struct tcphdr *tcp = ((struct tcphdr *) (((char *) ip) + 4 * ip->ip_hl)); AVs = (struct AVal *) malloc(6 * sizeof(struct AVal)); /* Link them together */ AVs[0].next = &AVs[1]; AVs[1].next = &AVs[2]; AVs[2].next = &AVs[3]; AVs[3].next = &AVs[4]; AVs[4].next = &AVs[5]; AVs[5].next = NULL; /* First we give the "response" flag to say we did actually receive a packet -- this way we won't match a template with Resp=N */ AVs[0].attribute = "Resp"; strcpy(AVs[0].value, "Y"); /* Next we check whether the Don't Fragment bit is set */ AVs[1].attribute = "DF"; if(ntohs(ip->ip_off) & 0x4000) { strcpy(AVs[1].value,"Y"); } else strcpy(AVs[1].value, "N"); /* Now we do the TCP Window size */ AVs[2].attribute = "W"; sprintf(AVs[2].value, "%hX", ntohs(tcp->th_win)); /* Time for the ACK, the codes are: S = same as syn S++ = syn + 1 O = other */ AVs[3].attribute = "ACK"; if (ntohl(tcp->th_ack) == syn + 1) strcpy(AVs[3].value, "S++"); else if (ntohl(tcp->th_ack) == syn) strcpy(AVs[3].value, "S"); else strcpy(AVs[3].value, "O"); /* Now time for the flags ... they must be in this order: B = Bogus (64, not a real TCP flag) U = Urgent A = Acknowledgement P = Push R = Reset S = Synchronize F = Final */ AVs[4].attribute = "Flags"; p = AVs[4].value; if (tcp->th_flags & TH_ECE) *p++ = 'B'; if (tcp->th_flags & TH_URG) *p++ = 'U'; if (tcp->th_flags & TH_ACK) *p++ = 'A'; if (tcp->th_flags & TH_PUSH) *p++ = 'P'; if (tcp->th_flags & TH_RST) *p++ = 'R'; if (tcp->th_flags & TH_SYN) *p++ = 'S'; if (tcp->th_flags & TH_FIN) *p++ = 'F'; *p++ = '\0'; /* Now for the TCP options ... */ AVs[5].attribute = "Ops"; p = AVs[5].value; /* Partly swiped from /usr/src/linux/net/ipv4/tcp_input.c in Linux kernel */ length = (tcp->th_off * 4) - sizeof(struct tcphdr); q = ((char *)tcp) + sizeof(struct tcphdr); while(length > 0 && ((p - AVs[5].value) < (int) (sizeof(AVs[5].value) - 3))) { opcode=*q++; length--; if (!opcode) { *p++ = 'L'; /* End of List */ break; } else if (opcode == 1) { *p++ = 'N'; /* No Op */ } else if (opcode == 2) { *p++ = 'M'; /* MSS */ q++; memcpy(&tmpshort, q, 2); if(ntohs(tmpshort) == mss) *p++ = 'E'; /* Echoed */ q += 2; length -= 3; } else if (opcode == 3) { /* Window Scale */ *p++ = 'W'; q += 2; length -= 2; } else if (opcode == 8) { /* Timestamp */ *p++ = 'T'; q += 9; length -= 9; } } *p++ = '\0'; return AVs;}static struct AVal *fingerprint_portunreach(struct ip *ip, struct udpprobeinfo *upi) { struct icmp *icmp; struct ip *ip2; int numtests = 10; unsigned short checksum; unsigned short *checksumptr; udphdr_bsd *udp; struct AVal *AVs; int i; int current_testno = 0; unsigned char *datastart, *dataend; /* The very first thing we do is make sure this is the correct response */ if (ip->ip_p != IPPROTO_ICMP) { error("fingerprint_portunreach handed a non-ICMP packet!"); return NULL; } if (ip->ip_src.s_addr != upi->target.s_addr) return NULL; /* Not the person we sent to */ icmp = ((struct icmp *) (((char *) ip) + 4 * ip->ip_hl)); if (icmp->icmp_type != 3 || icmp->icmp_code != 3) return NULL; /* Not a port unreachable */ ip2 = (struct ip*) ((char *)icmp + 8); udp = (udphdr_bsd *) ((char *)ip2 + 20); /* The ports better match as well ... */ if (ntohs(udp->uh_sport) != upi->sport || ntohs(udp->uh_dport) != upi->dport) { return NULL; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -