Skip to content

Commit

Permalink
Loading Pdb if not found (#380)
Browse files Browse the repository at this point in the history
* parent 3a2493f
author Eddy Nakamura <ednakamu@microsoft.com> 1621901759 -0300
committer Eddy Nakamura <ednakamu@microsoft.com> 1624275129 -0300

Loading Pdb if not found

More improvements

Revert "More improvements"

This reverts commit 13893c8.

More improvements

Fixing nullReference exception

Refactoring pdb reader

Fixing build

Improving BA4001 for managed libraries

Renaming variables, fixing pdb bug

Using interpolation

Changing to .NET Compiler and updating summary

* Updating pdb reader logic

* Updating to x64 debug/release mode; adding tests

* Updating to use x64

* Updating scripts

* Improving build pipeline

* Updating nuspec and scripts

* Update ado-build.yml for Azure Pipelines

* Update ado-build.yml for Azure Pipelines

* Update ado-build.yml for Azure Pipelines

* Update ado-build.yml for Azure Pipelines

* Update ado-build.yml for Azure Pipelines

* Update ado-build.yml for Azure Pipelines

* Update ado-build.yml for Azure Pipelines

* Update ado-build.yml for Azure Pipelines

* Updating script

* Updating yml

* Updating to run on  windows only

* Updating sh script

* commenting tests for now
  • Loading branch information
eddynaka committed Jul 6, 2021
1 parent dffaa52 commit e7dd776
Show file tree
Hide file tree
Showing 25 changed files with 1,414 additions and 1,377 deletions.
2 changes: 1 addition & 1 deletion BuildAndTest.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SETLOCAL
@REM %~dp0.nuget\NuGet.exe update -self

set Configuration=%1
set Platform=AnyCPU
set Platform=x64

if "%Configuration%" EQU "" (
set Configuration=Release
Expand Down
10 changes: 5 additions & 5 deletions BuildAndTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ if [[ "$(uname)" == "Linux" ]]; then
sed 's#\\#/#g' src/BinSkim.sln > src/BinSkimLinux.sln
fi

dotnet build src/BinSkimLinux.sln
dotnet build src/BinSkimLinux.sln --configuration Release

dotnet test src/Test.FunctionalTests.BinSkim.Driver/Test.FunctionalTests.BinSkim.Driver.csproj --no-build
dotnet test src/Test.FunctionalTests.BinSkim.Rules/Test.FunctionalTests.BinSkim.Rules.csproj --no-build
dotnet test src/Test.UnitTests.BinaryParsers/Test.UnitTests.BinaryParsers.csproj --no-build
dotnet test src/Test.UnitTests.BinSkim.Rules/Test.UnitTests.BinSkim.Rules.csproj --no-build
dotnet test src/Test.FunctionalTests.BinSkim.Driver/Test.FunctionalTests.BinSkim.Driver.csproj --no-build --configuration Release
dotnet test src/Test.FunctionalTests.BinSkim.Rules/Test.FunctionalTests.BinSkim.Rules.csproj --no-build --configuration Release
dotnet test src/Test.UnitTests.BinaryParsers/Test.UnitTests.BinaryParsers.csproj --no-build --configuration Release
dotnet test src/Test.UnitTests.BinSkim.Rules/Test.UnitTests.BinSkim.Rules.csproj --no-build --configuration Release
2 changes: 1 addition & 1 deletion CreateLayoutDirectory.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set Configuration=Release
)

if "%Platform%" EQU "" (
set Platform=AnyCPU
set Platform=x64
)

set BinaryOutputDirectory=%BinaryOutputDirectory%\%Platform%_%Configuration%\Publish
Expand Down
2 changes: 1 addition & 1 deletion CreatePackagesFromLayoutDirectory.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set Configuration=Release
)

if "%Platform%" EQU "" (
set Platform=AnyCpu
set Platform=x64
)

set BinaryOutputDirectory=%BinaryOutputDirectory%\%Platform%_%Configuration%\Publish
Expand Down
13 changes: 10 additions & 3 deletions ado-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ jobs:
- checkout: self
submodules: true

