utils.cc
来自「Ubuntu packages of security software。 相」· CC 代码 · 共 1,028 行 · 第 1/3 页
CC
1,028 行
void bintohexstr(char *buf, int buflen, char *src, int srclen){ int bp=0; int i; for(i=0; i<srclen; i++){ bp += Snprintf(buf+bp, buflen-bp, "\\x%02hhx",src[i]); if(bp >= buflen)break; if(i%16==7){ bp += Snprintf(buf+bp, buflen-bp," "); if(bp >= buflen)break; } if(i%16==15){ bp += Snprintf(buf+bp, buflen-bp,"\n"); if(bp >= buflen)break; } } if(i%16!=0 && bp < buflen) bp += Snprintf(buf+bp, buflen-bp,"\n");}static inline char* STRAPP(char *fmt, ...) { static char buf[256]; static int bp; int left = (int)sizeof(buf)-bp; if(!fmt){ bp = 0; return(buf); } if (left <= 0) return buf; va_list ap; va_start(ap, fmt); bp += Vsnprintf (buf+bp, left, fmt, ap); va_end(ap); return(buf);}#define HEXDUMP -2#define UNKNOWN -1#define BREAK() \ {option_type = HEXDUMP; break;}#define CHECK(tt) \ if(tt >= option_end) \ {option_type = HEXDUMP; break;}/* It tries to decode ip options. Returns static buffer. watch out. */char* print_ip_options(u8* ipopt, int ipoptlen) { char ipstring[32]; int option_type = UNKNOWN;// option type int option_len = 0; // option length int option_pt = 0; // option pointer int option_fl = 0; // option flag u8 *tptr; // temp pointer u32 *tint; // temp int int option_sta = 0; // option start offset int option_end = 0; // option end offset int pt = 0; // current offset // clear buffer STRAPP(NULL,NULL); if(!ipoptlen) return(NULL); while(pt<ipoptlen){ // for every char in ipopt // read ip option header if(option_type == UNKNOWN) { option_sta = pt; option_type = ipopt[pt++]; if(option_type != 0 && option_type != 1) { // should we be interested in length field? if(pt >= ipoptlen) // no more chars {option_type = HEXDUMP;pt--; option_end = 255; continue;} // no length field, hex dump to the end option_len = ipopt[pt++]; // end must not be greater than length option_end = MIN(option_sta + option_len, ipoptlen); // end must not be smaller than current position option_end = MAX(option_end, option_sta+2); } } switch(option_type) { case 0: // IPOPT_END STRAPP(" EOL", NULL); option_type = UNKNOWN; break; case 1: // IPOPT_NOP STRAPP(" NOP", NULL); option_type = UNKNOWN; break;/* case 130: // IPOPT_SECURITY option_type=-1; break;*/ case 131: // IPOPT_LSRR -> Loose Source and Record Route case 137: // IPOPT_SSRR -> Strict Source and Record Route case 7: // IPOPT_RR -> Record Route if(pt - option_sta == 2) { STRAPP(" %s%s{", (option_type==131)?"LS":(option_type==137)?"SS":"", "RR"); // option pointer CHECK(pt); option_pt = ipopt[pt++]; if(option_pt%4 != 0 || (option_sta + option_pt-1)>option_end || option_pt<4) //bad or too big pointer STRAPP(" [bad ptr=%02i]", option_pt); } if(pt - option_sta > 2) { // ip's int i, s = (option_pt)%4; // if pointer is mangled, fix it. it's max 3 bytes wrong CHECK(pt+3); for(i=0; i<s; i++) STRAPP("\\x%02x", ipopt[pt++]); option_pt -= i; // okay, now we can start printing ip's CHECK(pt+3); tptr = &ipopt[pt]; pt+=4; if(inet_ntop(AF_INET, (char *) tptr, ipstring, sizeof(ipstring)) == NULL) fatal("Failed to convert target address to presentation format!?! Error: %s", strerror(socket_errno())); STRAPP("%c%s",(pt-3-option_sta)==option_pt?'#':' ', ipstring); if(pt == option_end) STRAPP("%s",(pt-option_sta)==(option_pt-1)?"#":""); // pointer in the end? }else BREAK(); break; case 68: // IPOPT_TS -> Internet Timestamp if(pt - option_sta == 2){ STRAPP(" TM{"); // pointer CHECK(pt); option_pt = ipopt[pt++]; // bad or too big pointer if(option_pt%4 != 1 || (option_sta + option_pt-1)>option_end || option_pt<5) STRAPP(" [bad ptr=%02i]", option_pt); // flags + overflow CHECK(pt); option_fl = ipopt[pt++]; if((option_fl&0x0C) || (option_fl&0x03)==2) STRAPP(" [bad flags=\\x%01hhx]", option_fl&0x0F); STRAPP("[%i hosts not recorded]", option_fl>>4); option_fl &= 0x03; } if(pt - option_sta > 2) {// ip's int i, s = (option_pt+3)%(option_fl==0?4:8); // if pointer is mangled, fix it. it's max 3 bytes wrong CHECK(pt+(option_fl==0?3:7)); for(i=0; i<s; i++) STRAPP("\\x%02x", ipopt[pt++]); option_pt-=i; // print pt STRAPP("%c",(pt+1-option_sta)==option_pt?'#':' '); // okay, first grab ip. if(option_fl!=0){ CHECK(pt+3); tptr = &ipopt[pt]; pt+=4; if(inet_ntop(AF_INET, (char *) tptr, ipstring, sizeof(ipstring)) == NULL) fatal("Failed to convert target address to presentation format!?! Error: %s", strerror(socket_errno())); STRAPP("%s@", ipstring); } CHECK(pt+3); tint = (u32*)&ipopt[pt]; pt+=4; STRAPP("%u", ntohl(*tint)); if(pt == option_end) STRAPP("%s",(pt-option_sta)==(option_pt-1)?"#":" "); }else BREAK(); break; case 136: // IPOPT_SATID -> (SANET) Stream Identifier if(pt - option_sta == 2){ u16 *sh; STRAPP(" SI{",NULL); // length if(option_sta+option_len > ipoptlen || option_len!=4) STRAPP("[bad len %02i]", option_len); // stream id CHECK(pt+1); sh = (u16*) &ipopt[pt]; pt+=2; option_pt = ntohs(*sh); STRAPP("id=%i", option_pt); if(pt != option_end) BREAK(); }else BREAK(); break; case UNKNOWN: default: // we read option_type and option_len, print them. STRAPP(" ??{\\x%02hhx\\x%02hhx", option_type, option_len); // check option_end once more: if(option_len < ipoptlen) option_end = MIN(MAX(option_sta+option_len, option_sta+2),ipoptlen); else option_end = 255; option_type = HEXDUMP; break; case HEXDUMP: assert(pt<=option_end); if(pt == option_end){ STRAPP("}",NULL); option_type=-1; break; } STRAPP("\\x%02hhx", ipopt[pt++]); break; } if(pt == option_end && option_type != UNKNOWN) { STRAPP("}",NULL); option_type = UNKNOWN; } } // while if(option_type != UNKNOWN) STRAPP("}"); return(STRAPP("",NULL));}#undef CHECK#undef BREAK#undef UNKNOWN#undef HEXDUMP/* mmap() an entire file into the address space. Returns a pointer to the beginning of the file. The mmap'ed length is returned inside the length parameter. If there is a problem, NULL is returned, the value of length is undefined, and errno is set to something appropriate. The user is responsible for doing an munmap(ptr, length) when finished with it. openflags should be O_RDONLY or O_RDWR, or O_WRONLY*/#ifndef WIN32char *mmapfile(char *fname, int *length, int openflags) { struct stat st; int fd; char *fileptr; if (!length || !fname) { errno = EINVAL; return NULL; } *length = -1; if (stat(fname, &st) == -1) { errno = ENOENT; return NULL; } fd = open(fname, openflags); if (fd == -1) { return NULL; } fileptr = (char *)mmap(0, st.st_size, (openflags == O_RDONLY)? PROT_READ : (openflags == O_RDWR)? (PROT_READ|PROT_WRITE) : PROT_WRITE, MAP_SHARED, fd, 0); close(fd);#ifdef MAP_FAILED if (fileptr == (void *)MAP_FAILED) return NULL;#else if (fileptr == (char *) -1) return NULL;#endif *length = st.st_size; return fileptr;}#else /* WIN32 *//* FIXME: From the looks of it, this function can only handle one mmaped file at a time (note how gmap is used).*//* I believe this was written by Ryan Permeh ( ryan@eeye.com) */static HANDLE gmap = NULL;char *mmapfile(char *fname, int *length, int openflags){ HANDLE fd; DWORD mflags, oflags; char *fileptr; if (!length || !fname) { WSASetLastError(EINVAL); return NULL; } if (openflags == O_RDONLY) { oflags = GENERIC_READ; mflags = PAGE_READONLY; } else { oflags = GENERIC_READ | GENERIC_WRITE; mflags = PAGE_READWRITE; } fd = CreateFile ( fname, oflags, // open flags 0, // do not share NULL, // no security OPEN_EXISTING, // open existing FILE_ATTRIBUTE_NORMAL, NULL); // no attr. template if (!fd) pfatal ("%s(%u): CreateFile()", __FILE__, __LINE__); *length = (int) GetFileSize (fd, NULL); gmap = CreateFileMapping (fd, NULL, mflags, 0, 0, NULL); if (!gmap) pfatal ("%s(%u): CreateFileMapping(), file '%s', length %d, mflags %08lX", __FILE__, __LINE__, fname, *length, mflags); fileptr = (char*) MapViewOfFile (gmap, oflags == GENERIC_READ ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0); if (!fileptr) pfatal ("%s(%u): MapViewOfFile()", __FILE__, __LINE__); CloseHandle (fd); if (o.debugging > 2) log_write(LOG_PLAIN, "%s(): fd %08lX, gmap %08lX, fileptr %08lX, length %d\n", __func__, (DWORD)fd, (DWORD)gmap, (DWORD)fileptr, *length); return fileptr;}/* FIXME: This only works if the file was mapped by mmapfile (and only works if the file is the most recently mapped one */int win32_munmap(char *filestr, int filelen){ if (gmap == 0) fatal("%s: no current mapping !\n", __func__); FlushViewOfFile(filestr, filelen); UnmapViewOfFile(filestr); CloseHandle(gmap); gmap = NULL; return 0;}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?