-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNmap.cs
142 lines (137 loc) · 5.32 KB
/
Nmap.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
namespace Reecon
{
class Nmap
{
public static string DefaultScan(string[] args, bool mustPing)
{
// ip[0]
// outputfile[1]
if (args.Length < 1)
{
Console.WriteLine("Usage: ip outfile");
Environment.Exit(0);
}
string target = "";
string fileName = "";
if (args.Length == 1)
{
target = args[0];
Console.WriteLine("Outfile name (1 word, no extension)");
fileName = Console.ReadLine();
}
else if (args.Length == 2)
{
target = args[0];
fileName = args[1];
}
if (General.GetOS() == General.OS.Windows)
{
List<string> nmapOutput = General.GetProcessOutput("nmap", "-V");
if (nmapOutput.Count == 0 || !nmapOutput[0].Contains("https://nmap.org"))
{
Console.WriteLine("Error - nmap is not installed");
Environment.Exit(0);
}
}
// Check if nmap is installed
else if (General.GetOS() == General.OS.Linux)
{
if (!General.IsInstalledOnLinux("nmap"))
{
Console.WriteLine("Error - nmap is not installed");
Environment.Exit(0);
}
}
else
{
Console.WriteLine("Error - There is no nmap detection on this OS :<");
Environment.Exit(0);
}
DateTime beforeNmapDate = DateTime.Now;
Console.WriteLine($"Doing an optimized Nmap scan on {target} - This may take awhile...");
string noPing = mustPing ? "" : " -Pn ";
if (General.GetOS() == General.OS.Linux)
{
General.RunProcess($"sudo", $"nmap -sS -p- {noPing} --min-rate=5000 {target} -oG {fileName}.nmap");
}
else
{
General.RunProcess($"nmap", $"-sS -p- {noPing} --min-rate=5000 {target} -oG {fileName}.nmap");
}
DateTime afterNmapDate = DateTime.Now;
TimeSpan nmapScanDuration = afterNmapDate - beforeNmapDate;
Console.WriteLine("Scan complete in " + string.Format("{0:0.00}s", nmapScanDuration.TotalSeconds) + $" - {fileName}.nmap for reecon");
return fileName;
}
// Parses an -oG nmap file for ports and scans the results
public static (string Target, List<int> Ports) ParseFile(string fileName)
{
if (!File.Exists(fileName))
{
Console.WriteLine("Error - Cannot find file: " + fileName);
Environment.Exit(0);
}
string returnTarget;
List<int> allPorts = new();
List<int> returnPorts = new();
StreamReader sr1 = new(fileName);
string[] fileLines = sr1.ReadToEnd().Replace("\r", "").Split(new[] { "\n" }, StringSplitOptions.None);
sr1.Close();
// fileLines[1]: Host: 10.10.10.175 () Status: Up
string upLine = fileLines[1];
returnTarget = upLine.Split(' ')[1];
if (fileLines[1].Contains("0 hosts up"))
{
Console.WriteLine("Error - Host is down :(");
Environment.Exit(0);
}
if (!fileLines[2].Contains("/open/"))
{
Console.WriteLine("No open ports found");
return (returnTarget, returnPorts);
}
string portLine = fileLines[2];
string[] portItems = portLine.Split('\t');
string portSection = portItems[1];
portSection = portSection.Replace("Ports: ", "");
foreach (string item in portSection.Split(new[] { ", " }, StringSplitOptions.None))
{
int port = int.Parse(item.Split('/')[0]);
string status = item.Split('/')[1];
if (status == "open")
{
if (!allPorts.Contains(port))
{
allPorts.Add(port);
returnPorts.Add(port);
}
}
else if (status == "filtered")
{
if (!allPorts.Contains(port))
{
allPorts.Add(port);
Console.WriteLine($"Port {port} - Filtered".Recolor(Color.Orange));
}
}
else
{
// Unknown status - Add it to the found list, but skip it
if (!allPorts.Contains(port))
{
allPorts.Add(port);
}
if (status != "closed")
{
Console.WriteLine("Unknown Status: " + port + " -> " + status);
}
}
}
return (returnTarget, returnPorts);
}
}
}