Skip to content

Commit

Permalink
feat: make local share scanning opt-in #38 (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonGurney authored Jul 18, 2022
1 parent 6e1d97f commit 200ca82
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
7 changes: 1 addition & 6 deletions FileDiscovery/FileFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public List<File> Files
}
}

public FileFinder(List<Share> shares, bool enumerateLocalDrives = true, bool getPermissionsForSingleFileInDir = true, bool enumerateAcls = true, bool quiet = false, bool verbose = false, bool crossPlatform = false)
public FileFinder(List<Share> shares, bool getPermissionsForSingleFileInDir = true, bool enumerateAcls = true, bool quiet = false, bool verbose = false, bool crossPlatform = false)
{
pClientContext = IntPtr.Zero;
if (! crossPlatform)
Expand All @@ -65,11 +65,6 @@ public FileFinder(List<Share> shares, bool enumerateLocalDrives = true, bool get
_directories.Add(new Directory(path: "", share:share) { DirectoryType = Enums.DirectoryTypeEnum.SMB });
}

/* TODO: Reimplement in future
if (enumerateLocalDrives)
_directories.AddRange(GetLocalDriveDirectories());
*/

if (!quiet)
OutputHelper.WriteLine($"6a. Enumerating all subdirectories for known paths");

Expand Down
17 changes: 11 additions & 6 deletions NetworkDiscovery/NetworkFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,25 @@ public List<string> DiscoverAddressesViaSockets()
return new HashSet<string>(addresses).ToList();
}

public List<string> DiscoverNetworksViaClientConfiguration()
public List<string> DiscoverNetworksViaClientConfiguration(bool store=true)
{
List<string> localAddresses = new();
foreach (NetworkInterface iface in NetworkInterface.GetAllNetworkInterfaces())
{
List<UnicastIPAddressInformation> addresses = iface.GetIPProperties().UnicastAddresses.ToList();
foreach (UnicastIPAddressInformation address in addresses)
{
AddLocalAddress(address.Address.ToString());
// Convert to network and attempt to store
string net = ConvertAddressToNetwork(address.Address.ToString(), address.IPv4Mask.ToString());
AddNetwork(net, NetworkDiscoverySourceEnum.LOCAL);
localAddresses.Add(address.Address.ToString());
if (store)
{
AddLocalAddress(address.Address.ToString());
// Convert to network and attempt to store
string net = ConvertAddressToNetwork(address.Address.ToString(), address.IPv4Mask.ToString());
AddNetwork(net, NetworkDiscoverySourceEnum.LOCAL);
}
}
}
return new List<string>();
return localAddresses;
}

public void AddLocalAddress(string address)
Expand Down
27 changes: 11 additions & 16 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ static void Run(Options opts)
OutputHelper.WriteLine("ERROR: Username and Password required on none Windows platforms");
Environment.Exit(1);
}
if (opts.EnumerateLocalDrives)
{
OutputHelper.WriteLine("ERROR: We cannot enumrate local drives on None Windows platforms");
Environment.Exit(1);
}
}

if (opts.Username == null ^ opts.Password == null)
Expand All @@ -57,7 +52,11 @@ static void Run(Options opts)
{
crossPlatform = true;
// The library we use hangs when scanning ourselves
opts.DisableLocalShares = true;
if (opts.ScanLocalShares)
{
OutputHelper.WriteLine("ERROR: We cannot scan local shares when running on Linux or with commandline credentials");
Environment.Exit(1);
}
}

String username = "";
Expand Down Expand Up @@ -152,9 +151,10 @@ static void Run(Options opts)
OutputHelper.WriteLine("2. Skipping filtering as network discovery disabled...");
}

if (opts.DisableLocalShares)
filteredAddresses.AddRange(nf.LocalAddresses);

if (! opts.ScanLocalShares)
{
filteredAddresses.AddRange(nf.DiscoverNetworksViaClientConfiguration(store:false));
}
filteredAddresses.AddRange(opts.ExcludedHosts.ToList());

List<string> addresses = new();
Expand Down Expand Up @@ -311,7 +311,6 @@ static void Run(Options opts)
ff = new(
shares: shares,
getPermissionsForSingleFileInDir: opts.EnumerateOnlyASingleFilesAcl,
enumerateLocalDrives: (opts.EnumerateLocalDrives && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)),
enumerateAcls: !opts.DontEnumerateAcls,
verbose: opts.Verbose,
crossPlatform:crossPlatform
Expand Down Expand Up @@ -373,11 +372,8 @@ public class Options
[Option('f', "fast", Required = false, HelpText = "Enumerate only one files permissions per directory")]
public bool EnumerateOnlyASingleFilesAcl { get; set; }

[Option('l', "scan-local-drives", Required = false, HelpText = "Scan local drives on this machine")]
public bool EnumerateLocalDrives { get; set; }

[Option('L', "exclude-local-shares", Required = false, HelpText = "Do not scan local shares on this machine")]
public bool DisableLocalShares { get; set; }
[Option('l', "scan-local-shares", Required = false, HelpText = "Scan the local shares on this machine")]
public bool ScanLocalShares { get; set; }

[Option('D', "disable-network-discovery", Required = false, HelpText = "Disable network discovery")]
public bool DisableNetworkDiscovery { get; set; }
Expand Down Expand Up @@ -433,7 +429,6 @@ public static IEnumerable<Example> Examples
yield return new Example("Output to elasticsearch (Preffered)", unParserSettings, new Options { ElasticsearchHost = "127.0.0.1" });
yield return new Example("Output to elasticsearch and CSV", unParserSettings, new Options { ElasticsearchHost = "127.0.0.1", CsvFile = "out.csv" });
yield return new Example("Disable network discovery and provide manual networks", unParserSettings, new Options { ElasticsearchHost = "127.0.0.1", DisableNetworkDiscovery = true, Networks = new List<String>() { "192.168.12.0./23", "192.168.15.0/24" } });
yield return new Example("Scan local filesystem too (SLOW)", unParserSettings, new Options { ElasticsearchHost = "127.0.0.1", EnumerateLocalDrives = true });
yield return new Example("Do not enumerate ACLs (FASTER)", unParserSettings, new Options { ElasticsearchHost = "127.0.0.1", DontEnumerateAcls = true });
}
}
Expand Down

0 comments on commit 200ca82

Please sign in to comment.