[Autosec-devel] sonar/plugins network_connect.c,NONE,1.1 network_connect.h,NONE,1.1 Makefile.am,1.16
Brought to you by:
red0x
From: red0x <re...@us...> - 2004-05-19 08:19:26
|
Update of /cvsroot/autosec/sonar/plugins In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10830/plugins Modified Files: Makefile.am Makefile.in Added Files: network_connect.c network_connect.h Log Message: Added a TCP connect() scan plugin. Its about time Index: Makefile.in =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/Makefile.in,v retrieving revision 1.33 retrieving revision 1.34 diff -C2 -d -r1.33 -r1.34 *** Makefile.in 6 May 2004 20:11:09 -0000 1.33 --- Makefile.in 19 May 2004 08:19:15 -0000 1.34 *************** *** 99,103 **** CPPFLAGS = -I$(top_srcdir)/src ! lib_LTLIBRARIES = libsonar_net_icmp.la libsonar_net_ack.la libsonar_inp_random.la libsonar_out_log.la libsonar_out_xml.la libsonar_net_icmp_la_SOURCES = network_icmp.c network_icmp.h libsonar_net_icmp_la_LIBADD = --- 99,103 ---- CPPFLAGS = -I$(top_srcdir)/src ! lib_LTLIBRARIES = libsonar_net_icmp.la libsonar_net_ack.la libsonar_inp_random.la libsonar_out_log.la libsonar_out_xml.la libsonar_net_tcp.la libsonar_net_icmp_la_SOURCES = network_icmp.c network_icmp.h libsonar_net_icmp_la_LIBADD = *************** *** 108,111 **** --- 108,115 ---- libsonar_net_ack_la_LDFLAGS = -module -no-undefined -avoid-version + libsonar_net_tcp_la_SOURCES = network_connect.c network_connect.h + libsonar_net_tcp_la_LIBADD = + libsonar_net_tcp_la_LDFLAGS = -module -no-undefined -avoid-version + libsonar_inp_random_la_SOURCES = inp_rand.c inp_rand.h libsonar_inp_random_la_LIBADD = *************** *** 134,137 **** --- 138,144 ---- am_libsonar_net_icmp_la_OBJECTS = network_icmp.lo libsonar_net_icmp_la_OBJECTS = $(am_libsonar_net_icmp_la_OBJECTS) + libsonar_net_tcp_la_DEPENDENCIES = + am_libsonar_net_tcp_la_OBJECTS = network_connect.lo + libsonar_net_tcp_la_OBJECTS = $(am_libsonar_net_tcp_la_OBJECTS) libsonar_out_log_la_DEPENDENCIES = am_libsonar_out_log_la_OBJECTS = log_file.lo *************** *** 149,152 **** --- 156,160 ---- @AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/inp_rand.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/log_file.Plo ./$(DEPDIR)/log_xml.Plo \ + @AMDEP_TRUE@ ./$(DEPDIR)/network_connect.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/network_icmp.Plo ./$(DEPDIR)/rfc793.Plo COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ *************** *** 160,166 **** DIST_SOURCES = $(libsonar_inp_random_la_SOURCES) \ $(libsonar_net_ack_la_SOURCES) $(libsonar_net_icmp_la_SOURCES) \ ! $(libsonar_out_log_la_SOURCES) $(libsonar_out_xml_la_SOURCES) DIST_COMMON = Makefile.am Makefile.in ! SOURCES = $(libsonar_inp_random_la_SOURCES) $(libsonar_net_ack_la_SOURCES) $(libsonar_net_icmp_la_SOURCES) $(libsonar_out_log_la_SOURCES) $(libsonar_out_xml_la_SOURCES) all: all-am --- 168,175 ---- DIST_SOURCES = $(libsonar_inp_random_la_SOURCES) \ $(libsonar_net_ack_la_SOURCES) $(libsonar_net_icmp_la_SOURCES) \ ! $(libsonar_net_tcp_la_SOURCES) $(libsonar_out_log_la_SOURCES) \ ! $(libsonar_out_xml_la_SOURCES) DIST_COMMON = Makefile.am Makefile.in ! SOURCES = $(libsonar_inp_random_la_SOURCES) $(libsonar_net_ack_la_SOURCES) $(libsonar_net_icmp_la_SOURCES) $(libsonar_net_tcp_la_SOURCES) $(libsonar_out_log_la_SOURCES) $(libsonar_out_xml_la_SOURCES) all: all-am *************** *** 207,210 **** --- 216,221 ---- libsonar_net_icmp.la: $(libsonar_net_icmp_la_OBJECTS) $(libsonar_net_icmp_la_DEPENDENCIES) $(LINK) -rpath $(libdir) $(libsonar_net_icmp_la_LDFLAGS) $(libsonar_net_icmp_la_OBJECTS) $(libsonar_net_icmp_la_LIBADD) $(LIBS) + libsonar_net_tcp.la: $(libsonar_net_tcp_la_OBJECTS) $(libsonar_net_tcp_la_DEPENDENCIES) + $(LINK) -rpath $(libdir) $(libsonar_net_tcp_la_LDFLAGS) $(libsonar_net_tcp_la_OBJECTS) $(libsonar_net_tcp_la_LIBADD) $(LIBS) libsonar_out_log.la: $(libsonar_out_log_la_OBJECTS) $(libsonar_out_log_la_DEPENDENCIES) $(LINK) -rpath $(libdir) $(libsonar_out_log_la_LDFLAGS) $(libsonar_out_log_la_OBJECTS) $(libsonar_out_log_la_LIBADD) $(LIBS) *************** *** 221,224 **** --- 232,236 ---- @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/log_file.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/log_xml.Plo@am__quote@ + @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/network_connect.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/network_icmp.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rfc793.Plo@am__quote@ --- NEW FILE: network_connect.c --- /*************************************************************************** network_connect.c - TCP Connect network plugin ------------------- begin : Tue May 18 2004 copyright : (C) 2004 by red0x email : re...@us... rcsid : $Id: network_connect.c,v 1.1 2004/05/19 08:19:15 red0x Exp $ ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ /** @file network_connect.c * File contains TCP Connect plugin functions */ #include "network_connect.h" #include <assert.h> static const char rcsid[] = "\100$ autosec/sonar: $Id: network_connect.c,v 1.1 2004/05/19 08:19:15 red0x Exp $"; /// my last error static uint16_t my_errno; /// my socket static int my_socket; /// Plugin's plugin_t structure, built by plugin_init static plugin_t plugin_ctx; /// Sonar's sonar_t structure, built by sonar, passed to this plugin static sonar_t *sonar_ctx; static int connect_destroy(void); static int connect_open(void *cmdline, int len); static int connect_close(void); static int connect_sweep(target_t * targets); static void connect_status(void); static void connect_usage(void); static int connect_last_error(void); /** @fn plugin_init(void *in_data) * Plugin Init function, part of the Plugin API * @param in_data Pointer to a sonar_t structure that will be * passed from sonar when your plugin is loaded. * @return A pointer to a filled plugin_t structure or NULL on error. */ void * plugin_init(void *in_data) { assert(in_data != NULL); sonar_ctx = (sonar_t *) in_data; if(sonar_ctx->api < 6) { sonar_ctx->err_ptrs->err_msg("Plugin API must be 6 or higher, please update\n"); return NULL; } my_errno = PLUGIN_LOADED; my_socket = -1; plugin_ctx.version = 6; /* set API version */ plugin_ctx.type = NETWORK_PLUGIN; plugin_ctx.err = (int16_t *) & my_errno; plugin_ctx.ready = false; plugin_ctx.plugin_char = 'T'; plugin_ctx.shortname = "connect scan\0"; plugin_ctx.longname = "TCP connect() scan\0"; plugin_ctx.plugin_init = &plugin_init; plugin_ctx.plugin_destroy = &connect_destroy; plugin_ctx.plugin_open = &connect_open; plugin_ctx.plugin_close = &connect_close; plugin_ctx.plugin_next = NULL; plugin_ctx.plugin_sweep = &connect_sweep; plugin_ctx.plugin_output = NULL; plugin_ctx.plugin_results = NULL; plugin_ctx.plugin_usage = &connect_usage; plugin_ctx.plugin_status = &connect_status; plugin_ctx.plugin_last_error = &connect_last_error; return &plugin_ctx; } /** @fn connect_destroy(void) * Plugin Unload function * @return PLUGIN_UNLOADED */ static int connect_destroy(void) { if(my_socket > 0) close(my_socket); my_errno = PLUGIN_UNLOADED; return PLUGIN_UNLOADED; } static void get_socket(void) { my_socket = socket(PF_INET, SOCK_STREAM, 0); if(my_socket == -1) { sonar_ctx->err_ptrs->err_ret("could not get a socket\n"); exit(-1); } } /** @fn connect_open(void *cmdline, int len) * Plugin Start function * @param cmdline Arguments passed on the command line * @param len length of arguments passed * @return PLUGIN_OK */ static int connect_open(void *cmdline, int len) { /* XXX: Take some options, like TTL, etc. */ if(cmdline && len > 0) sonar_ctx->err_ptrs->err_msg("Connect scan takes no arguments yet\0"); if(sonar_ctx->opts.iPostDelay == -1) sonar_ctx->opts.iPostDelay = 0; get_socket(); plugin_ctx.ready = true; return PLUGIN_OK; } /** @fn connect_close(void) * Plugin destroy function * Call this before connect_destroy * @return PLUGIN_OK */ static int connect_close(void) { close(my_socket); my_socket = -1; plugin_ctx.ready = false; return PLUGIN_OK; } /** @fn connect_connect(int sock, struct addrinfo *ai, unsigned int port) * Protocol Independent wrapper for connect(2) * @param sock Socket descriptor * @param ai Address Info as returned from getaddrinfo(3) * @param port Port number to attempt connect on * @return see connect(2) */ int connect_connect(int sock, struct addrinfo *ai, unsigned int port) { struct sockaddr_in *sa; struct sockaddr_in6 *sa6; if(sock <=0 || ai == NULL) return -1; switch(ai->ai_family) { case PF_INET: sa = (struct sockaddr_in *) ai->ai_addr; sa->sin_port = ntohs(port); break; case PF_INET6: sa6 = (struct sockaddr_in6 *) ai->ai_addr; sa6->sin6_port = ntohs(port); break; default: return -1; } return connect(sock, ai->ai_addr, ai->ai_addrlen); } /** @fn connect_sweep(target_t *targets) * Plugin Sweep Function * Runs a TCP connect() scan against targets * @param targets Pointer to linked list of targets * @return PLUGIN_ERROR on error, PLUGIN_OK on ok */ static int connect_sweep(target_t * targets) { char broadcast = 0, fake = 0; unsigned int counter = 0; unsigned int open = 0, closed = 0, scanned = 0; int retval, i; struct servent *service; if(my_socket == -1) return PLUGIN_EOF; if(!plugin_ctx.ready) { sonar_ctx->err_ptrs->err_msg("plugin not ready\n"); return PLUGIN_ERROR; } while(targets != NULL && counter < sonar_ctx->num_targets) { if(targets->broadcast && !broadcast) { broadcast = 1; if(setsockopt(my_socket, SOL_SOCKET, SO_BROADCAST, (int *) &broadcast, sizeof(int)) < 0) { sonar_ctx->err_ptrs->err_ret("could not set broadcast mode, try being root\n"); sonar_ctx->err_ptrs->err_ret("skipping this target... :(\n"); continue; } } setservent(0); for(i = 0; i < 65536; i++) { /* stupid hax0r tr1ckz */ if(targets->ports[i] == PORT_SCAN) { targets->ports[i] = PORT_SCANNING; service = getservbyport(htons(i), NULL); if(!service) { service = (struct servent *) malloc(sizeof(struct servent)); if(!service) { sonar_ctx->err_ptrs->err_quit("Could not allocate memory\n"); return PLUGIN_ERROR; } service->s_name = strndup("unknown\0", 8); fake++; } /* scan this one port */ scanned++; retval = connect_connect(my_socket, targets->addrinfo, i); if(retval == 0) { targets->ports[i] = PORT_OPENED; open++; if(sonar_ctx->opts.iVerbose > 0) sonar_ctx->message(" %s[%d]: \t Open: %s\n", targets->entry, i, service->s_name); } else { closed++; targets->ports[i] = PORT_CLOSED; if(sonar_ctx->opts.iVerbose > 1) sonar_ctx->message(" %s[%d]: \t closed: %s\n", targets->entry, i, service->s_name); } if(fake) { free(service->s_name); free(service); fake--; } close(my_socket); get_socket(); } counter++; } } sonar_ctx->message("XXX: stats when done\n"); return PLUGIN_OK; } /** @fn connect_status(void) * Plugin Status hook function * Prints out status information right before the sweep begins */ static void connect_status(void) { if(plugin_ctx.ready) { sonar_ctx->output("TCP connect() scan: %d targets\n", sonar_ctx->num_targets); } } /** @fn connect_usage(void) * Plugin Usage hook function * Prints out plugin usage information. */ static void connect_usage(void) { assert(sonar_ctx->output != NULL); sonar_ctx->output("\n TCP connect() Scan plugin\n"); sonar_ctx-> output (" Usage: -sT\n"); sonar_ctx-> output (" Example: sonar -sT target.example.com\n"); /* Do not exit, so other plugins can ouput their usage too! exit(-1); */ } /** @fn connect_last_error() * Return last error code * @return Last error code */ static int connect_last_error(void) { return my_errno; } --- NEW FILE: network_connect.h --- /*************************************************************************** network_connect.h - TCP Connect network plugin ------------------- begin : Tue May 18 2004 copyright : (C) 2004 by red0x email : re...@us... rcsid : $Id: network_connect.h,v 1.1 2004/05/19 08:19:15 red0x Exp $ ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ /** @file network_connect.h * File contains TCP Connect plugin functions */ #ifndef _NETWORK_CONN_H_ #define _NETWORK_CONN_H_ #include "plugin.h" #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> void *plugin_init(void *in_data); #endif /* !_NETWORK_CONN_H_ */ Index: Makefile.am =================================================================== RCS file: /cvsroot/autosec/sonar/plugins/Makefile.am,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** Makefile.am 6 May 2004 20:11:09 -0000 1.16 --- Makefile.am 19 May 2004 08:19:15 -0000 1.17 *************** *** 5,9 **** DEBUG_CFLAGS= @DEBUG_CFLAGS@ -DHAVE_CONFIG_H -DPLUGINDIR="\\\"$(libdir)\\\"" ! lib_LTLIBRARIES = libsonar_net_icmp.la libsonar_net_ack.la libsonar_inp_random.la libsonar_out_log.la libsonar_out_xml.la libsonar_net_icmp_la_SOURCES = network_icmp.c network_icmp.h libsonar_net_icmp_la_LIBADD = --- 5,9 ---- DEBUG_CFLAGS= @DEBUG_CFLAGS@ -DHAVE_CONFIG_H -DPLUGINDIR="\\\"$(libdir)\\\"" ! lib_LTLIBRARIES = libsonar_net_icmp.la libsonar_net_ack.la libsonar_inp_random.la libsonar_out_log.la libsonar_out_xml.la libsonar_net_tcp.la libsonar_net_icmp_la_SOURCES = network_icmp.c network_icmp.h libsonar_net_icmp_la_LIBADD = *************** *** 14,17 **** --- 14,21 ---- libsonar_net_ack_la_LDFLAGS = -module -no-undefined -avoid-version + libsonar_net_tcp_la_SOURCES = network_connect.c network_connect.h + libsonar_net_tcp_la_LIBADD = + libsonar_net_tcp_la_LDFLAGS = -module -no-undefined -avoid-version + libsonar_inp_random_la_SOURCES = inp_rand.c inp_rand.h libsonar_inp_random_la_LIBADD = |