-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added certificate table support and a flag indicating if the image is…
… signed or not.
- Loading branch information
Showing
7 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using Workshell.PE.Native; | ||
|
||
namespace Workshell.PE | ||
{ | ||
|
||
public enum CertificateType : ushort | ||
{ | ||
X509Certificate = 0x0001, | ||
PKCSSignedData = 0x0002, | ||
Reserved = 0x0003, | ||
PKCS1ModuleSign = 0x0009 | ||
} | ||
|
||
public sealed class Certificate : ISupportsLocation, ISupportsBytes | ||
{ | ||
|
||
private CertificateTableContent content; | ||
private Location location; | ||
private WIN_CERTIFICATE cert; | ||
|
||
internal Certificate(CertificateTableContent certContent, Location certLocation, WIN_CERTIFICATE winCert) | ||
{ | ||
content = certContent; | ||
location = certLocation; | ||
cert = winCert; | ||
} | ||
|
||
#region Methods | ||
|
||
public byte[] GetBytes() | ||
{ | ||
Stream stream = content.DataDirectory.Directories.Reader.GetStream(); | ||
byte[] buffer = Utils.ReadBytes(stream,location); | ||
|
||
return buffer; | ||
} | ||
|
||
public CertificateType GetCertificateType() | ||
{ | ||
return (CertificateType)cert.wCertificateType; | ||
} | ||
|
||
public byte[] GetCertificateData() | ||
{ | ||
Stream stream = content.DataDirectory.Directories.Reader.GetStream(); | ||
ulong offset = location.FileOffset + Convert.ToUInt32(Utils.SizeOf<WIN_CERTIFICATE>()); | ||
byte[] buffer = new byte[cert.dwLength]; | ||
|
||
stream.Seek(Convert.ToInt64(offset),SeekOrigin.Begin); | ||
stream.Read(buffer,0,buffer.Length); | ||
|
||
return buffer; | ||
} | ||
|
||
#endregion | ||
|
||
#region Properties | ||
|
||
public CertificateTableContent Content | ||
{ | ||
get | ||
{ | ||
return content; | ||
} | ||
} | ||
|
||
public Location Location | ||
{ | ||
get | ||
{ | ||
return location; | ||
} | ||
} | ||
|
||
public uint Length | ||
{ | ||
get | ||
{ | ||
return cert.dwLength; | ||
} | ||
} | ||
|
||
public ushort Revision | ||
{ | ||
get | ||
{ | ||
return cert.wRevision; | ||
} | ||
} | ||
|
||
public ushort CertificateType | ||
{ | ||
get | ||
{ | ||
return cert.wCertificateType; | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
Src/Workshell.PE/Content/Certificates/CertificateTableContent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using Workshell.PE.Native; | ||
|
||
namespace Workshell.PE | ||
{ | ||
|
||
public sealed class CertificateTableContent : DataDirectoryContent | ||
{ | ||
|
||
private Certificate cert; | ||
|
||
internal CertificateTableContent(DataDirectory dataDirectory, ulong imageBase) : base(dataDirectory, imageBase) | ||
{ | ||
LocationCalculator calc = dataDirectory.Directories.Reader.GetCalculator(); | ||
Stream stream = dataDirectory.Directories.Reader.GetStream(); | ||
|
||
Load(calc, stream, imageBase); | ||
} | ||
|
||
#region Methods | ||
|
||
private void Load(LocationCalculator calc, Stream stream, ulong imageBase) | ||
{ | ||
ulong offset = DataDirectory.VirtualAddress; | ||
Location location = new Location(offset, DataDirectory.VirtualAddress, imageBase + offset, DataDirectory.Size, DataDirectory.Size); | ||
|
||
stream.Seek(Convert.ToInt64(offset),SeekOrigin.Begin); | ||
|
||
WIN_CERTIFICATE win_cert = Utils.Read<WIN_CERTIFICATE>(stream); | ||
|
||
cert = new Certificate(this,location,win_cert); | ||
} | ||
|
||
#endregion | ||
|
||
#region Properties | ||
|
||
public Certificate Certificate | ||
{ | ||
get | ||
{ | ||
return cert; | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Workshell.PE.Native | ||
{ | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
public struct WIN_CERTIFICATE | ||
{ | ||
|
||
public uint dwLength; | ||
public ushort wRevision; | ||
public ushort wCertificateType; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters