Jump to content
PionnerCast

NMap UDF (Beta Version)

Recommended Posts

PionnerCast

later , I will add more functions , please note that the main requirement is to have Nmap already installed in your machine

; ******Nmap UDF! v1.0.1*******
;Added and created by Pionner
;Chilean-Brazilian Developer
;Notice that you've nmap already installed in your machine
;In this case I dont add preventers or even error handlers, such as network connection fail or detect if nmap is already installed.

#include-once
#include <Array.au3>
#include <StringConstants.au3>
;#include

Global Const $NMAP_RAW_OUT = 0
Global Const $NMAP_ARRAY_OUT = 1
Global Const $NMAP_FILE_TXT = 2
Global Const $NMAP_FILE_XML = 3

; #FUNCTION# ====================================================================================================================
; Name ..........: _Nmap_SingleScan
; Description ...: Performs an scan to retrieve open ports using @comspec and child process
; Syntax ........: _Nmap_SingleScan($ip_address, $output = 0 , $dir = @ScriptDir)
; Parameters ....: $ip_address - A valid IpV4 address as string.
; $output - [optional] Select how the result will be displayed. it can be...
; $NMAP_RAW_OUT [0] - raw results with all headers into a string.(default)
; $NMAP_ARRAY_OUT[1] - a simple 2D array indicating port/protocol, status and Service
; $NMAP_FILE_TXT[2] - Save the Results to .txt file into a custom folder (default @ScriptDir)
; $NMAP_FILE_XML[3] - Save the Results to .xml file into a custom folder (default @ScriptDir)
; $dir - [optional] Set The folder to save your output file
; Return values .: success: return the results as specified above
; Author ........: Pionner
; Modified ......:
; Remarks .......: take in count that It will take several seconds to accomplish without notifications
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _Nmap_SingleScan($ip_address, $output = 0 , $dir = @ScriptDir)
    Local $Raw, $ix, $pSS
    Dim $NwArOut[3][3]
    Select
        Case $Output = 2
            $fileOuput = StringReplace($ip_address, ".", "_") & ".txt"
        Case $Output = 3
            $fileOutput = StringReplace($ip_address, ".", "_") & ".xml --no-stylesheet"
        Case Else
            $fileOutput = ""
    EndSelect
    $ports = Run(@ComSpec & " /c nmap " & $ip_address & " " & $fileOutput , $dir , @SW_HIDE, $STDOUT_CHILD)
    ProcessWaitClose($ports)
    $ArOutcome = StdoutRead($ports)
    Switch $output
        Case 0
            Return $ArOutcome
        Case 1
            local $iw=0
            $Raw = StringSplit($ArOutcome,@CRLF,$STR_ENTIRESPLIT)
            For $ix = 7 to $Raw[0]-3
                $pSS = StringSplit(StringRegExpReplace($Raw[$ix],"\ +","|"), "|")
                $NwArOut[$iw][0]= $pSS[1]
                $NwArOut[$iw][1]= $pSS[2]
                $NwArOut[$iw][2]= $pSS[3]
                ReDim $NwArOut[$iw+2][3]
                $iw = $iw + 1
            Next
            Return $NwArOut
        Case Else
            Return True
    EndSwitch
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _Nmap_MultiScanByArray
; Description ...: Scan multiple ips at once retrieve an Array pack with results
; Syntax ........: _Nmap_MultiScanByArray($ip_array, $pipe)
; Parameters ....: $ip_pack - An Array/String.
; $pipe - The separator string (dafault is coma)
; $output - [optional] Select how the result will be displayed. it can be...
; $NMAP_RAW_OUT [0] - raw results with all headers into a string.(default)
; $NMAP_ARRAY_OUT[1] - complex 2D array 1st Col = Ip's 2nd to nth Col... port/protocol
; $NMAP_FILE_TXT[2] - Save the Results to .txt file into a custom folder (default @ScriptDir)
; $NMAP_FILE_XML[3] - Save the Results to .xml file into a custom folder (default @ScriptDir)
; $dir - the path to save the results
; Return values : a XML file with speciific Format into your @scriptdir directory
; Author ........: Your Name
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Nmap_MultiScanByArray($ip_pack, $pipe="|", $dir = @ScriptDir)
    Local $ipCalc = StringReplace($ip_pack,$pipe," ")
    $ports = Run(@ComSpec & " /c nmap " & $ipCalc , $dir , @SW_HIDE, $STDOUT_CHILD)
    ProcessWaitClose($ports)
    $ArOutcome = StdoutRead($ports)
    Select
        Case ""
            Return $ArOutcome
        Case 1
            $ver = StringSplit($ArOutcome,"" & @CRLF,$STR_ENTIRESPLIT)
            $lUp = _ArrayFindAll($ver,"report",0,0,0,1)
            Dim $OutArrMult[1][2]
            Dim $ips[1]
            For $ix=0 to UBound($lUp)-1
                $ips[$ix] = StringReplace($ver[$lUp[$ix]],"Nmap scan report for ","")
                Local $in = ($lUp[$ix]+4)
                Local $iCol = 1
                $OutArrMult[$ix][0] = $ips[$ix]
                ReDim $ips[UBound($ips)+1]
                While $ver[$in + $iCol] <> ""
                    $iRow = StringSplit(StringRegExpReplace($ver[$in + $iCol],"\ +","|"),"|")
                    $OutArrMult[$ix][$iCol] = $iRow[1]
                    ReDim $OutArrMult[$ix+2][UBound($OutArrMult,2)+1]
                    $iCol = $iCol + 1
                WEnd
            Next
            Return $OutArrMult
    EndSelect
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _Nmap_MultiScanByList
; Description ...: Use a .txt with a list of lots of ipv4 addresses and make it hapen!
; Syntax ........: _Nmap_MultiScanByList($file)
; Parameters ....: $file - the Full path of file that contains the ipv4 list.
; $dir - the destination fiolder
; Return values .:
; Author ........: Pionner
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Nmap_MultiScanByList($file, $dir)
    $ports = Run(@ComSpec & " /c nmap -iL " & $file , $dir , @SW_HIDE, $STDOUT_CHILD)
    ProcessWaitClose($ports)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _Nmap_ScanByRange
