viewpoint-particle

Author Topic: CLR: Port Scan  (Read 102 times)

stanl

  • Pundit
  • *****
  • Posts: 875
CLR: Port Scan
« on: January 12, 2019, 03:45:01 am »
Haven't played around with WB's CLR in a while. Below is a simple script to connect to a port. It works but I don't think it tells the truth. The connect method doesn't have a timeout or error but there is an exception that I think can be handled by the System.ComponentModel.Win32Exception


Tony: maybe you can take a few seconds to look and I would appreciate any suggestions. Would like to make this a UDF.


Code: Winbatch

;Winbatch 2018b - Using CLR to check Port
;Stan Littlefield, 1/12/2019
;==========================================================================
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf


nPort=139
nWait=1000  ;milliseconds
computer = ComputerNameGet( 0 )
_True = ObjectType( 'BOOL', -1 )
_False = ObjectType( 'BOOL', 0 )
null = ObjectType("NULL","")


ObjectClrOption("useany", "System")


oTCP = ObjectClrNew( 'System.Net.Sockets.TcpClient' )
oCconnect = oTCP.Connect(computer, nPort)
netStream = oTCP.GetStream()


results="Port ":nPort:" is "
If netStream.CanRead
   results=results:"active."
Else
   results=results:"not active."
Endif




oTCP.Close()
oTCP.Dispose()
netStream.Close()


Message("Port Scan",results)


Exit
 

stanl

  • Pundit
  • *****
  • Posts: 875
Re: CLR: Port Scan
« Reply #1 on: January 12, 2019, 07:56:09 am »
I added code for socket exception. Then set up a WB error udf.

Code: Winbatch

;Winbatch 2018b - Using CLR to check Port
;Stan Littlefield, 1/12/2019
;==========================================================================
gosub udfs
IntControl(73,3,0,"xception",0)


If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf


nPort=20  ;48338
nWait=1000  ;milliseconds
computer = ComputerNameGet( 0 )
_True = ObjectType( 'BOOL', -1 )
_False = ObjectType( 'BOOL', 0 )
null = ObjectType("NULL","")


ObjectClrOption("useany", "System")


oTCP = ObjectClrNew( 'System.Net.Sockets.TcpClient' )


oCconnect = oTCP.Connect(computer, nPort)
netStream = oTCP.GetStream()
oEx = ObjectClrNew('System.Net.Sockets.SocketException')
 
results="Port ":nPort:" is "
If netStream.CanRead
   results=results:"active."
Else
   results=results:"Socket Exception":oEx.Message
Endif




oTCP.Close()
oTCP.Dispose()
netStream.Close()


Message("Port Scan",results)

Exit

:udfs
#DefineSubRoutine xception(Err_Array)
   wberroradditionalinfo = wberrorarray[6]
   lasterr = wberrorarray[0]
   handlerline = wberrorarray[1]
   textstring = wberrorarray[5]
   linenumber = wberrorarray[8]
   errstr = "Error: ":lasterr:@LF:textstring:@LF:"Line (":linenumber:")":@LF:wberroradditionalinfo
   Message("Exception Generated",errstr)
   oTCP.Close()
   oTCP.Dispose()
   Exit
#EndSubRoutine

Return