main
jeffrey 2025-03-29 14:47:43 +01:00
parent 4bf0c12507
commit 872c4f2c10
1 changed files with 49 additions and 47 deletions

View File

@ -13,10 +13,6 @@ using System.Threading.Tasks;
namespace SharpScan namespace SharpScan
{
class Program
{ {
public class Options public class Options
{ {
@ -24,7 +20,7 @@ namespace SharpScan
public IEnumerable<string> Hosts { get; set; } public IEnumerable<string> Hosts { get; set; }
[Option('p', "ports", Required = false, Separator = ',', HelpText = "TCP port to scan, coma separated")] [Option('p', "ports", Required = false, Separator = ',', HelpText = "TCP port to scan, coma separated")]
public IEnumerable<int> Ports { get; set; } public IEnumerable<string> Ports { get; set; }
[Option('t', "timeout", Required = false, Default = 500, HelpText = "Timeout in milliseconds to check a port")] [Option('t', "timeout", Required = false, Default = 500, HelpText = "Timeout in milliseconds to check a port")]
public int Timeout { get; set; } public int Timeout { get; set; }
@ -42,6 +38,10 @@ namespace SharpScan
public int TopPorts { get; set; } public int TopPorts { get; set; }
} }
class Program
{
public static List<string> GetIpsFromCidr(string cidr) public static List<string> GetIpsFromCidr(string cidr)
{ {
@ -165,7 +165,7 @@ namespace SharpScan
// Gestion des ports // Gestion des ports
if (o.Ports.Count() > 0) if (o.Ports.Count() > 0)
{ {
if (o.Ports.Count() == 1 && o.Ports.First() == -1) if (o.Ports.Count() == 1 && o.Ports.First() == "-")
{ {
foreach (int port in Enumerable.Range(0, 65536)) foreach (int port in Enumerable.Range(0, 65536))
{ {
@ -174,9 +174,23 @@ namespace SharpScan
} }
else else
{ {
foreach (int port in o.Ports) foreach (string port in o.Ports)
{ {
ports.Add(port); if (port.Contains("-"))
{
int start = Int32.Parse(port.Split('-')[0]);
int end = Int32.Parse(port.Split('-')[1]);
int nb = end - start + 1;
foreach (int i in Enumerable.Range(start, nb))
{
ports.Add(i);
}
}
else
{
ports.Add(Int32.Parse(port));
}
} }
} }
} }
@ -211,6 +225,7 @@ namespace SharpScan
{ {
foreach (int port in ports) foreach (int port in ports)
{ {
cibles.Add((address, port)); cibles.Add((address, port));
} }
} }
@ -220,6 +235,7 @@ namespace SharpScan
Shuffle(cibles); Shuffle(cibles);
} }
// Check wich ports are available // Check wich ports are available
Random r; Random r;
int sleep_value; int sleep_value;
@ -230,26 +246,12 @@ namespace SharpScan
chronometre.Start(); chronometre.Start();
int fraction = cibles.Count / 10; int percent = 0;
int cpt_fraction = 0;
// Estimation de la durée du scan
int total = 0;
total += ((delay + (jittler * delay / 100)) * cibles.Count) / 1000;
total += (timeout * cibles.Count) / 1000;
total += (2 * 100) / total; // On rajoute un chouilla pour le temps de traitement.
TimeSpan temps = TimeSpan.FromSeconds(total);
string total_formatee = temps.ToString(@"hh\:mm\:ss");
Console.WriteLine($"Duree estimée: {total_formatee}");
foreach ((string address, int port) in cibles) foreach ((string address, int port) in cibles)
{ {
if (cpt % fraction == 0) percent = (cpt * 100) / cibles.Count;
{ Console.Write($"\rRequest {cpt}/{cibles.Count} ({percent}%)");
cpt_fraction += 10;
Console.WriteLine($"Request {cpt}/{cibles.Count} ({cpt_fraction})%");
}
if (IsPortOpen(address, port, timeout)) if (IsPortOpen(address, port, timeout))
{ {
@ -260,7 +262,6 @@ namespace SharpScan
r = new Random(); r = new Random();
sleep_value = r.Next(delay, delay + (jittler * delay / 100)); sleep_value = r.Next(delay, delay + (jittler * delay / 100));
Thread.Sleep(sleep_value); Thread.Sleep(sleep_value);
cpt += 1; cpt += 1;
} }
@ -269,7 +270,8 @@ namespace SharpScan
TimeSpan duree = chronometre.Elapsed; TimeSpan duree = chronometre.Elapsed;
string dureeFormatee = duree.ToString(@"hh\:mm\:ss"); // Format : heures:minutes string dureeFormatee = duree.ToString(@"hh\:mm\:ss"); // Format : heures:minutes
Console.WriteLine($"Durée (heures:minutes) : {dureeFormatee}");
Console.WriteLine($"\nDurée (heures:minutes) : {dureeFormatee}");
// Affichage des résultats // Affichage des résultats