1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Replaced nmap's hex dump functions with new hexdump() included in nbase. Before

Nmap used two functions: one of them, hdump(), just printed raw hex bytes 
(no ASCII equivalents) and the other one, lamont_hdump() had a bug when 
printing buffers where bufflen%16==3. A new function has been implemented 
from scratch, that basically produces the same output as Wireshark. 
Output looks like this:

0000   e8 60 65 86 d7 86 6d 30  35 97 54 87 ff 67 05 9e  .`e...m05.T..g..
0010   07 5a 98 c0 ea ad 50 d2  62 4f 7b ff e1 34 f8 fc  .Z....P.bO{..4..
0020   c4 84 0a 6a 39 ad 3c 10  63 b2 22 c4 24 40 f4 b1  ...j9.<.c.".$@..

Changes:

- The new hexdump() function has been added to nbase.
- Old hdump() and lamont_dump() have been removed from nmap's code.
- A wrapper to the new hexdump(), called nmap_hexdump(), has been added
  to nmap's utils.cc. The wrapper basically prints the buffer returned 
  by hexdump() using nmap's log_write() function.
This commit is contained in:
luis
2009-08-25 18:09:19 +00:00
parent d63fba1dd6
commit a5ca31db9e
5 changed files with 14 additions and 86 deletions

View File

@@ -317,7 +317,7 @@ int send_rpc_query(Target *target_host, unsigned short portno,
/* Simply send this sucker we have created ... */
do {
if (o.debugging > 1)
hdump((unsigned char *) rpch, sizeof(struct rpc_hdr));
nmap_hexdump((unsigned char *) rpch, sizeof(struct rpc_hdr));
res = sendto(udp_rpc_socket, (char *)rpch, sizeof(struct rpc_hdr), 0,
(struct sockaddr *) &sock, socklen);
if (res == -1)

View File

@@ -4264,9 +4264,9 @@ static bool get_pcap_result(UltraScanInfo *USI, struct timeval *stime) {
break;
default:
error("Unexpected ICMP type/code 3/%d unreachable packet:",
error("Unexpected ICMP type/code 3/%d unreachable packet:\n",
icmp->icmp_code);
hdump((unsigned char *)icmp, ntohs(ip->ip_len) -
nmap_hexdump((unsigned char *)icmp, ntohs(ip->ip_len) -
sizeof(struct ip));
break;
}

View File

@@ -2223,7 +2223,7 @@ if (!pd) fatal("NULL packet device passed to %s", __func__);
fatal("FATAL: %s: bogus caplen from libpcap (%d) on interface type %d", __func__, head.caplen, datalink);
}
error("FATAL: Unknown datalink type (%d). Caplen: %d; Packet:", datalink, head.caplen);
lamont_hdump(p, head.caplen);
nmap_hexdump((unsigned char*)p, head.caplen);
exit(1);
}

View File

@@ -137,89 +137,18 @@ int wildtest(char *wild, char *test) {
}
/* Wrapper for nbase function hexdump() */
void nmap_hexdump(unsigned char *cp, unsigned int length){
/* 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");
char *string=NULL;
string = hexdump((u8*)cp, length);
if(string){
log_write(LOG_PLAIN, "%s", string);
free(string);
}
return;
}
/* 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_STRERROR
char *strerror(int errnum) {

View File

@@ -173,8 +173,7 @@ template<class T> T box(T bmin, T bmax, T bnum) {
int wildtest(char *wild, char *test);
void hdump(unsigned char *packet, unsigned int len);
void lamont_hdump(char *cp, unsigned int length);
void nmap_hexdump(unsigned char *cp, unsigned int length);
/* 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