-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortScanner.bat
More file actions
311 lines (296 loc) · 10.3 KB
/
Copy pathPortScanner.bat
File metadata and controls
311 lines (296 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
@echo off
:: ============================================================
:: Name : PortScanner.bat
:: Version : 1.0.0
:: Author : Anamicca23
:: Tested : Windows 10 22H2, Windows 11 23H2
:: Min OS : Windows 10 1803
:: Risk : LOW
:: Admin : Not Required
:: Reversible: Yes (read-only scan, no changes made)
:: Desc : Scans localhost for open TCP/UDP ports using
:: netstat. Maps each open port to its owning
:: process name and PID. No external tools needed.
:: ============================================================
setlocal enabledelayedexpansion
title PORT SCANNER v1.0.0
mode con: cols=80 lines=48
if not "%1"=="RUN" start /MAX cmd /k "%~f0" RUN & exit
:MENU
cls
color 09
echo.
echo +================================================================+
echo ^| P O R T S C A N N E R v1.0.0 ^|
echo ^| See what is listening on your machine ^|
echo +================================================================+
echo ^| ^|
echo ^| [1] Show All Listening Ports (TCP + UDP) ^|
echo ^| [2] Show TCP Listening Ports Only ^|
echo ^| [3] Show UDP Listening Ports Only ^|
echo ^| [4] Show All Established Connections ^|
echo ^| [5] Check a Specific Port ^|
echo ^| [6] Show Ports by Process Name ^|
echo ^| [7] Export Port Report to Desktop ^|
echo ^| [8] Common Ports Reference ^|
echo ^| [0] Exit ^|
echo ^| ^|
echo +================================================================+
echo.
set /p c= Enter Option:
if "%c%"=="1" goto ALLPORTS
if "%c%"=="2" goto TCPPORTS
if "%c%"=="3" goto UDPPORTS
if "%c%"=="4" goto ESTABLISHED
if "%c%"=="5" goto CHECKPORT
if "%c%"=="6" goto BYPROCESS
if "%c%"=="7" goto EXPORTREPORT
if "%c%"=="8" goto REFERENCE
if "%c%"=="0" goto EXIT
goto MENU
:ALLPORTS
cls
color 09
echo.
echo +================================================================+
echo ^| ALL LISTENING PORTS (TCP + UDP) ^|
echo +================================================================+
echo.
echo Proto Local Address PID Process Name
echo ----------------------------------------------------------------
for /f "skip=4 tokens=1,2,5" %%a in ('netstat -ano 2^>nul ^| findstr "LISTENING"') do (
set "proto=%%a"
set "addr=%%b"
set "pid=%%c"
set "procName=Unknown"
for /f "tokens=1 delims=," %%p in ('tasklist /fi "PID eq !pid!" /fo csv /nh 2^>nul') do (
set "procName=%%p"
set "procName=!procName:"=!"
)
call :PAD "!proto!" 8 & set "c1=!_P!"
call :PAD "!addr!" 22 & set "c2=!_P!"
call :PAD "!pid!" 8 & set "c3=!_P!"
echo !c1!!c2!!c3!!procName!
)
echo.
echo Tip: Run as Administrator to see process names for all ports.
echo.
pause
goto MENU
:TCPPORTS
cls
color 0B
echo.
echo +================================================================+
echo ^| TCP LISTENING PORTS ^|
echo +================================================================+
echo.
echo Port Local Address PID Process
echo ----------------------------------------------------------------
for /f "skip=4 tokens=1,2,5" %%a in ('netstat -ano 2^>nul ^| findstr "TCP.*LISTENING"') do (
set "addr=%%b"
set "pid=%%c"
:: Extract just the port number from address
for /f "tokens=2 delims=:" %%p in ("!addr!") do set "port=%%p"
set "procName=Unknown"
for /f "tokens=1 delims=," %%p in ('tasklist /fi "PID eq !pid!" /fo csv /nh 2^>nul') do (
set "procName=%%p" & set "procName=!procName:"=!"
)
call :PAD "!port!" 8 & set "c1=!_P!"
call :PAD "!addr!" 26 & set "c2=!_P!"
call :PAD "!pid!" 8 & set "c3=!_P!"
echo !c1!!c2!!c3!!procName!
)
echo.
pause
goto MENU
:UDPPORTS
cls
color 0D
echo.
echo +================================================================+
echo ^| UDP LISTENING PORTS ^|
echo +================================================================+
echo.
netstat -ano | findstr "UDP"
echo.
pause
goto MENU
:ESTABLISHED
cls
color 0E
echo.
echo +================================================================+
echo ^| ESTABLISHED TCP CONNECTIONS ^|
echo +================================================================+
echo.
echo Proto Local Address Remote Address PID
echo ----------------------------------------------------------------
for /f "skip=4 tokens=1,2,3,5" %%a in ('netstat -ano 2^>nul ^| findstr "ESTABLISHED"') do (
set "proto=%%a" & set "local=%%b" & set "remote=%%c" & set "pid=%%d"
set "procName=Unknown"
for /f "tokens=1 delims=," %%p in ('tasklist /fi "PID eq !pid!" /fo csv /nh 2^>nul') do (
set "procName=%%p" & set "procName=!procName:"=!"
)
call :PAD "!proto!" 8 & set "c1=!_P!"
call :PAD "!local!" 22 & set "c2=!_P!"
call :PAD "!remote!" 24 & set "c3=!_P!"
echo !c1!!c2!!c3!!pid! !procName!
)
echo.
pause
goto MENU
:CHECKPORT
cls
color 09
echo.
echo Enter port number to check (e.g. 80 443 8080 3389):
set /p portNum= Port:
echo.
echo +------------------------------------------------------------+
echo Checking port !portNum!...
echo +------------------------------------------------------------+
echo.
:: Check if it appears in netstat
netstat -ano | findstr ":!portNum! "
if errorlevel 1 (
echo Port !portNum! does not appear to be in use on this machine.
) else (
echo.
:: Try to identify the process
for /f "tokens=5" %%p in ('netstat -ano 2^>nul ^| findstr ":!portNum! "') do (
set "pid=%%p"
echo Owned by PID: !pid!
tasklist /fi "PID eq !pid!" 2>nul | findstr /v "^$" | findstr /v "======" | findstr /v "Image"
)
)
echo.
echo Well-known service on port !portNum!:
call :PORTLOOKUP !portNum!
echo.
pause
goto MENU
:PORTLOOKUP
set "knownPort=%~1"
if "%knownPort%"=="21" echo FTP (File Transfer Protocol)
if "%knownPort%"=="22" echo SSH (Secure Shell)
if "%knownPort%"=="23" echo Telnet
if "%knownPort%"=="25" echo SMTP (Email sending)
if "%knownPort%"=="53" echo DNS (Domain Name System)
if "%knownPort%"=="80" echo HTTP (Web traffic)
if "%knownPort%"=="110" echo POP3 (Email receiving)
if "%knownPort%"=="135" echo Windows RPC
if "%knownPort%"=="139" echo NetBIOS
if "%knownPort%"=="143" echo IMAP (Email)
if "%knownPort%"=="443" echo HTTPS (Secure web traffic)
if "%knownPort%"=="445" echo SMB (Windows file sharing)
if "%knownPort%"=="3306" echo MySQL database
if "%knownPort%"=="3389" echo RDP (Remote Desktop Protocol)
if "%knownPort%"=="5432" echo PostgreSQL database
if "%knownPort%"=="5900" echo VNC (Remote desktop)
if "%knownPort%"=="8080" echo HTTP alternate / dev server
if "%knownPort%"=="8443" echo HTTPS alternate
exit /b
:BYPROCESS
cls
color 09
echo.
echo Enter process name to find its ports (e.g. chrome svchost python):
set /p procSearch= Process:
echo.
echo +------------------------------------------------------------+
echo Ports used by processes matching: !procSearch!
echo +------------------------------------------------------------+
echo.
set "foundAny=0"
for /f "skip=4 tokens=1,2,5" %%a in ('netstat -ano 2^>nul') do (
set "proto=%%a" & set "addr=%%b" & set "pid=%%c"
for /f "tokens=1 delims=," %%p in ('tasklist /fi "PID eq !pid!" /fo csv /nh 2^>nul') do (
set "pname=%%p" & set "pname=!pname:"=!"
echo !pname! | findstr /i "!procSearch!" >nul 2>&1
if !errorlevel!==0 (
echo PID !pid! !proto! !addr! [!pname!]
set "foundAny=1"
)
)
)
if !foundAny!==0 echo No ports found for process "!procSearch!".
echo.
pause
goto MENU
:EXPORTREPORT
cls
color 0B
echo.
echo Exporting port report to Desktop...
set "rpt=%USERPROFILE%\Desktop\PortScan_Report.txt"
(
echo ================================================================
echo PORT SCANNER REPORT
echo Generated : %DATE% %TIME%
echo Computer : %COMPUTERNAME%
echo ================================================================
echo.
echo [ALL LISTENING PORTS]
netstat -ano | findstr "LISTENING"
echo.
echo [ESTABLISHED CONNECTIONS]
netstat -ano | findstr "ESTABLISHED"
echo.
echo [FULL NETSTAT OUTPUT]
netstat -ano
echo.
echo ================================================================
echo END OF REPORT
echo ================================================================
) > "%rpt%" 2>nul
if exist "%rpt%" (
echo [OK] Report saved to Desktop as PortScan_Report.txt
) else (
echo [ERROR] Could not write report.
)
echo.
pause
goto MENU
:REFERENCE
cls
color 09
echo.
echo +================================================================+
echo ^| COMMON PORTS QUICK REFERENCE ^|
echo +================================================================+
echo.
echo PORT PROTOCOL SERVICE
echo -----------------------------------------------
echo 20/21 TCP FTP (File Transfer)
echo 22 TCP SSH (Secure Shell)
echo 23 TCP Telnet (insecure, avoid)
echo 25 TCP SMTP (Send email)
echo 53 TCP/UDP DNS (Domain Name System)
echo 67/68 UDP DHCP (IP addressing)
echo 80 TCP HTTP (Web)
echo 110 TCP POP3 (Receive email)
echo 135 TCP Windows RPC
echo 139/445 TCP SMB (File sharing)
echo 143 TCP IMAP (Email)
echo 443 TCP HTTPS (Secure web)
echo 3306 TCP MySQL
echo 3389 TCP RDP (Remote Desktop)
echo 5432 TCP PostgreSQL
echo 5900 TCP VNC (Remote desktop)
echo 8080 TCP HTTP alternate / dev
echo 8443 TCP HTTPS alternate
echo 27017 TCP MongoDB
echo.
echo Ports below 1024 are well-known (system) ports.
echo Ports 1024-49151 are registered application ports.
echo Ports 49152-65535 are dynamic/ephemeral ports.
echo.
pause
goto MENU
:PAD
set "_P=%~1 "
set "_P=!_P:~0,%~2!"
exit /b
:EXIT
exit