-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkScanner.java
More file actions
94 lines (77 loc) · 2.58 KB
/
NetworkScanner.java
File metadata and controls
94 lines (77 loc) · 2.58 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
import java.util.Scanner;
import java.net.Socket;
import java.net.InetSocketAddress;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class NetworkScanner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter target IP: ");
String host = input.nextLine();
System.out.print("Enter start port: ");
int startPort = input.nextInt();
System.out.print("Enter end port: ");
int endPort = input.nextInt();
// String host = "127.0.0.1"; // target IP
// int startPort = 1;
// int endPort = 1024;
System.out.println("Scanning host: " + host);
for (int port = startPort; port <= endPort; port++) {
final int currentPort = port;
new Thread(() -> {
scanPort(host, currentPort);
}).start();
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static String getServiceName(int port) {
switch (port) {
case 21: return "FTP";
case 22: return "SSH";
case 23: return "TELNET";
case 25: return "SMTP";
case 53: return "DNS";
case 80: return "HTTP";
case 110: return "POP3";
case 143: return "IMAP";
case 443: return "HTTPS";
default: return "Unknown";
}
}
public static void scanPort(String host, int port) {
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(host, port), 500);
System.out.println("Port " + port + " (" + getServiceName(port) + ") is OPEN");
// Banner grabbing
socket.setSoTimeout(500);
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
// For HTTP, send request
if (port == 80 || port == 8080) {
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println("HEAD / HTTP/1.1");
writer.println("Host: " + host);
writer.println();
}
String line = reader.readLine();
if (line != null) {
System.out.println(" Banner: " + line);
}
} catch (Exception e) {
// No banner available
}
socket.close();
} catch (IOException e) {
// closed
}
}
}