Author Topic: netScan - development topic...  (Read 4669 times)

undertuga

  • Newbie
  • *
  • Posts: 9
    • Bunk3r IT Industries
netScan - development topic...
« on: July 13, 2007, 07:04:13 PM »
Hi there to all Basic @ Linux geeks!

This is my first soft powered by Gambas, and as promised on another post, here it is so that you guys can check it out! Its very basic and very simple (kinda newbie also), and and want to add more features, and off course, improve the scan routine!

Any feedback / opinion / suggestion / tip is extremely welcome!

Best regards!


--------------------------------------------------------------------------------------------------------------------
DOWNLOADS

- Download Source Package
- Download Executable


SCREENSHOTS





--------------------------------------------------------------------------------------------------------------------
« Last Edit: July 24, 2007, 12:27:59 AM by undertuga »

Linux Basic

netScan - development topic...
« on: July 13, 2007, 07:04:13 PM »

timothy

  • Sr. Member
  • ****
  • Posts: 319
Re: Lan Port Scanner 1.0 alpha
« Reply #1 on: July 14, 2007, 03:11:11 AM »

This is an interesting little project. However if you simply use a WAIT you may miss open ports. This may not be a problem on the localhost, but it could be on a remote host. A better way of scanning ports would be to handle the sockets events. You only need to add the open port to your list in the Ready event. And also close the port in the same event. You could do something like this in Gambas 2:

PUBLIC Connection AS NEW Socket AS "SocketClient"

PUBLIC SUB ButtonScan_Click()
  DIM portNumber AS Integer
  FOR portNumber = 1 TO 3000
    Connection.Connect("localhost", portNumber)
    ' Wait until the socket is closed
    ' or an error is found
    REPEAT
      WAIT
    UNTIL Connection.Status <= Net.Inactive
  NEXT
END

PUBLIC SUB SocketClient_Ready()
  PRINT "Port open " & Connection.Port & " on " & Connection.Host
  CLOSE #Connection
END

Events on a socket are:

' Fired when the host is found
PUBLIC SUB SocketClient_Found()
  PRINT "Host found " & Connection.Host
END

' Fired when the socket has been opened and you can read/write to the socket
PUBLIC SUB SocketClient_Ready()
  PRINT "Port open " & Connection.Port & " on " & Connection.Host
END

' Fired when there is an error with the socket.
' This includes when the socket cannot be opened
PUBLIC SUB SocketClient_Error()
  ' PRINT "Error opening socket: " & Connection.Status
END

' Fired when there is some data from the server to be read
PUBLIC SUB SocketClient_Read()
  DIM buffer AS String
  READ #Connection, buffer, Lof(Connection)
END

' Fired when the socket is closed by the server
PUBLIC SUB SocketClient_Closed()
  PRINT "socket closed"
END
42 - So long and thanks for all the fish.

undertuga

  • Newbie
  • *
  • Posts: 9
    • Bunk3r IT Industries
Re: Lan Port Scanner 1.0 alpha
« Reply #2 on: July 15, 2007, 12:38:54 PM »
uauu... that will help me a lot refining the scan routine!  :D ;D

Thanks a lot timothy!

As soon as i get it done, i will post the new solution!


Best regards!

timothy

  • Sr. Member
  • ****
  • Posts: 319
Re: Lan Port Scanner 1.0 alpha
« Reply #3 on: July 16, 2007, 01:52:16 AM »

A slightly better version of the above program. Put it in your startup module of a command line application. You will also need the gb.net component in your project. (The socket syntax is for Gambas 2.)

PUBLIC Connection AS NEW Socket AS "SocketClient"

PUBLIC SUB Main()
  DIM portNumber AS Integer
  FOR portNumber = 1 TO 3000
    Connection.Connect("localhost", portNumber)
    ' Wait until the socket is closed
    ' or an error is found
    REPEAT
      WAIT 0.01
    UNTIL Connection.Status <= Net.Inactive
    IF Connection.Status = Net.HostNotFound THEN
      PRINT "Host not found"
      ' No point in scanning if we can not find the host
      BREAK
    END IF
  NEXT
END

PUBLIC SUB SocketClient_Ready()
  PRINT "Port open " & Connection.Port & " on " & Connection.Host
  CLOSE #Connection
END
42 - So long and thanks for all the fish.

undertuga

  • Newbie
  • *
  • Posts: 9
    • Bunk3r IT Industries
Re: Lan Port Scanner 1.0 alpha
« Reply #4 on: July 16, 2007, 06:59:49 PM »
very nice timothy...

I'm implementing your solution and soon i will post it here!

Once again, thanks a lot! ;)


Best regards!

undertuga

  • Newbie
  • *
  • Posts: 9
    • Bunk3r IT Industries
Re: Lan Port Scanner 1.0 alpha
« Reply #5 on: July 16, 2007, 09:37:45 PM »
well...

i got some updates about this...

The solution above (the last one) makes the scan routine more effective, because i can make it faster and more accurate with it! The ports can be scanned faster and using the socket event SocketClient_Ready, it makes it very accurate detecting "regular" open ports!  ;)

But the same bottleneck still exist on the program. It's the  only think (on this version) that really doesnt work: scanning remote hosts! ???

On LAN's it's perfect, faster and very accurate, only needs some service detection (...later ;)), but now, this problem needs to be solve, or the scanner wil only be usefull for LAN scanning :-\

Either external IP's or domains cant be successfully scanned.


Heres the actual "scan core":


'defining connection socket
PUBLIC Connection AS NEW Socket AS "SocketClient"

'setting the screen counter
PUBLIC screenCount AS Integer



PUBLIC SUB btn1_Click()

 
    DIM targethost AS String
    DIM minportas AS Integer
    DIM maxportas AS Integer
   
    'linking host variable with inputs
    targethost = target.text

    'linking port variables with inputs
    minportas = minPort.Text
    maxportas = maxPort.text
   
    'defining screen counter
    screenCount = 0
   
    'defining range for the scan routine
    FOR minportas = minportas TO maxportas
       
          'connects to target host on current port
          Connection.Connect(targethost, minportas)
       
          'waits until connection is closed or error is found
          REPEAT
          WAIT 0.01
          UNTIL Connection.Status <= Net.Inactive
         
          'checks if hostname was found
          IF Connection.Status = Net.HostNotFound THEN
          screen.Add("Hostname not found...", 0)
          BREAK
          ENDIF
         
          'displays current port...
          current.text = minportas
         
          'counts open ports
          openCount.text = screenCount
         
      NEXT
END

PUBLIC SUB SocketClient_Ready()
 
  'found open port...
  screen.Add("Port: " & Connection.Port & "   |   Status: Open", screenCount)
 
  'close the connection
  CLOSE #Connection
 
  'increment screen counter
  screenCount = screenCount + 1
 
 

END



Once again, thanks for your help!

Best regards!

----------------------------------------------------------------------------------------------
EDIT: Download Current Source Package (alpha2)
----------------------------------------------------------------------------------------------
« Last Edit: July 17, 2007, 09:21:07 AM by undertuga »

undertuga

  • Newbie
  • *
  • Posts: 9
    • Bunk3r IT Industries
Re: Lan Port Scanner 1.0 alpha
« Reply #6 on: July 22, 2007, 09:06:38 PM »
I've been working around this little source in order to make it possible to scan "external" remote hosts!

I got a solution, very simple and very "messy" also :-\, but...
... it gave me the possibility to finally get it to scan remote hosts!

The solution consists on implementing some kind of "hand made & forced" connection timeout, because i just cant find anything like "connection timeput" on Gambas!

If you dont have, you got to make, and i did, not perfect, but its working (for now...)!

Here's the actual core source:

'defining connection socket
PUBLIC Connection AS NEW Socket AS "SocketClient"

'setting timeout Counter
PUBLIC timeoutCount AS Integer

'setting the screen counter
PUBLIC screenCount AS Integer

'setting the scan status scheme
PUBLIC scanStatus AS Boolean

'setting the countTime
PUBLIC countTime AS Integer

'setting minutos...
PUBLIC minutos AS Integer

'setting horas...
PUBLIC horas AS Integer

PUBLIC segundos AS Integer



PUBLIC SUB Form_Open()


    'setting the buttons...
    btn2.Enabled = FALSE
   
    'hide the about and help forms
    about.hide
    help.hide
 
END 
 
 

