Skip to content

Commit

Permalink
Gfs/#326 (#330)
Browse files Browse the repository at this point in the history
* Don't throw from WalkDirectory.

* Fix #326.

* Fix #328

* Catch InvalidOperationException sometimes thrown by Mono. Fix #328.

* Improve telemetry opt-out message.

* Fix projects for VS for Mac

* Wait less time to flush.

* Add a ForceNoAdmin argument

Only for Collect command.

* Remove unused file.

* Remove unneeded tostring conversion.

* Fix group capturing on os x.
  • Loading branch information
gfs committed Dec 19, 2019
1 parent d347c03 commit 8357625
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 36 deletions.
1 change: 1 addition & 0 deletions Asa/Asa.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<OutputType>Exe</OutputType>
<ReleaseVersion>2.1-alpha</ReleaseVersion>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<!--<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
Expand Down
10 changes: 8 additions & 2 deletions Asa/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ public class CollectCommandOptions

[Option(HelpText = "Suppress all logging statements below WARN")]
public bool Quiet { set; get; }

[Option(HelpText = "Force run without admin/root (collectors may not function).")]
public bool ForceNoAdmin { set; get; }
}
[Verb("monitor", HelpText = "Continue running and monitor activity")]
public class MonitorCommandOptions
Expand Down Expand Up @@ -676,7 +679,7 @@ private static void CheckFirstRun()
{
if (DatabaseManager.FirstRun)
{
string exeStr = $"{System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Split('/').Last()} config --telemetry-opt-out true";
string exeStr = $"config --telemetry-opt-out true";
Log.Information(Strings.Get("ApplicationHasTelemetry"));
Log.Information(Strings.Get("ApplicationHasTelemetry2"), "https://github.com/Microsoft/AttackSurfaceAnalyzer/blob/master/PRIVACY.md");
Log.Information(Strings.Get("ApplicationHasTelemetry3"), exeStr);
Expand Down Expand Up @@ -1239,7 +1242,10 @@ public static int RunCollectCommand(CollectCommandOptions opts)
StartEvent.Add("Admin", AsaHelpers.IsAdmin().ToString(CultureInfo.InvariantCulture));
AsaTelemetry.TrackEvent("Run Command", StartEvent);

AdminOrQuit();
if (!opts.ForceNoAdmin)
{
AdminOrQuit();
}

CheckFirstRun();
DatabaseManager.VerifySchemaVersion();
Expand Down
8 changes: 2 additions & 6 deletions Lib/AsaLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageVersion>2.1-alpha</PackageVersion>
<Authors>Microsoft Corporation</Authors>
<Owners>Microsoft Corporation</Owners>
<LangVersion>8.0</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Attack Surface Analyzer is a Microsoft-developed open source security tool available at https://github.com/microsoft/AttackSurfaceAnalyzer that analyzes the attack surface of a target system and reports on potential security vulnerabilities introduced by the installation of software or by system misconfiguration.

Expand All @@ -17,11 +18,6 @@ This NuGet contains the AttackSurfaceAnalyzer Library, which is used by the CLI
<UpgradeBackupLocation>C:\Users\Gstoc\Documents\GitHub\AttackSurfaceAnalyzer\Backup\Lib\</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Utils\CommandOptions.cs" />
<Compile Remove="Collectors\OpenPorts\LinuxOpenPortUtils.cs" />
<Compile Remove="Collectors\OpenPorts\WindowsOpenPortUtils.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CompareNETObjects" Version="4.64.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
Expand All @@ -33,7 +29,7 @@ This NuGet contains the AttackSurfaceAnalyzer Library, which is used by the CLI
<PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="3.1.0" />
<PackageReference Include="murmurhash" Version="1.0.3" />
<PackageReference Include="PeNet" Version="1.2.0" />
<PackageReference Include="PeNet" Version="1.3.3" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
Expand Down
6 changes: 3 additions & 3 deletions Lib/Collectors/FileSystemCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public override void ExecuteInternal()

foreach (var root in roots)
{
Log.Information("{0} root {1}", Strings.Get("Scanning"), root.ToString(CultureInfo.InvariantCulture));
//Ensure the transaction is started to prevent collisions on the multithreaded code ahead
Log.Information("{0} root {1}", Strings.Get("Scanning"), root);
var fileInfoEnumerable = DirectoryWalker.WalkDirectory(root);
Parallel.ForEach(fileInfoEnumerable,
(fileInfo =>
Expand Down Expand Up @@ -282,7 +281,8 @@ public static FileSystemObject FileSystemInfoToFileSystemObject(FileSystemInfo f
}
catch (Exception e) when (
e is ArgumentNullException
|| e is ArgumentException)
|| e is ArgumentException
|| e is InvalidOperationException)
{
Log.Verbose($"Failed to get permissions for {fileInfo.FullName} {e.GetType().ToString()}");
}
Expand Down
16 changes: 12 additions & 4 deletions Lib/Collectors/FileSystemUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ public static string GetFilePermissions(FileSystemInfo fileInfo)
{
permissions = new UnixFileInfo(filename).FileAccessPermissions;
}
catch (IOException ex)
catch (IOException e)
{
Log.Debug("Unable to get access control for {0}: {1}", fileInfo.FullName, ex.Message);
Log.Debug("Unable to get access control for {0}: {1}", fileInfo.FullName, e.Message);
}
catch (InvalidOperationException e)
{
Log.Debug("Path probably doesn't exist: {0}", fileInfo.FullName);
}
}
else if (fileInfo is DirectoryInfo)
Expand All @@ -64,9 +68,13 @@ public static string GetFilePermissions(FileSystemInfo fileInfo)
{
permissions = new UnixDirectoryInfo(filename).FileAccessPermissions;
}
catch (IOException ex)
catch (IOException e)
{
Log.Debug("Unable to get access control for {0}: {1}", fileInfo.FullName, e.Message);
}
catch (InvalidOperationException e)
{
Log.Debug("Unable to get access control for {0}: {1}", fileInfo.FullName, ex.Message);
Log.Debug("Path probably doesn't exist: {0}", fileInfo.FullName);
}
}
else
Expand Down
18 changes: 13 additions & 5 deletions Lib/Collectors/UserAccountCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ namespace AttackSurfaceAnalyzer.Collectors
/// </summary>
public class UserAccountCollector : BaseCollector
{
Dictionary<string, UserAccountObject> users = new Dictionary<string, UserAccountObject>();
Dictionary<string, GroupAccountObject> groups = new Dictionary<string, GroupAccountObject>();

public UserAccountCollector(string runId)
{
this.RunId = runId;
Expand Down Expand Up @@ -53,6 +50,8 @@ public override void ExecuteInternal()
/// </summary>
public void ExecuteWindows()
{
Dictionary<string, UserAccountObject> users = new Dictionary<string, UserAccountObject>();
Dictionary<string, GroupAccountObject> groups = new Dictionary<string, GroupAccountObject>();
try
{
List<string> lines = new List<string>(ExternalCommandRunner.RunExternalCommand("net", "localgroup").Split('\n'));
Expand Down Expand Up @@ -363,9 +362,18 @@ private void ExecuteOsX()
foreach (var username in accountDetails.Keys)
{
// Admin user details
var groupsRaw = ExternalCommandRunner.RunExternalCommand("groups", "username");
string groupsRaw = string.Empty;

var groups = result.Split(' ');
try
{
groupsRaw = ExternalCommandRunner.RunExternalCommand("groups", username);
}
catch (Exception)
{

}

var groups = groupsRaw.Split(' ');
foreach (var group in groups)
{
accountDetails[username].Groups.Add(group);
Expand Down
3 changes: 2 additions & 1 deletion Lib/Collectors/WindowsFileSystemUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public static List<string> GetDllCharacteristics(string Path)
e is IndexOutOfRangeException
|| e is ArgumentNullException
|| e is System.IO.IOException
|| e is ArgumentException)
|| e is ArgumentException
|| e is UnauthorizedAccessException)
{
Log.Verbose($"Failed to get PE Headers for {Path} {e.GetType().ToString()}");
}
Expand Down
2 changes: 1 addition & 1 deletion Lib/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@
<value>For our privacy policy visit: {0}.</value>
</data>
<data name="ApplicationHasTelemetry3" xml:space="preserve">
<value>To disable telemetry run '{0}'.</value>
<value>To disable telemetry run Attack Surface Analyzer with these arguments: '{0}'.</value>
</data>
<data name="DumpingDataFromDatabase" xml:space="preserve">
<value>Dumping data from database located at {0}.</value>
Expand Down
10 changes: 0 additions & 10 deletions Lib/Utils/CommandOptions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Lib/Utils/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static void SleepAndFlushQueue()
while (!WriteQueue.IsEmpty) {
WriteNext();
}
Thread.Sleep(500);
Thread.Sleep(100);
}

public static PLATFORM RunIdToPlatform(string runid)
Expand Down
5 changes: 2 additions & 3 deletions Lib/Utils/DirectoryWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public static IEnumerable<FileSystemInfo> WalkDirectory(string root)
// examined for files.
Stack<string> dirs = new Stack<string>();

if (!System.IO.Directory.Exists(root))
if (System.IO.Directory.Exists(root))
{
throw new ArgumentException("Unable to find [" + root + "]");
dirs.Push(root);
}
dirs.Push(root);

while (dirs.Count > 0)
{
Expand Down

0 comments on commit 8357625

Please sign in to comment.