/// <summary> /// Perform new scan on host /// </summary> /// <param name="host"></param> public static void ReScanHost(SMBHost host) { HostShare[] hostShares; List <string> discoveredHostShares; SMBScanResult currentResult; // If the recursive level is not set in the Config class, we use the level used for the first scan if (Config.ScanForNewSharesRecusiveLevel == -1) { Config.ScanForNewSharesRecusiveLevel = host.scanRecursiveLevel; } foreach (SMBScanResult scanResult in host.hostSharesScanResult.Values) { ReScanSMBScanResult(scanResult); } // Check whether the scan will be performed on discovered shares only or try to identify new shares. // The discovery operation includes only the scanned hosts. To add new hosts you should use AppendHosts method. if (Config.ScanForNewShares) { hostShares = GetNetShare.EnumNetShares(host.hostname); if (host.hostSharesScanResult.Count > 0) { discoveredHostShares = host.hostSharesScanResult.Keys.ToList(); foreach (HostShare hostShare in hostShares) { if (!discoveredHostShares.Contains(hostShare.shareInfo.shi1_netname)) { currentResult = new SMBScanResult { shareACL = ShareACLUtils.GetShareACL(hostShare), shareSubDirectories = new Dictionary <string, ScanDirectoryResult>() }; if (IsRecursivelyScannable(currentResult.shareACL.share)) { currentResult.shareSubDirectories = ScanShareDirectory(hostShare.ToString(), Config.ScanForNewSharesRecusiveLevel).shareDirectorySubDirectories; } host.hostSharesScanResult.Add(hostShare.shareInfo.shi1_netname, currentResult); } } } else { host.hostSharesScanResult = ScanHost(host.hostname).hostSharesScanResult; } } }
/// <summary> /// Fetch the ACL of a share (and his subdirectories) and append them to the evolution list /// </summary> /// <param name="scanResult"></param> public static void ReScanSMBScanResult(SMBScanResult scanResult) { scanResult.shareACL.AddShareACL(ShareACLUtils.GetShareACL(scanResult.shareACL.share.ToString())); if (scanResult.shareSubDirectories.Count > 0) { ReScanScanDirectoryResults(scanResult.shareSubDirectories, Config.ScanForNewSharesRecusiveLevel - 1); } if (Config.ScanForNewSharesRecusiveLevel > 0 && IsRecursivelyScannable(scanResult.shareACL.share)) { // Get share subdirectories and check if there are new ones foreach (string subDirectory in GetSubDirectories(scanResult.shareACL.share.ToString())) { if (!scanResult.shareSubDirectories.ContainsKey(subDirectory.Split('\\').Last())) { // If so, then perform a scan on the new subdirectories scanResult.shareSubDirectories.Add(subDirectory, ScanShareDirectory(subDirectory, Config.ScanForNewSharesRecusiveLevel - 1)); } } } }
/// <summary> /// Scan host's SMB shares /// </summary> /// <param name="hostname">Target to scan.</param> /// <returns></returns> public static SMBHost ScanHost(string hostname) { SMBHost result = new SMBHost(); SMBScanResult currentResult; HostShare[] hostShares; IPAddress ip = null; result.scanRecursiveLevel = Config.RecursiveLevel; result.hostname = hostname; try { ip = IPAddress.Parse(hostname); result.ip = ip.ToString(); } catch (FormatException) { if ((Config.TryResolveHostName && !TryResolveHostName(hostname))) { Console.WriteLine("[-][" + DateTime.Now.ToString() + "] Could not resolve " + hostname); return(result); } result.ip = ""; } // Get target's shares try { if (Config.Debug) { Console.WriteLine("[*][" + DateTime.Now.ToString() + "] Getting " + hostname + " shares ..."); } hostShares = GetNetShare.EnumNetShares(hostname); } catch (Exception e) { if (Config.Debug) { Console.WriteLine("[-][" + DateTime.Now.ToString() + "] Error on enumerating " + hostname + " shares (" + e.ToString() + ")."); } return(result); } List <SMBShareACL> sharesACL = ShareACLUtils.GetSharesACL(hostShares); // Iterate over target's shares foreach (SMBShareACL shareACL in sharesACL) { // Create SMBScanResult object for every shareInfo currentResult = new SMBScanResult { shareACL = shareACL, shareSubDirectories = new Dictionary <string, ScanDirectoryResult>() }; // if the shareInfo is not IPC$ or a printer, do a recursive scan on the subdirectories if (IsRecursivelyScannable(currentResult.shareACL.share)) { currentResult.shareSubDirectories = ScanShareDirectory(shareACL.share.ToString(), Config.RecursiveLevel).shareDirectorySubDirectories; } result.hostSharesScanResult.Add(shareACL.share.shareInfo.shi1_netname, currentResult); } return(result); }