Skip to content

Commit

Permalink
Adding compiler data to AppInsights (#404)
Browse files Browse the repository at this point in the history
* Adding compiler data to AppInsights

Adding improvements

* Adding basic constructor tests, adding parameters

* Adding flush method, adding placeholder for dialect

* Using envvariable to retrieve AppInsightsKey

* Simplifying tests

* Addressing PR feedback - 1

* Addressing PR feedback - 2

* Adressing PR feedback - 3 (making target relative)

* Adding CompilerData struct to prevent magic strings

* Removing data manipulation. let's use the raw data.

* Retrieving FileVersion

* Updating rules, adding more relevant information

* Deleting test

* removing redundant code
  • Loading branch information
eddynaka authored Jul 27, 2021
1 parent be2289a commit aa1f867
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 68 deletions.
33 changes: 32 additions & 1 deletion src/BinSkim.Driver/AnalyzeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ protected override BinaryAnalyzerContext CreateContext(AnalyzeOptions options, I
ShouldWarnVerbose = false;
}

if (binaryAnalyzerContext.Policy != null)
{
bool isRule4001Enabled = (binaryAnalyzerContext.Policy.TryGetValue("BA4001.ReportPECompilerData.Options", out object rule4001)
&& rule4001 is PropertiesDictionary property4001
&& property4001.TryGetValue("RuleEnabled", out object rule4001Value)
&& rule4001Value.ToString() == "Error");
bool isRule4002Enabled = (binaryAnalyzerContext.Policy.TryGetValue("BA4002.ReportDwarfCompilerData.Options", out object rule4002)
&& rule4002 is PropertiesDictionary property4002
&& property4002.TryGetValue("RuleEnabled", out object rule4002Value)
&& rule4002Value.ToString() == "Error");

if (isRule4001Enabled || isRule4002Enabled)
{
binaryAnalyzerContext.CompilerDataLogger = new CompilerDataLogger(binaryAnalyzerContext,
options.TargetFileSpecifiers);
}
}

return binaryAnalyzerContext;
}

Expand All @@ -66,7 +84,20 @@ public override int Run(AnalyzeOptions analyzeOptions)
analyzeOptions.Kind = new List<ResultKind> { ResultKind.Fail, ResultKind.NotApplicable, ResultKind.Pass };
}

int result = base.Run(analyzeOptions);
int result = 0;

try
{
result = base.Run(analyzeOptions);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
CompilerDataLogger.Flush();
}

// In BinSkim, no rule is ever applicable to every target type. For example,
// we have checks that are only relevant to either 32-bit or 64-bit binaries.
Expand Down
29 changes: 14 additions & 15 deletions src/BinSkim.Rules/DwarfRules/BA4002.ReportDwarfCompilerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void PrintCompilerData(BinaryAnalyzerContext context, string language, I
this.PrintHeader = false;
}

var processedRecords = new HashSet<string>();
var processedRecords = new HashSet<CompilerData>();