; Description ...:
; Syntax ........: _Nmap_ScanByRange($ip, $range)
; Parameters ....: $ip - An integer value.
; $range - An unknown value.
; Return values .: None
; Author ........: Your Name
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Nmap_ScanByRange($ip, $range)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _Nmap_ScanEntireSubnet
; Description ...:
; Syntax ........: _Nmap_ScanEntireSubnet($ip_loc)
; Parameters ....:
; Return values .: None
; Author ........: Your Name
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Nmap_ScanEntireSubnet($ip_loc)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _Nmap_ScanAgressiveMode
; Description ...: Make a Agressive Scan to a Ip address (no stealth mode)
; Syntax ........: _Nmap_ScanAgressiveMode($ip_address)
; Parameters ....: $ip_address - A Valid Ipv4 address.
; Return values .: Open ports for given IP
; Author ........: Pionner
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Nmap_ScanAgressiveMode($ip_address)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _Nmap_ScanIpv6Target
; Description ...:
; Syntax ........: _Nmap_ScanIpv6Target($ip_address)
; Parameters ....: $ip_address - A Valid Ipv6 address
; Return values .: None
; Author ........: Your Name
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Nmap_ScanIpv6Target($ip_address)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _Nmap_ARPPing
; Description ...: Ping a host with ARP script
; Syntax ........: _Nmap_ARPPing($ip_address)
; Parameters ....: $ip_address - A valid Ipv4 address.
; Return values .: Open Ports of given
; Author ........: Your Name
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Nmap_ARPPing($ip_address)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _Nmap_ScanFastMode
; Description ...: Scan the given Ipv4 address in Turbo mode with minimal verbosing info
; Syntax ........: _Nmap_ScanFastMode($ip_address)
; Parameters ....: $ip_address - An integer value.
; Return values .: None
; Author ........: Your Name
; Modified ......:
; Remarks .......:
; Related .......:
; Example .......: No
; ===============================================================================================================================
Func _Nmap_ScanFastMode($ip_address)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _Nmap_ExecuteNSEscript
; Description ...:
; Syntax ........: _Nmap_ExecuteNSEscript($ip_address, $sname[, $sarguments = ""[, $fileoutput = ""]])
; Parameters ....: $ip_address - An integer value.
; $sname - A string value.
; $sarguments - [optional] A string value. Default is "".
; $fileoutput - [optional] A boolean value. Default is "".
; Return values .: None
; Author ........: Your Name
; Modified ......:
; Remarks .......:list of interesting nse scripts
; nmap -sU -p 161 -T4 -d -v -n -Pn --script snmp-interfaces & ip -- get the remote MAC address
; nmap -sU -script=nbstat.nse -p137 --open 172.192.10.0/23 -oX 172.192.10.0.xml | grep MAC * | awk -F";" {'print $4'}
; Related .......: ; Link ..........:
; Example .......: Yes
;
; ===============================================================================================================================
Func _Nmap_ExecuteNSEscript($ip_address, $sname,$sarguments = "", $fileoutput= "")
EndFunc

 

Edited by Melba23
Added code tags

Share this post


Link to post
Share on other sites
Melba23

PionnerCast,

When you post code please use Code tags - see here how to do it - so that it is contained in a scrolling box with syntax colouring.

M23

Edit: A few spare minutes - so I did it for you.

Edited by Melba23
  • Like 1

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
spudw2k

