hackoo

Multi-Threaded_Network_IP_Ports_Scanner.bat

Sep 15th, 2021 (edited)
2,500
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. Title Multi-Threaded Network IP And Port Scanner Tool by Hackoo 2021
  3. ::=====================================================================================================================
  4. REM The Multi-Threaded Idea is inspired from here ==> http://karceh.blogspot.com/2011/06/batch-script-port-scanner.html
  5. REM First Release on 09/09/2021 (https://pastebin.com/kPthEnDL) Edited and Modified and added Port Scanner by Hackoo
  6. REM -------------------------------------------------------------------------------------------------------------------
  7. REM Update on 15/09/2021 Version 2 Multi-Threaded Network IP And Port Scanner Tool v2
  8. REM Added Multi-Threaded Scan Port
  9. REM Added Function To Calculate Global Time Execution Script
  10. ::=====================================================================================================================
  11. Mode 70,13 & color 0B
  12. Call :init
  13. chcp 65001>nul
  14. set NUMTH=8
  15. set "scanfx=%Temp%\s%random%c%random%n"
  16. Setlocal EnableDelayedExpansion
  17. echo( & Call :ColorText 0E "            Please Wait a While Loading is in Progress"
  18. Call :StartTime
  19. Call :Get_WiFi_Network_Name
  20. Call :GET_LocalIP
  21. Call :Get_MY_MAC
  22. Call :Get_WANIP
  23. Call :wmic nic where NetConnectionStatus=2 get NetConnectionID
  24. set "IPALive=%~dp0%WiFi_Network%_IP_Alive.txt"
  25. cls
  26. If /I "%NetConnection%"=="Wi-Fi" (
  27.     echo(
  28.     echo(    ============================================================
  29.     echo(                The Net Connection is over "%NetConnection%"
  30.     echo(                Connected to "%WiFi_Network%" SSID
  31.     echo(            LAN IP = %LocalIP% - MAC = %MY_MAC%
  32.     echo(                      WAN IP = %WANIP%
  33.     echo(    ============================================================
  34. ) Else (
  35.     echo(    ============================================================
  36.     echo(         The Net Connection is over "%NetConnection%"
  37.     echo(    ============================================================
  38.     @for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /I "Ipv4"') do (
  39.         Set "LocalIP=%%a"
  40.         Set "LocalIP=!LocalIP:~1!"
  41.     )
  42. )
  43.  
  44. If not defined LocalIP (
  45. echo(
  46. Call :ColorText 0C "   Your Local IP address is Empty, So Please Check your Network"
  47.     Timeout /T 10 /NoBreak>nul & Exit
  48. )
  49.  
  50. echo(
  51. Call :ColorText 0D "                Local IP address = !LocalIP!"
  52. @for /f "tokens=1,2,3,4 delims=." %%a in ('echo !LocalIP!') do (Set "A=%%a" & Set "B=%%b" & Set "C=%%c" & Set "D=%%d")
  53.  
  54. (echo for /L %%%%a in ^(1,1,32^) do @ping -n 1 -w 1000 !A!.!B!.!C!.%%%%a ^|find "TTL=" ^>^>%%0.log)>%scanfx%1.bat
  55. (echo for /L %%%%a in ^(33,1,64^) do @ping -n 1 -w 1000 !A!.!B!.!C!.%%%%a ^|find "TTL=" ^>^>%%0.log)>%scanfx%2.bat
  56. (echo for /L %%%%a in ^(65,1,96^) do @ping -n 1 -w 1000 !A!.!B!.!C!.%%%%a ^|find "TTL=" ^>^>%%0.log)>%scanfx%3.bat
  57. (echo for /L %%%%a in ^(97,1,128^) do @ping -n 1 -w 1000 !A!.!B!.!C!.%%%%a ^|find "TTL=" ^>^>%%0.log)>%scanfx%4.bat
  58. (echo for /L %%%%a in ^(129,1,160^) do @ping -n 1 -w 1000 !A!.!B!.!C!.%%%%a ^|find "TTL=" ^>^>%%0.log)>%scanfx%5.bat
  59. (echo for /L %%%%a in ^(161,1,192^) do @ping -n 1 -w 1000 !A!.!B!.!C!.%%%%a ^|find "TTL=" ^>^>%%0.log)>%scanfx%6.bat
  60. (echo for /L %%%%a in ^(193,1,224^) do @ping -n 1 -w 1000 !A!.!B!.!C!.%%%%a ^|find "TTL=" ^>^>%%0.log)>%scanfx%7.bat
  61. (echo for /L %%%%a in ^(225,1,254^) do @ping -n 1 -w 1000 !A!.!B!.!C!.%%%%a ^|find "TTL=" ^>^>%%0.log)>%scanfx%8.bat
  62.  
  63. echo        Please Wait While Scanning The Network is in Progress
  64. echo(
  65. for /L %%i in (1,1,%NUMTH%) do echo (@echo Done ^>%%0.txt ^& exit)>>%scanfx%%%i.bat
  66. for /L %%i in (1,1,%NUMTH%) do start /MIN %scanfx%%%i.bat
  67.  
  68. :: Wait for all threads to finish
  69. :waitthread
  70. REM Remark : I got "◘" by pressing [Alt] + [8]
  71. REM (the actual symbol may vary depending upon codepage).
  72. <nul set /p "_s=◘
  73. @ping -n 2 127.0.0.1>nul
  74. @for /L %%i in (1,1,%NUMTH%) do if not exist %scanfx%%%i.bat.txt goto waitthread
  75. :: Copy the scan logs to a single file
  76. copy %scanfx%*.bat.log %scanfx%.scan.log >nul
  77. :: Clean up, delete temp files
  78. for /L %%i in (1,1,%NUMTH%) do @del /F /Q %scanfx%%%i.bat
  79. for /L %%i in (1,1,%NUMTH%) do @del /F /Q %scanfx%%%i.bat.txt
  80. for /L %%i in (1,1,%NUMTH%) do @del /F /Q %scanfx%%%i.bat.log
  81. CMD /C Start /Min /Wait "" Powershell -C "(GC %scanfx%.scan.log | Select-String -Pattern '\d{1,3}(\.\d{1,3}){3}' -AllMatches).Matches.Value | Out-File -FilePath '%IPALive%'"
  82. REM Start /MAX  notepad.exe %IPALive%
  83. Del /F /Q %scanfx%.scan.log
  84. ::=======================================Port Scanner =========================================
  85. :PortScanner
  86. Setlocal EnableDelayedExpansion
  87. cls
  88. set "IPALive=%~dp0%WiFi_Network%_IP_Alive.txt"
  89. set "ScanPort=%Temp%\P%random%O%random%R%random%T"
  90. Mode 70,25
  91. Set "LogFile_Open_Ports=%~dp0%WiFi_Network%_Open_Ports.txt"
  92. Set /A Count=0
  93. @for /f "delims=" %%i in ('Type "%IPALive%"') do (
  94.     echo( Scanning %%i for Open Port
  95.     Set /A Count+=1
  96.     (
  97.         echo @echo off
  98.         echo Title %%i Scanning for open Port
  99.         echo set vlcPath="%%ProgramFiles%%\VideoLAN\VLC\vlc.exe"
  100.         echo set "vlcFound="
  101.         echo If exist %%vlcPath%% set "vlcFound=OK"
  102.         echo Set "Ports=21 23 80 8080 8800 8001 554 5050 5051"
  103.         echo @for %%%%P in (%%Ports%%^) do (
  104.         echo echo  Scanning the host %%i for open Port %%%%P
  105.         echo ^>^>%%0.log ^(
  106.         echo    @for /f %%%%a in ('powershell "$t=New-Object Net.Sockets.TcpClient;try{$t.Connect('%%i',%%%%P)}catch{};$t.Connected"'^) do (
  107.         echo        @If /I [%%%%a] EQU [True] (echo %%i:%%%%P^)
  108.         echo        @If /I [%%%%a] EQU [True] If [%%%%P] EQU [554] If defined vlcFound (Start "" %%vlcPath%% "rtsp://%%i" --fullscreen^)
  109.         echo        ^)
  110.         echo    ^)
  111.         echo ^)
  112.         echo echo Done^>%%0.txt ^& exit
  113.     )>%ScanPort%_!Count!.bat
  114. )
  115. REM @for /L %%i in (1,1,%Count%) do echo (@echo Done ^>%%0.txt ^& exit)>>%ScanPort%_%%i.bat
  116. @for /L %%i in (1,1,%Count%) do start /MIN %ScanPort%_%%i.bat
  117.  
  118. :: Wait for all threads of scan port to finish
  119. :WaitScanPort
  120. REM Remark : I got "◘" by pressing [Alt] + [8]
  121. REM (the actual symbol may vary depending upon codepage).
  122. <nul set /p "_s=◘
  123. @ping -n 2 127.0.0.1>nul
  124. @for /L %%i in (1,1,%Count%) do if not exist %ScanPort%_%%i.bat.txt goto :WaitScanPort
  125. :: Copy the scan logs to a single file
  126. REM echo Copy the scan logs to a single file
  127. copy %ScanPort%*.log "%LogFile_Open_Ports%">nul
  128. echo(
  129. call :GetDuration
  130. Timeout /T 2 /NoBreak>nul
  131. :: Clean up, delete temp files
  132. for /L %%i in (1,1,%Count%) do @del /F /Q %ScanPort%_%%i.bat
  133. for /L %%i in (1,1,%Count%) do @del /F /Q %ScanPort%_%%i.bat.txt
  134. for /L %%i in (1,1,%Count%) do @del /F /Q %ScanPort%_%%i.bat.log
  135. If Exist "%LogFile_Open_Ports%" Start /MAX "Log" "%LogFile_Open_Ports%" & Exit
  136. ::------------------------------------------------------------------------------------
  137. :init
  138. prompt $g
  139. @for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
  140. exit /b
  141. ::-----------------------------------------------------------------------------------
  142. :ColorText
  143. echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & Exit /B
  144. exit /b
  145. ::-----------------------------------------------------------------------------------
  146. :wmic
  147. @for /f "skip=1 delims=" %%A in ('"wmic %*"') do for /f "delims=" %%B in ("%%A") do set "NetConnection=%%B"
  148. Call :Trim "%NetConnection%" NetConnection
  149. exit /b
  150. ::-----------------------------------------------------------------------------------
  151. :Get_WiFi_Network_Name
  152. @for /f "delims=: tokens=2" %%a in (
  153.     'netsh wlan show interface name="Wi-Fi" ^| find /I "Profil"'
  154. ) do Set "Profil=%%a"
  155. Call :Trim "%Profil%" WiFi_Network
  156. Exit /B
  157. ::-----------------------------------------------------------------------------------
  158. :Trim <Var> <NewVar>
  159. >"%tmp%\%~n0.vbs" (echo Wscript.echo Trim("%~1"^))
  160. @for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do set "%2=%%a"
  161. Del "%tmp%\%~n0.vbs"
  162. exit /b
  163. ::-----------------------------------------------------------------------------------
  164. :GET_MAC
  165. Set "MAC="
  166. @for /f "tokens=2" %%M in ('arp -a %1 2^>NUL ^| find "%1"') do Set "MAC=%%M"
  167. Exit /B
  168. ::-----------------------------------------------------------------------------------
  169. :Get_MY_MAC
  170. Set "My_MAC="
  171. @for /f "delims=: tokens=1*" %%a in (
  172.     'netsh wlan show interface name="Wi-Fi" ^| find "phy"'
  173. ) do set "MY_MAC=%%b"
  174. Call :Trim "%MY_MAC%" MY_MAC
  175. Exit /B
  176. ::-----------------------------------------------------------------------------------
  177. :GET_LocalIP <LocalIP>
  178. Set "LocalIP="
  179. @for /f "delims=: tokens=2" %%a in ('ipconfig ^| findstr IPv4') do set "IPaddr=%%a"
  180. Call :Trim "%IPaddr%" LocalIP
  181. Exit /B
  182. ::-----------------------------------------------------------------------------------
  183. :Get_WANIP
  184. Set "MyCommand=nslookup myip.opendns.com resolver1.opendns.com 2^>nul"
  185. @for /f "skip=4 delims=: tokens=2" %%a in ('%MyCommand%') do (
  186.     Set "WANIP=%%a"
  187. )
  188. Call :Trim "%WANIP%" WANIP
  189. Exit /B
  190. ::-----------------------------------------------------------------------------------
  191. :GetDuration
  192. echo(------------------------------------
  193. echo( Start Time Scan   : %MyStartTime%
  194. Call :EndTime
  195. echo( Finish Time Scan  : %MyEndTime%
  196. echo(------------------------------------
  197. echo( Time Elapsed Scan : %TimeElapsed%
  198. echo(------------------------------------
  199. Timeout /T 5 /NoBreak>nul
  200. >>"%LogFile_Open_Ports%" (
  201. echo ===================================================
  202. echo The Net Connection is over "%NetConnection%"
  203. echo Connected to "%WiFi_Network%" SSID
  204. echo LAN IP = %LocalIP% - MAC = %MY_MAC%
  205. echo WAN IP = %WANIP%
  206. echo ===================================================
  207. echo( Start Time Scan   : %MyStartTime%
  208. echo( Finish Time Scan  : %MyEndTime%
  209. echo(-------------------------------------
  210. echo( Time Elapsed Scan : %TimeElapsed%
  211. )
  212. Exit /b
  213. ::==========================================================================================================================
  214. :StartTime
  215. Set "MyStartTime=%Time%"
  216. @for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set t=%%I
  217. set /a t1 = %t:~8,1%*36000 + %t:~9,1%*3600 + %t:~10,1%*600 + %t:~11,1%*60 + %t:~12,1%*10 + %t:~13,1% && set t1=!t1!%t:~15,3%
  218. Exit /B
  219. ::==========================================================================================================================
  220. :EndTime
  221. Set "MyEndTime=%Time%"
  222. @for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set t=%%I
  223. set /a t2 = %t:~8,1%*36000 + %t:~9,1%*3600 + %t:~10,1%*600 + %t:~11,1%*60 + %t:~12,1%*10 + %t:~13,1% && set t2=!t2!%t:~15,3%
  224. set /a t2-=t1 && if !t2! lss 0 set /a t2+=24*3600000
  225. set /a "h=t2/3600000,t2%%=3600000,m=t2/60000,t2%%=60000" && set t2=00000!t2!&& set t2=!t2:~-5!
  226. if %h% leq 9 (set h=0%h%) && if %m% leq 9 (set m=0%m%)
  227. set TimeElapsed=%h%:%m%:%t2:~0,2%.%t2:~2,3%
  228. Exit /B
  229. ::==========================================================================================================================
RAW Paste Data