foreach (ICompiler compiler in compilers)
{
Expand All @@ -82,24 +82,23 @@ private void PrintCompilerData(BinaryAnalyzerContext context, string language, I

foreach (string file in files)
{
string currentRecord = compiler.Compiler + "," + compiler.Version + "," + language;

if (processedRecords.Contains(currentRecord))
var record = new CompilerData
{
BinaryType = "ELF",
Language = language,
CompilerName = compiler.Compiler.ToString(),
CompilerBackEndVersion = compiler.Version.ToString(),
CompilerFrontEndVersion = compiler.Version.ToString(),
};

if (processedRecords.Contains(record))
{
continue;
}

processedRecords.Add(currentRecord);

Console.Write($"{context.TargetUri.LocalPath},");
Console.Write($"{compiler.Compiler},");
Console.Write($"{compiler.Version},");
Console.Write($"{compiler.Version},");
Console.Write($"{language},");
Console.Write($"{file},");
Console.Write($"{file},");
Console.Write($"{context?.Hashes?.Sha256},");
Console.WriteLine();
processedRecords.Add(record);

context.CompilerDataLogger.Write(record, file);
}
}
}
Expand Down
59 changes: 32 additions & 27 deletions src/BinSkim.Rules/PERules/BA4001.ReportPECompilerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,34 @@ public override void AnalyzePortableExecutableAndPdb(BinaryAnalyzerContext conte

if (PrintHeader)
{
Console.WriteLine("Target,Compiler Name,Compiler BackEnd Version,Compiler FrontEnd Version,Language,Module Name,Module Library,Hash,Error");
context.CompilerDataLogger.PrintHeader();
PrintHeader = false;
}

if (pdb == null)
{
Console.Write(context.TargetUri.LocalPath + ",");
Console.WriteLine($",,,,,,{context?.Hashes?.Sha256},{target.PdbParseException.Message}");
string errorMessage = target.PdbParseException.Message;
context.CompilerDataLogger.WriteException(errorMessage);
return;
}

var records = new Dictionary<string, ObjectModuleDetails>();
var records = new Dictionary<CompilerData, ObjectModuleDetails>();

if (target.PE.IsManaged)
{
string record = $".NET Compiler,{target.PE.LinkerVersion},{target.PE.LinkerVersion},{Language.MSIL}";

if (!records.TryGetValue(record, out ObjectModuleDetails value))
var record = new CompilerData
{
BinaryType = "PE",
CompilerName = ".NET Compiler",
Language = nameof(Language.MSIL),
DebuggingFileName = pdb.GlobalScope?.Name,
DebuggingFileGuid = pdb.GlobalScope?.Guid.ToString(),
FileVersion = target.PE.FileVersion?.FileVersion,
CompilerBackEndVersion = target.PE.LinkerVersion.ToString(),
CompilerFrontEndVersion = target.PE.LinkerVersion.ToString(),
};

if (!records.ContainsKey(record))
{
records[record] = null;
}
Expand All @@ -77,33 +87,28 @@ public override void AnalyzePortableExecutableAndPdb(BinaryAnalyzerContext conte
Symbol om = omView.Value;
ObjectModuleDetails omDetails = om.GetObjectModuleDetails();

string record =
omDetails.CompilerName?.Replace(",", "_").Trim() + "," +
omDetails.CompilerBackEndVersion + "," +
omDetails.CompilerFrontEndVersion + "," +
omDetails.Language;

if (!records.TryGetValue(record, out ObjectModuleDetails value))
var record = new CompilerData
{
BinaryType = "PE",
CompilerName = omDetails.CompilerName,
Language = omDetails.Language.ToString(),
DebuggingFileName = pdb.GlobalScope?.Name,
FileVersion = target.PE.FileVersion?.FileVersion,
DebuggingFileGuid = pdb.GlobalScope?.Guid.ToString(),
CompilerBackEndVersion = omDetails.CompilerBackEndVersion.ToString(),
CompilerFrontEndVersion = omDetails.CompilerFrontEndVersion.ToString(),
};

if (!records.ContainsKey(record))
{
records[record] = omDetails;
}
}
}

foreach (KeyValuePair<string, ObjectModuleDetails> kv in records)
foreach (KeyValuePair<CompilerData, ObjectModuleDetails> kv in records)
{
string compilerData = kv.Key;
ObjectModuleDetails omDetails = kv.Value;

string name = omDetails.Name?.Replace(",", "_");
string library = omDetails.Library?.Replace(",", ";");

Console.Write($"{context.TargetUri.LocalPath},");
Console.Write($"{compilerData},");
Console.Write($"{name},");
Console.Write($"{(name == library ? string.Empty : library)},");
Console.Write($"{context?.Hashes?.Sha256},");
Console.WriteLine();
context.CompilerDataLogger.Write(kv.Key, kv.Value);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/BinSkim.Sdk/BinSkim.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.17.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/BinSkim.Sdk/BinaryAnalyzerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public Uri TargetUri
this.uri = value;
}
}

public bool TracePdbLoads { get; set; }

public string SymbolPath { get; set; }
Expand All @@ -77,6 +78,8 @@ public string MimeType
public bool AnalysisComplete { get; set; }
public DefaultTraces Traces { get; set; }

public CompilerDataLogger CompilerDataLogger { get; set; }

private bool disposed = false;

protected virtual void Dispose(bool disposing)
Expand Down
2 changes: 1 addition & 1 deletion src/BinSkim.Sdk/BinaryTargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.CodeAnalysis.IL.Sdk
{
internal class BinaryTargetManager
internal static class BinaryTargetManager
{
// We may want to consider changing this to an extension/plugin model rather than a hardcoded list of supported binary parsers.
// However, for now this will do.
Expand Down
22 changes: 22 additions & 0 deletions src/BinSkim.Sdk/CompilerData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.CodeAnalysis.IL.Sdk
{
public struct CompilerData
{
public string Language { get; set; }
public string BinaryType { get; set; }
public string FileVersion { get; set; }
public string CompilerName { get; set; }
public string DebuggingFileName { get; set; }
public string DebuggingFileGuid { get; set; }
public string CompilerBackEndVersion { get; set; }
public string CompilerFrontEndVersion { get; set; }

public override string ToString()
{
return $"{CompilerName},{CompilerBackEndVersion},{CompilerFrontEndVersion},{FileVersion},{BinaryType},{Language},{DebuggingFileName},{DebuggingFileGuid}";
}
}
}
Loading

0 comments on commit aa1f867

Please sign in to comment.