main
jeffrey 2025-03-29 09:54:44 +01:00
parent 8b0f909a10
commit 4bf0c12507
1 changed files with 80 additions and 74 deletions

View File

@ -14,32 +14,33 @@ using System.Threading.Tasks;
namespace SharpScan namespace SharpScan
{ {
public class Options
{
[Option('h', "hosts", Required = true, Separator = ',', HelpText = "Hosts to scan, coma separated. CIDR accepted")]
public IEnumerable<string> Hosts { get; set; }
[Option('p', "ports", Required = false, Separator = ',', HelpText = "TCP port to scan, coma separated")]
public IEnumerable<int> Ports { get; set; }
[Option('t', "timeout", Required = false, Default = 500, HelpText = "Timeout in milliseconds to check a port")]
public int Timeout { get; set; }
[Option('d', "delay", Required = false, Default = 0, HelpText = "Delay in milliseconds between 2 scan request")]
public int Delay { get; set; }
[Option('j', "jittler", Required = false, Default = 0, HelpText = "Jittler to apply to delay, in percent, new evalutation for each request")]
public int Jittler { get; set; }
[Option('r', "randomize", Required = false, Default = false, HelpText = "Randomize hosts/ports scan order. Results will be printed in normal order")]
public bool Randomize { get; set; }
[Option('f', "top-ports", Required = false, Default = 0, HelpText = "Equivalent of nmap --top-ports option")]
public int TopPorts { get; set; }
}
class Program class Program
{ {
public class Options
{
[Option('h', "hosts", Required = true, Separator = ',', HelpText = "Hosts to scan, coma separated. CIDR accepted")]
public IEnumerable<string> Hosts { get; set; }
[Option('p', "ports", Required = false, Separator = ',', HelpText = "TCP port to scan, coma separated")]
public IEnumerable<int> Ports { get; set; }
[Option('t', "timeout", Required = false, Default = 500, HelpText = "Timeout in milliseconds to check a port")]
public int Timeout { get; set; }
[Option('d', "delay", Required = false, Default = 0, HelpText = "Delay in milliseconds between 2 scan request")]
public int Delay { get; set; }
[Option('j', "jittler", Required = false, Default = 0, HelpText = "Jittler to apply to delay, in percent, new evalutation for each request")]
public int Jittler { get; set; }
[Option('r', "randomize", Required = false, Default = false, HelpText = "Randomize hosts/ports scan order. Results will be printed in normal order")]
public bool Randomize { get; set; }
[Option('f', "top-ports", Required = false, Default = 0, HelpText = "Equivalent of nmap --top-ports option")]
public int TopPorts { get; set; }
}
public static List<string> GetIpsFromCidr(string cidr) public static List<string> GetIpsFromCidr(string cidr)
@ -134,9 +135,7 @@ namespace SharpScan
} }
} }
static void RunOptions(Options o)
static void Main(string[] args)
{ {
List<(string address, int port)> cibles = new List<(string address, int port)>(); List<(string address, int port)> cibles = new List<(string address, int port)>();
List<string> addresses = new List<string>(); List<string> addresses = new List<string>();
@ -149,69 +148,63 @@ namespace SharpScan
int jittler = 0; int jittler = 0;
int top_port = 0; int top_port = 0;
// Gestion des hosts
Parser.Default.ParseArguments<Options>(args).WithParsed<Options>(o => { foreach (string host in o.Hosts)
{
// Gestion des hosts if (host.Contains('/'))
foreach (string host in o.Hosts)
{ {
if (host.Contains('/')) addresses.AddRange(GetIpsFromCidr(host));
{
addresses.AddRange(GetIpsFromCidr(host));
}
else
{
addresses.Add(host);
}
} }
else
// Gestion des ports
if (o.Ports.Count() > 0)
{ {
if (o.Ports.Count() == 1 && o.Ports.First() == -1) addresses.Add(host);
}
}
// Gestion des ports
if (o.Ports.Count() > 0)
{
if (o.Ports.Count() == 1 && o.Ports.First() == -1)
{
foreach (int port in Enumerable.Range(0, 65536))
{ {
foreach (int port in Enumerable.Range(0, 65536)) ports.Add(port);
{
ports.Add(port);
}
}
else
{
foreach (int port in o.Ports)
{
ports.Add(port);
}
} }
} }
else else
{ {
if (o.TopPorts > 0) foreach (int port in o.Ports)
{ {
if (o.TopPorts <= Utils.top_ports_list.Count()) ports.Add(port);
{ }
ports = Utils.top_ports_list.GetRange(0, o.TopPorts); }
} }
else else
{ {
ports = Utils.top_ports_list; if (o.TopPorts > 0)
} {
if (o.TopPorts <= Utils.top_ports_list.Count())
{
ports = Utils.top_ports_list.GetRange(0, o.TopPorts);
} }
else else
{ {
ports = Utils.top_ports_list.GetRange(0, 12); ports = Utils.top_ports_list;
} }
} }
else
{
ports = Utils.top_ports_list.GetRange(0, 12);
}
}
timeout = o.Timeout; timeout = o.Timeout;
randomize = o.Randomize; randomize = o.Randomize;
delay = o.Delay; delay = o.Delay;
jittler = o.Jittler; jittler = o.Jittler;
top_port = o.TopPorts; top_port = o.TopPorts;
});
// Construction des cibles // Construction des cibles
foreach (string address in addresses) foreach (string address in addresses)
@ -252,7 +245,7 @@ namespace SharpScan
foreach ((string address, int port) in cibles) foreach ((string address, int port) in cibles)
{ {
if( cpt % fraction == 0) if (cpt % fraction == 0)
{ {
cpt_fraction += 10; cpt_fraction += 10;
Console.WriteLine($"Request {cpt}/{cibles.Count} ({cpt_fraction})%"); Console.WriteLine($"Request {cpt}/{cibles.Count} ({cpt_fraction})%");
@ -293,6 +286,19 @@ namespace SharpScan
} }
Console.WriteLine($" {port} opened ({Utils.GetDesc(port)})"); Console.WriteLine($" {port} opened ({Utils.GetDesc(port)})");
} }
}
static void HandleParseError(IEnumerable<Error> errs)
{
//handle errors
}
static void Main(string[] args)
{
CommandLine.Parser.Default.ParseArguments<Options>(args)
.WithParsed(RunOptions)
.WithNotParsed(HandleParseError);
} }
} }
} }