Jump to content

NMap UDF (Beta Version)


PionnerCast
 Share

Recommended Posts

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
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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.

Link to comment
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
 Share

×
×
  • Create New...