PUBLIC SUB btn1_Click()

 
    DIM targethost AS String
    DIM minportas AS Integer
    DIM maxportas AS Integer
    DIM portTotal AS Integer
    DIM progress AS Float
   
   
   
   
    'setting hora, minutos and counTime
    countTime = 1
    horas = 0
    minutos = 0
    segundos = 1
   
   
   
    'linking host variable with inputs
    targethost = target.text



    'linking port variables with inputs
    minportas = minPort.Text
    maxportas = maxPort.text
   
   
   
    'defining screen counter
    screenCount = 0
   
   
   
    'validates the user inputs and sets scan status
    IF (targetHost <> "" AND minportas <= maxportas) THEN
       
        'if all ok, sets the scan status to TRUE
        scanStatus = TRUE
    ELSE
        'displays input error message
        message.Error("Error detected! Please do the following:\n\n- Insert Valid IP/Hostname\n- Insert Valid Port Range", "ok")
   
        'reforces negative scan status...
        scanStatus = FALSE   
    ENDIF
   
   

    'defining percentage for progress bar
    portTotal = maxportas - minportas
    progress = 1 / portTotal
    barra.value = 0
   
    message.Info("Progress: " & progress, "continue")
   
   
   
    'checks scan status before start scan
    IF (scanStatus = TRUE) THEN
   
   
   
    'setting the buttons on scanning state...
    btn1.Enabled = FALSE
    btn2.Enabled = TRUE
   
   
   
    'starts the timer
    timing.Enabled = TRUE
   
   
   
    'clear the screen
    screen.clear
   
   
   
    'defining range for the scan routine
    FOR minportas = minportas TO maxportas
       
       
       
        'increments the progress bar
        barra.value = barra.value + progress
       
            timeoutCount = 0
            timeout.Enabled = TRUE
       
          'connects to target host on current port
          Connection.Connect(targethost, minportas)
       
       
       
          'waits until connection is closed or error is found
          REPEAT
          WAIT 0.01
          UNTIL Connection.Status <= net.Inactive OR timeoutCount = 2 ' means 0.4 seconds
         
          timeout.Enabled = FALSE
          Connection.Close
         
         
          'checks if hostname was found
          IF Connection.Status = Net.HostNotFound THEN
          screen.Add("Hostname not found...", 0)
          BREAK
          ENDIF
         
         
         
          'displays current port...
          current.text = minportas
         
          'counts open ports
          openCount.text = screenCount
         
         
          'verifying scan status before next port scanning
          IF (scanStatus = FALSE) THEN
         
              'nuke minportas in order to stop the scan routine
              minportas = maxportas + 1 'nuked
             
              'clear all fields and notify scan cancel
              current.text = ""
              openCount.text = ""
             
              screen.Clear
              screen.Add("Scan was canceled by user...", 0)
             
              barra.Reset
             
             
              'stops and clears the timer when scan is canceled
              timing.Enabled = FALSE
              scanTimer.text = ""
             
             
          ENDIF 'ends scan status verification
         
         
         
      NEXT 'ends the scan routine
     
     
     
      'stops the timer when scan ends
      timing.enabled = FALSE



      'setting buttons on stopped...
      btn1.Enabled = TRUE
      btn2.Enabled = FALSE
     
     
      ELSE
      STOP EVENT
      ENDIF
     
END



PUBLIC SUB SocketClient_Ready()
 
  'found open port,  add it to screen...
  screen.Add("Port: " & Connection.Port & "   |   Status: Open", screenCount)
 
  'close the connection
  CLOSE #Connection
 
  'increment screen counter
  screenCount = screenCount + 1
 
 

END



PUBLIC SUB btn2_Click()
 
  'pause the timer
  timing.Enabled = FALSE
 
 'scan cancel confirmation
 SELECT message.Question("Are you sure you want to cancel this scan?", "Yes, cancel it!", "No, continue the scan!")
 
 CASE 1
 scanStatus = FALSE
 
 CASE 2
 timing.Enabled = TRUE
 
 END SELECT

 
END


PUBLIC SUB btnQuit_Click()

  'exit program
  QUIT

END



PUBLIC SUB timing_Timer()

  countTime = countTime + 1
  horas = countTime / 3600
  minutos = countTime / 60
  segundos = segundos + 1
 
 
 
  'routine the format elapsed time
  IF (minutos <= 0 AND horas <= 0) THEN
 
    scanTimer.Text = "0h : 0m : " & segundos & "s"
   
  ELSE IF (segundos > 59) THEN
 
    segundos = 0
   
  ELSE IF (minutos >= 1 AND horas <= 0) THEN
   
    scanTimer.Text = "0h : " & minutos & "m : " & segundos & "s"
   
  ELSE IF (minutos > 59) THEN
 
    minutos = 0
   
  ELSE IF (horas >= 1) THEN
 
    scanTimer.Text = horas & "h : " & minutos & "m : " & segundos & "s"
   
  ENDIF
 
 

 

END


PUBLIC SUB helpShow_Click()

  'displays the help form
  help.show 

END

PUBLIC SUB aboutShow_Click()

  'displays the about form
  about.show 

END


PUBLIC SUB timeout_Timer()


        'incrementing timeout
        timeoutCount = timeoutCount + 1
     

END



-----------------------------------------------------------------------------------
Download Source Package: netScan_1.0alpha3_src
-----------------------------------------------------------------------------------

Any feedback will be very appreciated...

Best regards!

undertuga

  • Newbie
  • *
  • Posts: 9
    • Bunk3r IT Industries
Re: Lan Port Scanner 1.0 alpha
« Reply #7 on: July 24, 2007, 12:24:58 AM »
after some thinking, I decided to expand a little bit the netScan project concept!

Now, the new future steps of development for the relase of version 1.0 alpha4 are:

- Implementation of service detection scheme for each open port found...
- Development of Alive hosts discovery scheme, using ping and such...

With those two new "concepts" i'm trying to make it more usable and usefully, making it more than a simple port scanner!

New ideas, suggestion or any feedback are extremely welcome!



ScreenShot of current alpha4 development:




Best regards!