Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable analysis for managed binary #335

Merged
merged 11 commits into from
Jan 25, 2021

Conversation

eddynaka
Copy link
Contributor

No description provided.

@@ -43,7 +43,7 @@ private Pdb LoadPdb()
// We should never be required to load a PDB for a managed assembly that does
// not incorporate native code, as no managed-relevant rule currently crawls
// PDBs for its analysis.
Copy link
Member

@michaelcfanning michaelcfanning Jan 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete all this eventually. :) #Closed

@michaelcfanning
Copy link
Member

michaelcfanning commented Jan 18, 2021

    public string PdbLocation => this.session.globalScope.symbolsFileName;

consider updating this. #Closed


Refers to: src/BinaryParsers/PEBinary/ProgramDatabase/Pdb.cs:399 in 2fb9917. [](commit_id = 2fb9917, deletion_comment = False)

@eddynaka
Copy link
Contributor Author

    public string PdbLocation => this.session.globalScope.symbolsFileName;

updated the logic...


In reply to: 762434522 [](ancestors = 762434522)


Refers to: src/BinaryParsers/PEBinary/ProgramDatabase/Pdb.cs:399 in 2fb9917. [](commit_id = 2fb9917, deletion_comment = False)

@@ -5,6 +5,12 @@
<RootNamespace>Microsoft.CodeAnalysis.BinaryParsers</RootNamespace>
<TargetFramework>$(NetStandardVersion)</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Copy link
Member

@michaelcfanning michaelcfanning Jan 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can collapse this into a single property group if you remove the condition attribute #Closed

}
}

return false;
Copy link
Member

@michaelcfanning michaelcfanning Jan 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return false [](start = 12, length = 12)

Always prefer early exit if possible. So, change the code to say if (!TryOpen) then return false immediately. #Closed

public bool IsChecksumAlgorithmSecureForFullPdb()
{
const string sha256 = "8829d00f-11b8-4213-878b-770e8597ac16";
var sha256guid = new Guid(sha256);
Copy link
Member

@michaelcfanning michaelcfanning Jan 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare a private static readonly Guid that's initialized to this. #Closed

const string sha256 = "8829d00f-11b8-4213-878b-770e8597ac16";
var sha256guid = new Guid(sha256);

if (this.peReader.TryOpenAssociatedPortablePdb(
Copy link
Member

@michaelcfanning michaelcfanning Jan 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryOpenAssociatedPortablePdb [](start = 30, length = 28)

eventually pdb location logic needs to understand binskim's '--local-symbol-directories' command-line arguments. let's open an issue and take this in a future change. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue opened: #338


In reply to: 563213769 [](ancestors = 563213769)

@@ -789,5 +789,66 @@ public bool IsWixBinary
return this.isWixBinary.Value;
}
}

public bool IsChecksumAlgorithmSecureForPortablePdb()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsChecksumAlgorithmSecureForPortablePdb [](start = 20, length = 39)

rather than creating these helpers, you should add a helper that simply returns the checksum algorithm for managed PDBs.

ManagedPdbSourceFileChecksumAlgorithm { get; }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property s/be on the PDB class, not in the PE class.


In reply to: 563213952 [](ancestors = 563213952)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh...remembered why i didn't move the below logic to pdb.cs: to do th reading I need a few things: peReader and the metadatareader.


In reply to: 563214137 [](ancestors = 563214137,563213952)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed a little bit, let me know what do u think.


In reply to: 563297494 [](ancestors = 563297494,563214137,563213952)

@@ -134,6 +143,42 @@ private void WindowsNativeLoadPdbUsingDia(string pePath, string symbolPath, stri

public bool IsStripped => this.GlobalScope.IsStripped;

public PdbFileType FileType
Copy link
Member

@michaelcfanning michaelcfanning Jan 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should cache this property value rather than computing it every time. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are caching unless it's unknown


In reply to: 563214200 [](ancestors = 563214200)

get
{
string path = this.session.globalScope.symbolsFileName;
if (File.Exists(path))
Copy link
Member

@michaelcfanning michaelcfanning Jan 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this if (File.Exists(path) || !Directory.Exists(path)) { return path;}

and you will simplify the remaining code. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestion!


In reply to: 563214413 [](ancestors = 563214413)

// if (portableExecutable.IsResourceOnly) { return result; }
if (target.PE.IsManaged && di == null)
{
reasonForNotAnalyzing = MetadataConditions.CouldNotLoadPdb;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reasonForNotAnalyzing = MetadataConditions.CouldNotLoadPdb [](start = 16, length = 58)

in other checks, the absence of a PDB results in an error level notification, not a 'not applicable' message. 'Not applicable' is not the right return value here, as this return value means 'the binary isn't a valid scan target'. that's not true here, managed code is a valid scan target, the problem is that we can't analyze it due to a missing pdb. you should go look and see how the native pdb reading errors handle a missing pdb.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you validate again? pushed new changes. Thank you


In reply to: 563214549 [](ancestors = 563214549)

@@ -62,7 +62,7 @@
"arguments": [
"Binskim.win-x86.RTR.dll",
"EnableSecureSourceCodeHashing",
"image is a managed IL library (i.e., ahead of time compiled) assembly"
"an exception occurred attempting to load its pdb"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"an exception occurred attempting to load its pdb" [](start = 14, length = 50)

this is wrong. a missing PDB is an error (because analysis that could have occurred did not). 'not applicable' means, 'we didn't look at this because the analysis is not applicable to this scan target'

Copy link
Member

@michaelcfanning michaelcfanning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕐

@eddynaka eddynaka marked this pull request as ready for review January 24, 2021 23:59
Copy link
Member

@michaelcfanning michaelcfanning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@michaelcfanning michaelcfanning merged commit a7816c7 into main Jan 25, 2021
@michaelcfanning michaelcfanning deleted the users/ednakamu/ba2004-managed-improvement branch January 25, 2021 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants