what you don't know can hurt you
Home Files News &[SERVICES_TAB]About Contact Add New

nmapgrep-0.2.c

nmapgrep-0.2.c
Posted May 25, 2004
Authored by blh | Site blh.se

nmapgrep is a small tool customized to grep regular expression patterns from a nmap log file and output the IP addresses that match the pattern.

Changes: Improved matching code.
tags | tool, nmap
systems | unix
SHA-256 | fee7b6368d25712423e3f5c6f72366c8809222691257594176a7019b4973204e

nmapgrep-0.2.c

Change Mirror Download
///////////////////////////////////////////////////////////////////////////////

//

// nmapgrep v0.2 by blh@sr-71.nu 20040522

//

// nmapgrep is a program that searches for patterns in nmap log files and

// outputs the matching ip if the port is flagged as open.

//

// in example, searching for hosts that has port 80 open:

// # nmapgrep ^80/tcp logfile.txt

//

// compile:

// in win32 using visual studio (not tested for 0.2):

// cl nmapgrep.c

//

// in any unix or with cygwin:

// gcc -Wall -O2 -DHAVE_REGEX -o nmapgrep nmapgrep.c

//

// use -DHAVE_REGEX only if you know that you have regex.h

//

// feel free to use the code at your own disposal

//

// changes for version 0.2

// - altered regexp and keywork searching to work for all ports discovered

// for the host instead of matching for each line in the log file.

//

// greetings to everyone attending ph-neutral 0x7d4!

//

///////////////////////////////////////////////////////////////////////////////



#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#ifdef HAVE_REGEX

#include <sys/types.h>

#include <regex.h>

#endif

#include <errno.h>



void

errdo(char *str)

{

char estr[255];



snprintf(estr, 255, "%s, line %d", str, __LINE__);



perror(estr);



return;

}



int

main(int argc, char **argv)

{

FILE *fp = stdin;



char buf[1024];

char hoststr[64];



char *ptr1 = NULL;

char *ptr2 = NULL;



char *ports = NULL;



int i = 1;



int bz = 1024;

int ubz = 0;



#ifdef HAVE_REGEX

int nmatch = 0;

regmatch_t pmatch;

regex_t preg;

#endif



fprintf(stderr, "nmapgrep v0.2 by blh@sr-71.nu 20040522\n");



if(argc < 2)

fprintf(stderr, "syntax: %s pattern [nmaplogfile]\n"

#ifdef HAVE_REGEX

"\t- regular expressions supported\n"

#endif

, argv[0]), exit(1);



if(argc > 2)

{

if((fp = fopen(argv[2], "r")) == NULL)

errdo("fopen"), exit(1);

}



fgets(buf, 1024, fp);



if(strncmp(buf, "# nmap", 6))

fprintf(stderr, "%s: unknown log format.\n", argv[2]), exit(1);



if((ports = (char *) malloc(bz)) == NULL)

errdo("malloc"), exit(1);



#ifdef HAVE_REGEX

if((regcomp(&preg, argv[1], REG_EXTENDED)) != 0)

errdo("expression"), exit(1);

#endif



while((fgets(buf, 1024, fp)) != NULL)

{

if(!strncmp(buf, "Interesting", 11))

{

if(((ptr1 = strchr(buf, '(')) == NULL) ||

((ptr2 = strchr(ptr1, ')')) == NULL))

fprintf(stderr, "error parsing line %d\n", i), exit(1);



ptr1[0] = 0;

ptr2[0] = 0;



ptr1++;



strncpy(hoststr, ptr1, 64);

}

else if(buf[0] == '\n' && ubz > 0)

{

#ifdef HAVE_REGEX

if((regexec(&preg, ports, nmatch, &pmatch, REG_NOTEOL)) != REG_NOMATCH)

#else

if(strstr(ports, argv[1]))

#endif

fprintf(stdout, "%s\n", hoststr);



ubz = 0;



memset(ports, 0, bz);

}

else if(strstr(buf, "open"))

{

if((ubz + strlen(buf)) > bz)

{

bz += 1024;



if((ports = (char *) realloc(ports, bz)) == NULL)

errdo("realloc"), exit(1);

}



strncpy(ports + ubz, buf, bz - ubz);

ubz += strlen(buf);

}

i++;

}



fclose(fp);



exit(0);

}

Login or Register to add favorites

File Archive:

May 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    May 1st
    44 Files
  • 2
    May 2nd
    5 Files
  • 3
    May 3rd
    11 Files
  • 4
    May 4th
    0 Files
  • 5
    May 5th
    0 Files
  • 6
    May 6th
    28 Files
  • 7
    May 7th
    3 Files
  • 8
    May 8th
    4 Files
  • 9
    May 9th
    54 Files
  • 10
    May 10th
    12 Files
  • 11
    May 11th
    0 Files
  • 12
    May 12th
    0 Files
  • 13
    May 13th
    17 Files
  • 14
    May 14th
    11 Files
  • 15
    May 15th
    17 Files
  • 16
    May 16th
    13 Files
  • 17
    May 17th
    22 Files
  • 18
    May 18th
    0 Files
  • 19
    May 19th
    0 Files
  • 20
    May 20th
    0 Files
  • 21
    May 21st
    0 Files
  • 22
    May 22nd
    0 Files
  • 23
    May 23rd
    0 Files
  • 24
    May 24th
    0 Files
  • 25
    May 25th
    0 Files
  • 26
    May 26th
    0 Files
  • 27
    May 27th
    0 Files
  • 28
    May 28th
    0 Files
  • 29
    May 29th
    0 Files
  • 30
    May 30th
    0 Files
  • 31
    May 31st
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2022 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close