- task: Bash@3
displayName: 'Build and Test'
- task: DotNetCoreCLI@2
name: 'Build'
inputs:
filePath: 'BuildAndTest.sh'
command: 'build'
projects: 'src/*.sln'
arguments: '--configuration Release'

# - task: Bash@3
# displayName: 'Build and Test'
# inputs:
# filePath: 'BuildAndTest.sh'

- job: windows
pool:
Expand Down
1 change: 1 addition & 0 deletions src/BinSkim.Driver/BinSkim.Driver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<TargetFramework>$(NetCoreVersion)</TargetFramework>
<TargetLatestRuntimePatch>True</TargetLatestRuntimePatch>
<OutputType>Exe</OutputType>
<Platforms>x64</Platforms>
</PropertyGroup>

<PropertyGroup>
Expand Down
263 changes: 133 additions & 130 deletions src/BinSkim.Driver/DumpCommand.cs
Original file line number Diff line number Diff line change
@@ -1,130 +1,133 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

using Microsoft.CodeAnalysis.BinaryParsers.PortableExecutable;
using Microsoft.CodeAnalysis.BinaryParsers.ProgramDatabase;
using Microsoft.CodeAnalysis.Sarif.Driver;

namespace Microsoft.CodeAnalysis.IL
{
internal class DumpCommand : DriverCommand<DumpOptions>
{
public override int Run(DumpOptions dumpOptions)
{
var targets = new List<string>();

foreach (string specifier in dumpOptions.BinaryFileSpecifiers)
{
if (Directory.Exists(specifier))
{
var fileSpecifier = new FileSpecifier(specifier + ".dll", recurse: dumpOptions.Recurse);
targets.AddRange(fileSpecifier.Files);

fileSpecifier = new FileSpecifier(specifier + ".exe", recurse: dumpOptions.Recurse);
targets.AddRange(fileSpecifier.Files);
}
else
{
var fileSpecifier = new FileSpecifier(specifier, recurse: dumpOptions.Recurse);
targets.AddRange(fileSpecifier.Files);
}
}

var dumpTask = Task.Run(() => Parallel.ForEach(targets, (target) => this.DumpFile(target, dumpOptions.Verbose)));
dumpTask.Wait();

return 0;
}

private const string Indent = "\t";
private const string Delimiter = ", ";

private void DumpFile(string target, bool verbose)
{
PE pe;
var sb = new StringBuilder();
try
{
pe = new PE(target);
}
catch (UnauthorizedAccessException)
{
Console.WriteLine(Path.GetFileName(target) + ": Unauthorized access exception");
return;
}

sb.AppendLine(Path.GetFileName(pe.FileName) + ":");

if (verbose)
{
sb.AppendLine(Indent + "Path: " + pe.FileName);
}

sb.Append(Indent + "Attr: ");

if (!pe.IsPEFile)
{
sb.AppendLine("Not a portable executable");
sb.AppendLine();
return;
}

string language = pe.IsManaged ? "Pure Managed" : "Native";
if (pe.IsManaged && !pe.IsILOnly) { language = "Mixed Managed"; }
sb.Append(language);

string machine = pe.Machine.ToString();
sb.Append(Delimiter + machine);

string subsystem = pe.Subsystem.ToString();
sb.Append(Delimiter + subsystem);

if (pe.IsKernelMode)
{
sb.Append(Delimiter + "Kernel Mode");
}

if (pe.IsResourceOnly)
{
sb.Append(Delimiter + "Resource Only");
}

sb.Append(Delimiter + "Link " + pe.LinkerVersion.ToString());

sb.AppendLine(); // Close comma-separated attributes line

sb.Append(Indent + "Pdb : ");
Pdb pdb = null;
try
{
pdb = new Pdb(pe.FileName);
}
catch (PdbException pdbParseException)
{
sb.AppendLine(pdbParseException.ExceptionDisplayMessage);
}

if (pdb != null)
{
if (verbose)
{
sb.AppendLine(pdb.PdbLocation);
}
else
{
sb.AppendLine(Path.GetFileName(pdb.PdbLocation));
}
}

sb.AppendLine(Indent + "SHA1: " + pe.SHA1Hash);

Console.Out.WriteLineAsync(sb.ToString());
}
}
}
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

