From a9140ddb920bdab790134a3a97a5d85c27bda614 Mon Sep 17 00:00:00 2001 From: dmiller Date: Tue, 14 Aug 2012 21:07:50 +0000 Subject: [PATCH] Debugging changes to jdwp-* scripts, better error output --- scripts/jdwp-exec.nse | 22 +++++++++++++++++----- scripts/jdwp-info.nse | 15 ++++++++++++--- scripts/jdwp-inject.nse | 20 ++++++++++++++------ 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/scripts/jdwp-exec.nse b/scripts/jdwp-exec.nse index 31c395224..36e3ec0ee 100644 --- a/scripts/jdwp-exec.nse +++ b/scripts/jdwp-exec.nse @@ -51,6 +51,7 @@ action = function(host, port) local status,socket = jdwp.connect(host,port) -- initialize the connection if not status then stdnse.print_debug("error, %s",socket) + return nil end -- read .class file @@ -60,28 +61,39 @@ action = function(host, port) -- inject the class local injectedClass status,injectedClass = jdwp.injectClass(socket,class_bytes) + if not status then + stdnse.print_debug(1, "%s: Failed to inject class", SCRIPT_NAME) + return stdnse.format_output(false, "Failed to inject class") + end -- find injected class method local runMethodID = jdwp.findMethod(socket,injectedClass.id,"run",false) if runMethodID == nil then - stdnse.print_debug("Couldn't find run method.") - return false + stdnse.print_debug(1, "%s: Couldn't find run method", SCRIPT_NAME) + return stdnse.format_output(false, "Couldn't find run method.") end -- set run() method argument local cmd = stdnse.get_script_args(SCRIPT_NAME .. '.cmd') if cmd == nil then - stdnse.print_debug("This script requires a cmd argument to be specified.") - return false + return stdnse.format_output(false, "This script requires a cmd argument to be specified.") end local cmdID status,cmdID = jdwp.createString(socket,0,cmd) + if not status then + stdnse.print_debug(1, "%s: Couldn't create string", SCRIPT_NAME) + return stdnse.format_output(false, cmdID) + end local runArgs = bin.pack(">CL",0x4c,cmdID) -- 0x4c is object type tag -- invoke run method local result status, result = jdwp.invokeObjectMethod(socket,0,injectedClass.instance,injectedClass.thread,injectedClass.id,runMethodID,1,runArgs) + if not status then + stdnse.print_debug(1, "%s: Couldn't invoke run method", SCRIPT_NAME) + return stdnse.format_output(false, result) + end -- get the result string local _,_,stringID = bin.unpack(">CL",result) status,result = jdwp.readString(socket,0,stringID) - return stdnse.format_output(true,result) + return stdnse.format_output(status,result) end diff --git a/scripts/jdwp-info.nse b/scripts/jdwp-info.nse index aac2187b1..7127ee310 100644 --- a/scripts/jdwp-info.nse +++ b/scripts/jdwp-info.nse @@ -59,6 +59,7 @@ action = function(host, port) local status,socket = jdwp.connect(host,port) -- initialize the connection if not status then stdnse.print_debug("error, %s",socket) + return nil end -- read .class file @@ -68,21 +69,29 @@ action = function(host, port) -- inject the class local injectedClass status,injectedClass = jdwp.injectClass(socket,class_bytes) + if not status then + stdnse.print_debug(1, "%s: Failed to inject class", SCRIPT_NAME) + return stdnse.format_output(false, "Failed to inject class") + end -- find injected class method local runMethodID = jdwp.findMethod(socket,injectedClass.id,"run",false) if runMethodID == nil then - stdnse.print_debug("Couldn't find run method.") - return false + stdnse.print_debug(1, "%s: Couldn't find run method", SCRIPT_NAME) + return stdnse.format_output(false, "Couldn't find run method.") end -- invoke run method local result status, result = jdwp.invokeObjectMethod(socket,0,injectedClass.instance,injectedClass.thread,injectedClass.id,runMethodID,0,nil) + if not status then + stdnse.print_debug(1, "%s: Couldn't invoke run method", SCRIPT_NAME) + return stdnse.format_output(false, result) + end -- get the result string local _,_,stringID = bin.unpack(">CL",result) status,result = jdwp.readString(socket,0,stringID) -- parse results - return stdnse.format_output(true,result) + return stdnse.format_output(status,result) end diff --git a/scripts/jdwp-inject.nse b/scripts/jdwp-inject.nse index 393527ef9..e67ae91e7 100644 --- a/scripts/jdwp-inject.nse +++ b/scripts/jdwp-inject.nse @@ -46,13 +46,13 @@ action = function(host, port) local status,socket = jdwp.connect(host,port) -- initialize the connection if not status then stdnse.print_debug("error, %s",socket) + return nil end -- read .class file local filename = stdnse.get_script_args(SCRIPT_NAME .. '.filename') if filename == nil then - stdnse.print_debug("This script requires a .class file to inject.") - return false + return stdnse.format_output(false, "This script requires a .class file to inject.") end local file = io.open(nmap.fetchfile(filename), "rb") local class_bytes = file:read("*all") @@ -60,21 +60,29 @@ action = function(host, port) -- inject the class local injectedClass status,injectedClass = jdwp.injectClass(socket,class_bytes) + if not status then + stdnse.print_debug(1, "%s: Failed to inject class", SCRIPT_NAME) + return stdnse.format_output(false, "Failed to inject class") + end -- find injected class method local runMethodID = jdwp.findMethod(socket,injectedClass.id,"run",false) - + if runMethodID == nil then - stdnse.print_debug("Couldn't find run method.") - return false + stdnse.print_debug(1, "%s: Couldn't find run method", SCRIPT_NAME) + return stdnse.format_output(false, "Couldn't find run method.") end -- invoke run method local result status, result = jdwp.invokeObjectMethod(socket,0,injectedClass.instance,injectedClass.thread,injectedClass.id,runMethodID,0,nil) + if not status then + stdnse.print_debug(1, "%s: Couldn't invoke run method", SCRIPT_NAME) + return stdnse.format_output(false, result) + end -- get the result string local _,_,stringID = bin.unpack(">CL",result) status,result = jdwp.readString(socket,0,stringID) -- parse results - return stdnse.format_output(true,result) + return stdnse.format_output(status,result) end