utils.cc

来自「Ubuntu packages of security software。 相」· CC 代码 · 共 1,028 行 · 第 1/3 页

CC
1,028
字号
/*************************************************************************** * utils.cc -- Various miscellaneous utility functions which defy          * * categorization :)                                                       * *                                                                         * ***********************IMPORTANT NMAP LICENSE TERMS************************ *                                                                         * * The Nmap Security Scanner is (C) 1996-2008 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: utils.cc 6633 2007-12-22 06:32:03Z fyodor $ */#include "nmap.h"#include "utils.h"#include "NmapOps.h"extern NmapOps o;/* Test a wildcard mask against a test string. Wildcard mask * can include '*' and '?' which work the same as they do * in /bin/sh (except it's case insensitive) * Return val of 1 means it DID match. 0 means it DIDN'T * - Doug Hoyte, 2005 */int wildtest(char *wild, char *test) {  int i;  while(*wild != '\0'  ||  *test != '\0') {    if (*wild == '*') {      /* --- Deal with multiple asterisks. --- */      while (wild[1] == '*') wild++;      /* --- Deal with terminating asterisks. --- */      if (wild[1] == '\0') return 1;      for(i=0; test[i]!='\0'; i++)        if ((tolower((int)wild[1]) == tolower((int)test[i]) || wild[1] == '?')            &&  wildtest(wild+1, test+i) == 1) return 1;      return 0;    }    /* --- '?' can't match '\0'. --- */    if (*wild == '?' && *test == '\0') return 0;    if (*wild != '?' && tolower((int)*wild) != tolower((int)*test)) return 0;    wild++; test++;  }  if (tolower((int)*wild) == tolower((int)*test)) return 1;  return 0;}/* Hex dump */void hdump(unsigned char *packet, unsigned int len) {unsigned int i=0, j=0;log_write(LOG_PLAIN, "Here it is:\n");for(i=0; i < len; i++){  j = (unsigned) (packet[i]);  log_write(LOG_PLAIN, "%-2X ", j);  if (!((i+1)%16))    log_write(LOG_PLAIN, "\n");  else if (!((i+1)%4))    log_write(LOG_PLAIN, "  ");}log_write(LOG_PLAIN, "\n");}/* A better version of hdump, from Lamont Granquist.  Modified slightly   by Fyodor (fyodor@insecure.org) */void lamont_hdump(char *cp, unsigned int length) {  /* stolen from tcpdump, then kludged extensively */  static const char asciify[] = "................................ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~.................................................................................................................................";  const u_short *sp;  const u_char *ap;  unsigned char *bp = (unsigned char *) cp;  u_int i, j;  int nshorts, nshorts2;  int padding;    log_write(LOG_PLAIN, "\n\t");  padding = 0;  sp = (u_short *)bp;  ap = (u_char *)bp;  nshorts = (u_int) length / sizeof(u_short);  nshorts2 = (u_int) length / sizeof(u_short);  i = 0;  j = 0;  while(1) {    while (--nshorts >= 0) {      log_write(LOG_PLAIN, " %04x", ntohs(*sp));      sp++;      if ((++i % 8) == 0)        break;    }    if (nshorts < 0) {      if ((length & 1) && (((i-1) % 8) != 0)) {        log_write(LOG_PLAIN, " %02x  ", *(u_char *)sp);        padding++;      }      nshorts = (8 - (nshorts2 - nshorts));      while(--nshorts >= 0) {        log_write(LOG_PLAIN, "     ");      }      if (!padding) log_write(LOG_PLAIN, "     ");    }    log_write(LOG_PLAIN, "  ");    while (--nshorts2 >= 0) {      log_write(LOG_PLAIN, "%c%c", asciify[*ap], asciify[*(ap+1)]);      ap += 2;      if ((++j % 8) == 0) {        log_write(LOG_PLAIN, "\n\t");        break;      }    }    if (nshorts2 < 0) {      if ((length & 1) && (((j-1) % 8) != 0)) {        log_write(LOG_PLAIN, "%c", asciify[*ap]);      }      break;    }  }  if ((length & 1) && (((i-1) % 8) == 0)) {    log_write(LOG_PLAIN, " %02x", *(u_char *)sp);    log_write(LOG_PLAIN, "                                       %c", asciify[*ap]);  }  log_write(LOG_PLAIN, "\n");}#ifndef HAVE_STRERRORchar *strerror(int errnum) {  static char buf[1024];  sprintf(buf, "your system is too old for strerror of errno %d\n", errnum);  return buf;}#endif/* Like the perl equivalent -- It removes the terminating newline from string   IF one exists.  It then returns the POSSIBLY MODIFIED string */char *chomp(char *string) {  int len = strlen(string);  if (len && string[len - 1] == '\n') {    if (len > 1 && string[len - 2] == '\r')      string[len - 2] = '\0';    else      string[len - 1] = '\0';  }  return string;}/* Compare a canonical option name (e.g. "max-scan-delay") with a   user-generated option such as "max_scan_delay" and returns 0 if the   two values are considered equivalant (for example, - and _ are   considered to be the same), nonzero otherwise. */int optcmp(const char *a, const char *b) {  while(*a && *b) {    if (*a == '_' || *a == '-') {      if (*b != '_' && *b != '-')	return 1;    }    else if (*a != *b)      return 1;    a++; b++;  }  if (*a || *b)    return 1;  return 0;}/* Scramble the contents of an array*/void genfry(unsigned char *arr, int elem_sz, int num_elem) {int i;unsigned int pos;unsigned char *bytes;unsigned char *cptr;unsigned short *sptr;unsigned int *iptr;unsigned char *tmp;int bpe;if (sizeof(unsigned char) != 1)  fatal("%s() requires 1 byte chars", __func__);if (num_elem < 2)  return; if (elem_sz == sizeof(unsigned short)) {   shortfry((unsigned short *)arr, num_elem);   return; }/* OK, so I am stingy with the random bytes! */if (num_elem < 256)   bpe = sizeof(unsigned char);else if (num_elem < 65536)  bpe = sizeof(unsigned short);else bpe = sizeof(unsigned int);bytes = (unsigned char *) safe_malloc(bpe * num_elem);tmp = (unsigned char *) safe_malloc(elem_sz);get_random_bytes(bytes, bpe * num_elem);cptr = bytes;sptr = (unsigned short *)bytes;iptr = (unsigned int *) bytes; for(i=num_elem - 1; i > 0; i--) {   if (num_elem < 256) {     pos = *cptr; cptr++;   }   else if (num_elem < 65536) {     pos = *sptr; sptr++;   } else {     pos = *iptr; iptr++;   }   pos %= i+1;   memcpy(tmp, arr + elem_sz * i, elem_sz);   memcpy(arr + elem_sz * i, arr + elem_sz * pos, elem_sz);   memcpy(arr + elem_sz * pos, tmp, elem_sz); } free(bytes); free(tmp);}void shortfry(unsigned short *arr, int num_elem) {int num;unsigned short tmp;int i;if (num_elem < 2)  return;  for(i= num_elem - 1; i > 0 ; i--) {   num = get_random_ushort() % (i + 1);   if (i == num) continue;   tmp = arr[i];   arr[i] = arr[num];   arr[num] = tmp; } 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?