using Microsoft.CodeAnalysis.BinaryParsers.PortableExecutable;
using Microsoft.CodeAnalysis.BinaryParsers.ProgramDatabase;
using Microsoft.CodeAnalysis.Sarif.Driver;

namespace Microsoft.CodeAnalysis.IL
{
internal class DumpCommand : DriverCommand<DumpOptions>
{
public override int Run(DumpOptions dumpOptions)
{
var targets = new List<string>();

foreach (string specifier in dumpOptions.BinaryFileSpecifiers)
{
if (Directory.Exists(specifier))
{
var fileSpecifier = new FileSpecifier(specifier + ".dll", recurse: dumpOptions.Recurse);
targets.AddRange(fileSpecifier.Files);

fileSpecifier = new FileSpecifier(specifier + ".exe", recurse: dumpOptions.Recurse);
targets.AddRange(fileSpecifier.Files);
}
else
{
var fileSpecifier = new FileSpecifier(specifier, recurse: dumpOptions.Recurse);
targets.AddRange(fileSpecifier.Files);
}
}

var dumpTask = Task.Run(() => Parallel.ForEach(targets, (target) => this.DumpFile(target, dumpOptions.Verbose)));
dumpTask.Wait();

return 0;
}

private const string Indent = "\t";
private const string Delimiter = ", ";

private void DumpFile(string target, bool verbose)
{
PE pe;
var sb = new StringBuilder();
try
{
pe = new PE(target);
}
catch (UnauthorizedAccessException)
{
Console.WriteLine(Path.GetFileName(target) + ": Unauthorized access exception");
return;
}

sb.AppendLine(Path.GetFileName(pe.FileName) + ":");

if (verbose)
{
sb.AppendLine(Indent + "Path: " + pe.FileName);
}

sb.Append(Indent + "Attr: ");

if (!pe.IsPEFile)
{
sb.AppendLine("Not a portable executable");
sb.AppendLine();
return;
}

string language = pe.IsManaged ? "Pure Managed" : "Native";
if (pe.IsManaged && !pe.IsILOnly) { language = "Mixed Managed"; }
sb.Append(language);

string machine = pe.Machine.ToString();
sb.Append(Delimiter + machine);

string subsystem = pe.Subsystem.ToString();
sb.Append(Delimiter + subsystem);

if (pe.IsKernelMode)
{
sb.Append(Delimiter + "Kernel Mode");
}

if (pe.IsResourceOnly)
{
sb.Append(Delimiter + "Resource Only");
}

sb.Append(Delimiter + "Link " + pe.LinkerVersion.ToString());

sb.AppendLine(); // Close comma-separated attributes line

sb.Append(Indent + "Pdb : ");
Pdb pdb = null;
try
{
string extension = Path.GetExtension(pe.FileName);
pdb = extension.Equals(".pdb", StringComparison.OrdinalIgnoreCase)
? new Pdb(pe.FileName, false)
: new Pdb(pe.FileName, null, null, false);
}
catch (PdbException pdbParseException)
{
sb.AppendLine(pdbParseException.ExceptionDisplayMessage);
}

if (pdb != null)
{
if (verbose)
{
sb.AppendLine(pdb.PdbLocation);
}
else
{
sb.AppendLine(Path.GetFileName(pdb.PdbLocation));
}
}

sb.AppendLine(Indent + "SHA1: " + pe.SHA1Hash);

Console.Out.WriteLineAsync(sb.ToString());
}
}
}
1 change: 1 addition & 0 deletions src/BinSkim.Rules/BinSkim.Rules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<PropertyGroup>
<RootNamespace>Microsoft.CodeAnalysis.IL.Rules</RootNamespace>
<TargetFramework>$(NetStandardVersion)</TargetFramework>
<Platforms>x64</Platforms>
</PropertyGroup>

<ItemGroup>
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 @@ -5,6 +5,7 @@
<PropertyGroup>
<RootNamespace>Microsoft.CodeAnalysis.IL.Sdk</RootNamespace>
<TargetFramework>$(NetStandardVersion)</TargetFramework>
<Platforms>x64</Platforms>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit e7dd776

Please sign in to comment.