-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNmap.cs
More file actions
140 lines (134 loc) · 5.36 KB
/
Nmap.cs
File metadata and controls
140 lines (134 loc) · 5.36 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
namespace Reecon
{
internal static class Nmap
{
// Does an optimised nmap scan of the host, and outputs it in a greppable format for processing
public static string DefaultScan(string[] args, bool mustPing)
{
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() ?? "reecon";
}
else if (args.Length == 2)
{
target = args[0];
fileName = args[1];
}
if (General.GetOperatingSystem() == General.OperatingSystem.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.GetOperatingSystem() == General.OperatingSystem.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.GetOperatingSystem() == General.OperatingSystem.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("Optimized Nmap Scan complete in " + $"{nmapScanDuration.TotalSeconds:0.00}s" + $" - Created {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);
}
List<int> allPorts = new();
List<int> returnPorts = new();
StreamReader sr1 = new(fileName);
string[] fileLines = sr1.ReadToEnd().Replace("\r", "").Split(["\n"], StringSplitOptions.None);
sr1.Close();
// fileLines[1]: Host: 10.10.10.175 () Status: Up
string upLine = fileLines[1];
string returnTarget = upLine.Split(' ')[1];
if (fileLines[1].Contains("0 hosts up"))
{
Console.WriteLine("Error - Host is down :(");
Environment.Exit(0);
}
string portLine = fileLines[2];
string[] portItems = portLine.Split('\t');
string portSection = portItems[1];
portSection = portSection.Replace("Ports: ", "");
foreach (string item in portSection.Split([", "], StringSplitOptions.None))
{
if (item == "")
{
continue;
}
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);
}
}
}