Welcome @PionnerCast.  Neat idea to create an AutoIt wrapper for NMAP.  I'd recommend reading the Best coding practices article.  It will provide some useful understanding and guidance.  Also, looking at your UDF so far, it looks like there will be a lot of room for optimization.  I recently helped (try) to explain the concept of coding modular-ly.  You might benefit from that as well.  It may give you a general idea on on way you could consolidate your UDF.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • PramodR
      By PramodR
      Hello,
      I have to execute below block of code in elevated previllage, as that particular wmic class will work only with admin previlage.
      i am getting out put when i launch autoit as administrator,  but i will not be able to launch autoit as admin in this particular case but my user have admin rights.
      is there any better way i can run those command as administrator.?
      tried below steps:
      i have tried #RequireAdmin but that creates a user prompt  Tried using Runas command but gives error as wrong username or password , stuck with that step.  
       
    • IgImAx
      By IgImAx
      Hi
       
      UPDATED Notes: ===============================================================================
      UPDATED: My main and second questions answered. Here the answers:
      To add/access WinMenuSelectItem you need to [Thanks to Fernando_Marinho]: Add AutoItX.Dotnet in Manage NuGet Packages Right Click in your Project -> Add -> Reference... -> COM ( Type Libraries )than, check the option AutoItX3 1.0 Type Library  using AutoItX3Lib; AutoItX3 au3 = new AutoItX3(); au3.WinMenuSelectItem("", ...) My full source code in C# exists in 11 posts in below. How to access those overloaded methods in AutoitX3 that are not accessible via above method!? Or how to fix AutoitX3 DLL Registration need in target computers without Autoit pre-installed on them!? Please check my post at 14 posts below!
      =============================================================================== Original Post:
       
       
      I was writing a small app in Autoit to close µTorrent app. It was working. Then I try to import AutoItX into C#, but unfortunately this method
      WinMenuSelectItem Couldn't find by IntelliSense and If I typed completely it still give me this message:
      Please check the image. I Google it and I found this QA at stackoverflow: Autoit error within C# application I saw they use this line:
      au = new AutoItX3Lib.AutoItX3Class(); I figure it how to add 'AutoItX3Lib' to project (by adding 'AutoItX3.dll' to reference) but again! When I use this line:
      var au = new AutoItX3Class(); I got this error message: Interop type 'AutoItX3Class' cannot be embedded. Use the applicable interface instead.
      My system info:
      Visual Studio 2017 Enterprise - v15.5.4
      X64 Windows 10 Enterprise 1607
      Thanks in advanced
      IgImAx

    • Rammanan
      By Rammanan
      Hi all, Below is my script,
      Pls advise me if any wrong on that....File exit in location but the return -1.
      FileOpen($file, 0)
         $line = FileReadLine($file)
         MsgBox(0,'',$line)
      FileClose($file)
      Local $aArray = _FileListToArrayRec($serverpath, "*|*.txt", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_NOPATH)
      If @error Then
              MsgBox($MB_SYSTEMMODAL, "Ooops!", "No files found")
          Else
          ;_ArrayDisplay($aArray, "Sorted tree")
                 Local $hSearch = FileFindFirstFile($line)
               MsgBox(0,'',$hSearch)
       
    • PauloRodrigues
      By PauloRodrigues
      I need to select an item from a SAP combobox, could anyone help me or did something like that? I tried to use some AutoIt functions but without success.
      When i try to identify the combobox with au3Info, this is the return:


      Could anyone help me with this challange?
    • GTAVLover
      By GTAVLover
      I recently tried to use ControlClick method to send a mouse click command to a button in a application, but I noticed that it is not working when I give first parameter as a window handle (HWND). But, when I give class name or window title, it works fine. I used AutoItX3 and VBScript. 
      Following are the commands I tried in VBScript:
      Dim WINAPI: Set WINAPI = WScript.CreateObject("WinAPIWrapper.WINAPI") Dim AutoItX3: Set AutoItX3 = WScript.CreateObject("AutoItX3.Control") Dim WindowHandle: WindowHandle = WINAPI.WinAPI_FindWindow("my window title", "my window class") 'Window handle obtains fine by my wrapper function 'Above window handle is obtained as a hexadecimal string. (I.E. - 0x00000000) I tried it like: WScript.Echo CStr(AutoItX3.ControlClick(WindowHandle, "", "[CLASS:Button; INSTANCE:1]")) // DOES NOT WORK WScript.Echo CStr(AutoItX3.ControlClick(CLng("&h" + Replace(WindowHandle, "0x", "")), "", "[CLASS:Button; INSTANCE:1]")) // DOES NOT WORK WScript.Echo CStr(AutoItX3.ControlClick(000000, "", "[CLASS:Button; INSTANCE:1]")) // DOES NOT WORK WScript.Echo CStr(AutoItX3.ControlClick(0x00000000, "", "[CLASS:Button; INSTANCE:1]")) // DOES NOT WORK Only following commands work: WScript.Echo CStr(AutoItX3.ControlClick("[CLASS:my window class]", "", "[CLASS:Button; INSTANCE:1]")) // WORKS FINE WScript.Echo CStr(AutoItX3.ControlClick("My window title", "", "[CLASS:Button; INSTANCE:1]")) // WORKS FINE  
      If any of your suggestions don't work, I will have to write a wrapper function myself to send mouse click commands. I chosen AutoIt V3 because it is good specially when comes to send keystrokes to applications. Please tell me why this won't work when I give window handle as first parameter.
      Thanks in advance.
×