From 3e3e87f462448ec915fd078c73c00dc38e69ec67 Mon Sep 17 00:00:00 2001 From: d33tah Date: Thu, 13 Jun 2013 15:04:23 +0000 Subject: [PATCH] * create d33tah directory at nmap-exp * create a branch ncat-lua in nmap-exp/d33tah based on current nmap trunk * merge in patches from github into ncat-lua branch --- ncat/Makefile.in | 34 ++++++++++++--- ncat/configure.ac | 103 ++++++++++++++++++++++++++++++++++++++++++++ ncat/hello.lua | 8 ++++ ncat/ncat_connect.c | 14 ++++-- ncat/ncat_core.c | 8 +++- ncat/ncat_core.h | 7 ++- ncat/ncat_main.c | 42 ++++++++++++++++++ 7 files changed, 203 insertions(+), 13 deletions(-) create mode 100644 ncat/hello.lua diff --git a/ncat/Makefile.in b/ncat/Makefile.in index a9707aed3..19d7b43de 100644 --- a/ncat/Makefile.in +++ b/ncat/Makefile.in @@ -59,6 +59,11 @@ STRIP = @STRIP@ OPENSSL_LIBS = @OPENSSL_LIBS@ HAVE_OPENSSL = @HAVE_OPENSSL@ PCAP_LIBS = @PCAP_LIBS@ +HAVE_LUA = @LIBLUA_LIBS@ +LUA_LIBS = @LIBLUA_LIBS@ +LIBLUADIR = @LIBLUADIR@ +LUA_CFLAGS = @LUA_CFLAGS@ + CPPFLAGS += $(DEFS) $(INCLS) @@ -78,6 +83,13 @@ OBJS += http_digest.o DATAFILES = certs/ca-bundle.crt endif +ifneq ($(HAVE_LUA),) +SRCS += ncat_lua.c +OBJS += ncat_lua.o +LUA_LIBS += -lm #a quick hack +LUA_CFLAGS += -DHAVE_LUA=1 +endif + TARGET = ncat mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs CONFIG_HEADER = config.h @@ -88,13 +100,17 @@ ifneq ($(HAVE_OPENSSL),) TEST_PROGS += test/test-wildcard endif -all: $(TARGET) $(TEST_PROGS) +all: @LUA_BUILD@ $(TARGET) $(TEST_PROGS) -$(TARGET): $(OBJS) $(NSOCKLIB) - $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(OBJS) $(NSOCKLIB) $(NBASELIB) $(OPENSSL_LIBS) $(PCAP_LIBS) $(LIBS) +lua_build: $(LIBLUADIR)/Makefile + @echo Compiling liblua; cd $(LIBLUADIR) && $(MAKE) liblua.a CC="$(CC)" MYCFLAGS="$(CFLAGS) $(LUA_CFLAGS)" + + +$(TARGET): @LUA_DEPENDS@ $(OBJS) $(NSOCKLIB) + $(CC) -o $@ $(CFLAGS) $(lua_cflags) $(LDFLAGS) $(OBJS) $(NSOCKLIB) $(NBASELIB) $(OPENSSL_LIBS) $(PCAP_LIBS) $(LUA_LIBS) $(LIBS) %.o: %.c - $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + $(CC) $(CPPFLAGS) $(LUA_CFLAGS) $(CFLAGS) -c $< -o $@ Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status cd $(top_builddir) \ @@ -158,9 +174,15 @@ uninstall: ncat_clean: $(RM) -f *.o test/*.o $(TARGET) $(TEST_PROGS) -clean: ncat_clean +clean: ncat_clean @LUA_CLEAN@ -distclean: clean +lua_clean: + -cd $(LIBLUADIR) && $(MAKE) clean + +lua_dist_clean: + -cd $(LIBLUADIR) && $(MAKE) clean + +distclean: clean @LUA_DIST_CLEAN@ -rm -f Makefile makefile.dep $(CONFIG_CLEAN_FILES) TESTS = ./test-addrset.sh ./test-cmdline-split ./test-uri diff --git a/ncat/configure.ac b/ncat/configure.ac index 8ee523c2c..5ea713f6f 100644 --- a/ncat/configure.ac +++ b/ncat/configure.ac @@ -203,10 +203,113 @@ AC_CHECK_LIB(odm, odm_initialize) AC_CHECK_LIB(odm, odm_initialize) AC_CHECK_LIB(cfg, _system_configuration) + + +LIBLUADIR=../liblua + +have_lua=no +requested_included_lua=no +no_lua=no + +# First we test whether they specified liblua explicitly +AC_ARG_WITH(liblua, +AC_HELP_STRING([--with-liblua=DIR], [Use an existing (compiled) lua lib from DIR/include and DIR/lib.]) +AC_HELP_STRING([--with-liblua=included], [Use the liblua version included with Nmap]) +AC_HELP_STRING([--without-liblua], [Compile without lua (this will exclude all of NSE from compilation)]), +[ case "$with_liblua" in + yes) + ;; + included) + CPPFLAGS="-I\$(top_srcdir)/$LIBLUADIR $CPPFLAGS" + LIBLUA_LIBS="\$(top_srcdir)/$LIBLUADIR/liblua.a" + LUA_DEPENDS="\$(top_srcdir)/$LIBLUADIR/liblua.a" + LUA_BUILD="lua_build" + LUA_CLEAN="lua_clean" + LUA_DIST_CLEAN="lua_dist_clean" + have_lua="yes" + + ;; + no) + no_lua="yes" + ;; + *) + CPPFLAGS="-I$with_liblua/include $CPPFLAGS" + LDFLAGS="-L$with_liblua/lib $LDFLAGS" + ;; + esac] +) + +LUA_CFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" +AC_SUBST(LUA_CFLAGS) + +# OpenSSL and NSE C modules can require dlopen +AC_SEARCH_LIBS(dlopen, dl) + +# They don't want lua +if test "$no_lua" = "yes"; then + CPPFLAGS="-DNOLUA $CPPFLAGS" + LIBLUA_LIBS="" + LUA_DEPENDS="" + LUA_BUILD="" + LUA_CLEAN="" + LUA_DIST_CLEAN="" + INSTALLNSE="" + +else + + # If they didn't specify it, we try to find it + if test $have_lua != yes; then + AC_CHECK_HEADERS([lua.h lua/lua.h lua5.2/lua.h], + AC_CHECK_LIB(lua, lua_yieldk, [have_lua=yes; LIBLUA_LIBS="-llua"; CPPFLAGS="-I/usr/include/lua $CPPFLAGS"; break],, [-lm]) + AC_CHECK_LIB(lua52, lua_yieldk, [have_lua=yes; LIBLUA_LIBS="-llua52"; CPPFLAGS="-I/usr/include/lua52 $CPPFLAGS"; break],, [-lm]) + AC_CHECK_LIB(lua5.2, lua_yieldk, [have_lua=yes; LIBLUA_LIBS="-llua5.2"; CPPFLAGS="-I/usr/include/lua5.2 $CPPFLAGS"; break],, [-lm]) + ) + + AC_LANG_PUSH(C++) + AC_MSG_CHECKING([for lua version >= 502]) + AC_RUN_IFELSE([ AC_LANG_PROGRAM( + [[#include ]], + [[if(LUA_VERSION_NUM < 502) return 1;]])], + have_lua=yes, have_lua=no, AC_MSG_RESULT(cross-compiling -- assuming yes); have_lua=yes) + AC_LANG_POP(C++) + + LUA_DEPENDS="" + LUA_BUILD="" + LUA_CLEAN="" + LUA_DIST_CLEAN="" + fi + + # if we didn't find we use our own + if test $have_lua != yes; then + AC_MSG_RESULT(no) + CPPFLAGS="-I\$(top_srcdir)/$LIBLUADIR $CPPFLAGS" + LIBLUA_LIBS="\$(top_srcdir)/$LIBLUADIR/liblua.a" + LUA_DEPENDS="\$(top_srcdir)/$LIBLUADIR/liblua.a" + LUA_BUILD="lua_build" + LUA_CLEAN="lua_clean" + LUA_DIST_CLEAN="lua_dist_clean" + AC_DEFINE(LUA_INCLUDED) + else + AC_MSG_RESULT(yes) + fi + + INSTALLNSE="install-nse" +fi + +AC_SUBST(LIBLUA_LIBS) +AC_SUBST(LIBLUADIR) +AC_SUBST(LUA_DEPENDS) +AC_SUBST(LUA_BUILD) +AC_SUBST(LUA_CLEAN) +AC_SUBST(LUA_DIST_CLEAN) + + + AC_CONFIG_FILES(Makefile) AC_OUTPUT # NCAT ASCII ART if test -f docs/ncat-ascii-art.txt; then cat docs/ncat-ascii-art.txt fi + echo "Configuration complete." diff --git a/ncat/hello.lua b/ncat/hello.lua new file mode 100644 index 000000000..f399cdb82 --- /dev/null +++ b/ncat/hello.lua @@ -0,0 +1,8 @@ +print("I'm in hello.lua.") + +function on_connect() +sock_write("Hello") +end + +--connect("localhost", 2233) +--connect("localhost", 2234) diff --git a/ncat/ncat_connect.c b/ncat/ncat_connect.c index d838aac3e..67b7f670d 100644 --- a/ncat/ncat_connect.c +++ b/ncat/ncat_connect.c @@ -92,6 +92,7 @@ #include "base64.h" #include "nsock.h" #include "ncat.h" +#include "ncat_lua.h" #include "util.h" #include "sys_wrap.h" @@ -137,7 +138,7 @@ static void connect_handler(nsock_pool nsp, nsock_event evt, void *data); static void post_connect(nsock_pool nsp, nsock_iod iod); static void read_stdin_handler(nsock_pool nsp, nsock_event evt, void *data); static void read_socket_handler(nsock_pool nsp, nsock_event evt, void *data); -static void write_socket_handler(nsock_pool nsp, nsock_event evt, void *data); +void write_socket_handler(nsock_pool nsp, nsock_event evt, void *data); static void idle_timer_handler(nsock_pool nsp, nsock_event evt, void *data); static void refresh_idle_timer(nsock_pool nsp); @@ -277,7 +278,7 @@ static const char *sock_to_url(const union sockaddr_u *su) if (su->storage.ss_family == AF_INET) Snprintf(buf, sizeof(buf), "%s:%hu", host_str, port); else if (su->storage.ss_family == AF_INET6) - Snprintf(buf, sizeof(buf), "[%s]:%hu", host_str, port); + Snprintf(buf, sizeof(buf), "[%s]:%hu]", host_str, port); else bye("Unknown address family in sock_to_url_host."); @@ -773,6 +774,13 @@ static void post_connect(nsock_pool nsp, nsock_iod iod) cs.idle_timer_event_id = nsock_timer_create(nsp, idle_timer_handler, o.idletimeout, NULL); } + +#ifdef HAVE_LUA + if (o.lua) + { + lua_run_onconnect(nsp, iod); + } +#endif } static void read_stdin_handler(nsock_pool nsp, nsock_event evt, void *data) @@ -866,7 +874,7 @@ static void read_socket_handler(nsock_pool nsp, nsock_event evt, void *data) refresh_idle_timer(nsp); } -static void write_socket_handler(nsock_pool nsp, nsock_event evt, void *data) +void write_socket_handler(nsock_pool nsp, nsock_event evt, void *data) { enum nse_status status = nse_status(evt); enum nse_type type = nse_type(evt); diff --git a/ncat/ncat_core.c b/ncat/ncat_core.c index 6813bc728..456a5f6c2 100644 --- a/ncat/ncat_core.c +++ b/ncat/ncat_core.c @@ -118,8 +118,8 @@ size_t srcaddrlen; union sockaddr_u targetss; size_t targetsslen; -union sockaddr_u httpconnect, socksconnect; -size_t httpconnectlen, socksconnectlen; +union sockaddr_u httpconnect; +union sockaddr_u socksconnect; /* Global options structure. */ struct options o; @@ -177,6 +177,10 @@ void options_init(void) o.sslverify = 0; o.ssltrustfile = NULL; #endif +//#ifdef HAVE_LUA + o.lua = 0; + o.luafile = NULL; +//#endif } /* Internal helper for resolve and resolve_numeric. addl_flags is ored into diff --git a/ncat/ncat_core.h b/ncat/ncat_core.h index caee21abb..c6049f505 100644 --- a/ncat/ncat_core.h +++ b/ncat/ncat_core.h @@ -107,8 +107,8 @@ extern size_t srcaddrlen; extern union sockaddr_u targetss; extern size_t targetsslen; -extern union sockaddr_u httpconnect, socksconnect; -extern size_t httpconnectlen, socksconnectlen; +extern union sockaddr_u httpconnect; +extern union sockaddr_u socksconnect; struct options { unsigned short portno; @@ -162,6 +162,9 @@ struct options { char *sslkey; int sslverify; char *ssltrustfile; + + int lua; + char *luafile; }; extern struct options o; diff --git a/ncat/ncat_main.c b/ncat/ncat_main.c index 084f17cea..40be83d07 100644 --- a/ncat/ncat_main.c +++ b/ncat/ncat_main.c @@ -114,6 +114,10 @@ #include #endif +#ifdef HAVE_LUA +#include "ncat_lua.h" +#endif + static int ncat_connect_mode(void); static int ncat_listen_mode(void); @@ -276,6 +280,9 @@ int main(int argc, char *argv[]) {"ssl-verify", no_argument, NULL, 0}, {"ssl-trustfile", required_argument, NULL, 0}, #endif +#ifdef HAVE_LUA + {"lua-file", required_argument, NULL, 0}, +#endif {0, 0, 0, 0} }; @@ -290,8 +297,13 @@ int main(int argc, char *argv[]) while (1) { /* handle command line arguments */ int option_index; +#ifdef HAVE_LUA + int c = getopt_long(argc, argv, "46UCc:e:g:G:i:km:hp:d:lo:x:ts:uvw:n:L", + long_options, &option_index); +#else int c = getopt_long(argc, argv, "46UCc:e:g:G:i:km:hp:d:lo:x:ts:uvw:n", long_options, &option_index); +#endif /* That's the end of the options. */ if (c == -1) @@ -406,6 +418,12 @@ int main(int argc, char *argv[]) case 't': o.telnet = 1; break; + +#ifdef HAVE_LUA + case 'L': + o.lua = 1; + break; +#endif case 0: if (strcmp(long_options[option_index].name, "version") == 0) { print_banner(); @@ -471,6 +489,12 @@ int main(int argc, char *argv[]) o.sslverify = 1; } #endif +#ifdef HAVE_LUA + else if (strcmp(long_options[option_index].name, "lua-file") == 0) { + o.lua = 1; + o.luafile = Strdup(optarg); + } +#endif break; case 'h': printf("%s %s ( %s )\n", NCAT_NAME, NCAT_VERSION, NCAT_URL); @@ -524,6 +548,10 @@ int main(int argc, char *argv[]) " --ssl-verify Verify trust and domain name of certificates\n" " --ssl-trustfile PEM file containing trusted SSL certificates\n" #endif +#ifdef HAVE_LUA +" --lua-file A .lua ncat script\n" +" -L Run Ncat in Lua stdin mode\n" +#endif " --version Display Ncat's version information and exit\n" "\n" "See the ncat(1) manpage for full options, descriptions and usage examples\n" @@ -656,6 +684,9 @@ int main(int argc, char *argv[]) #endif /* Listen defaults to any address and DEFAULT_NCAT_PORT */ if (!o.listen) +#ifdef HAVE_LUA + if (!(o.lua && o.luafile == NULL)) +#endif bye("You must specify a host to connect to."); } else { #if HAVE_SYS_UN_H @@ -679,6 +710,9 @@ int main(int argc, char *argv[]) bye("Could not resolve hostname \"%s\": %s.", o.target, gai_strerror(rc)); optind++; } else { +#ifdef HAVE_LUA + if (!(o.lua && o.luafile == NULL)) +#endif if (!o.listen) bye("You must specify a host to connect to."); } @@ -787,6 +821,14 @@ connection brokering should work."); the console. A no-op on Unix. */ set_lf_mode(); +#ifdef HAVE_LUA + if(o.lua) + lua_setup(); + + if(o.lua && o.luafile == NULL) + return ncat_lua_mode(); +#endif + if (o.listen) return ncat_listen_mode(); else