TCP port scanner

Miscellaneous Forums/Blitz Showcase/TCP port scanner

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Wrote a little TCP port scanner in BlitzPlus. Finds most of the common servers (FTP, HTTP, POP3, IMAP, MySQL etc) within 30 seconds.

Screenshot:


Compile this code to "port.exe" and run it, othwerwise it won't work:
Type ports
	Field port%, name$
End Type

AddPort("HTTP", 80)
AddPort("FTP", 21)

AddPort("POP3", 110)
AddPort("SMTP", 25)
AddPort("IMAP", 143)
AddPort("IRC", 194)
AddPort("Telnet", 23)
AddPort("IMAP3", 220)

AddPort("FTP data", 20)
AddPort("SFTP", 115)
AddPort("SIMAP", 993)
AddPort("SPOP", 995)
AddPort("HTTP", 8080)
AddPort("MySQL", 3306)
AddPort("ICQ", 4000)
AddPort("Yahoo messenger", 5010)
AddPort("AOL messenger", 5190)
AddPort("Napster", 6699)

AddPort("SSH", 22)
AddPort("whois", 43)
AddPort("DNS", 53)
AddPort("DHCP", 68)
AddPort("Finger", 79)
AddPort("NNTP", 119)
AddPort("NTP", 123)
AddPort("SNMP", 161)
AddPort("SSL", 443)
AddPort("SMB", 445)

; Scanner
If Len(CommandLine$()) > 2
	host$ = Mid$(CommandLine$(), 2)
	stream% = CreateUDPStream()
	
	test% = OpenTCPStream(Sector(host$, ":", 0), Sector(host$, ":", 1))
	If test% Then
		CloseTCPStream test%
		
		WriteLine stream%, Sector(host$, ":", 1)
		SendUDPMsg stream%, HostIP(CountHostIPs("")), 11111
		AddLog(host$ + " (" + PortName$(Int Sector(host$, ":", 1)) + ") Successfull!")
	EndIf
	CloseUDPStream stream%
	
	End
EndIf

; Client
AppTitle "TCP port scanner"
Print "TCP port scanner":Print ""

stream% = CreateUDPStream(11111)
timeout% = 30 * 1000

Repeat
	cmd$ = Input$("-> ")
	
	Select Lower(cmd$)
		Case "open", "o", "scan"
			server$ = Input("Host: ")
			If Len(server$) => 5
				AddLog("-------------------- " + CurrentDate() + " " + CurrentTime() + " --------------------")
				
				Print "Prepare streams..."
				For this.ports = Each ports
					ExecFile("port " + server$ + ":" + this\port)
				Next
				
				Print "Waiting responses (30 seconds)..."
				tim% = MilliSecs()
				Repeat
					from% = RecvUDPMsg(stream%)
					If from%
						avail% = ReadAvail(stream%)
						If avail% Then
							port% = Int ReadLine$(stream%)
							Print server$ + ":" + port% + " (" + PortName$(port%) + ") Successfull!"
						EndIf
					EndIf
					
					If MilliSecs() - tim > timeout%
						Exit
					EndIf
				Forever
				Print "THAT'S IT!"
			Else
				Print "Invalid host!"
			EndIf
		Case "add", "addport"
			portname$ = Input("Port name: ")
			num% = Input("Port number: ")
			If Len(portname$) > 0 And num% > 0
				AddPort(portname$, num%)
				Print "Added port " + num% + " as " + portname$ + " to scan list!"
			Else
				Print "Port not added!"
			EndIf
		Case "ports"
			For this.ports = Each ports
				Print this\port% + " is " + this\name$
			Next
		Case "help", "?"
			Print "open, scan - Scan for ports"
			Print "add, addport - Add manually a port to scan"
			Print "ports - List ports that will be scanned"
			Print "exit, close, bye, quit - Quit TCP port scanner"
		Case "exit", "close", "bye", "quit"
			End
		Default
			Print "No such command! Use 'help' or '?' for command list."
	End Select
Forever

Function AddPort(name$, port%)
	this.ports = New ports
		this\port% = port%
		this\name$ = name$
End Function

Function PortName$(port%)
	For this.ports = Each ports
		If this\port% = port% Return this\name$
	Next
	Return "User defined"
End Function

Function Sector$(txt$, separator$, sector%, toend% = False)
	Local result$ = "", occ
	For i = 1 To Len(txt$)
		If Mid$(txt$, i, 1) = separator$
			occ = occ + 1
			If toend% And occ% > sector% Then result$ = result$ + Mid$(txt$, i, 1)
		Else
			If occ => sector Then result$ = result$ + Mid$(txt$, i, 1)
		EndIf
		If Not toend% Then If occ > sector Then Exit
	Next
	Return result$
End Function

Function Sectors%(txt$, needle$)
	occ% = 0
	For i = 1 To Len(txt$) Step 1
		If Instr(txt$, needle$, i)
			occ% = occ% + 1
			i = Instr(txt$, needle$, i)
		Else
			Exit
		EndIf
	Next
	Return occ%
End Function

Function AddLog(txt$)
	; Log
	If Not FileType("log.txt") Then CloseFile WriteFile("log.txt")
	Repeat
		file% = OpenFile("log.txt")
		If file%
			SeekFile(file%, FileSize("log.txt"))
			WriteLine file%, txt$
		EndIf
	Until file%
	CloseFile file%
End Function

You need to compile it because it uses one "port.exe" for each port. One exe would take too long :)

Results for blitzbasic.com:
www.blitzbasic.com:21 (FTP) Successfull!
www.blitzbasic.com:80 (HTTP) Successfull!
www.blitzbasic.com:3306 (MySQL) Successfull!
www.blitzbasic.com:995 (SPOP) Successfull!
www.blitzbasic.com:53 (DNS) Successfull!
www.blitzbasic.com:143 (IMAP) Successfull!
www.blitzbasic.com:993 (SIMAP) Successfull!
www.blitzbasic.com:443 (SSL) Successfull!
www.blitzbasic.com:22 (SSH) Successfull!
www.blitzbasic.com:110 (POP3) Successfull!