Skip to content

Commit

Permalink
Merge branch 'develop' into pei-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Nov 8, 2019
2 parents 4a4ed5e + 6870728 commit e53d0e0
Show file tree
Hide file tree
Showing 22 changed files with 1,671 additions and 128 deletions.
8 changes: 5 additions & 3 deletions Src/Workshell.PE/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public static ulong ReadUInt64(byte[] bytes)
return BitConverter.ToUInt64(bytes,0);
}

/*
public static void Write(sbyte value, Stream stream)
{
var buffer = BitConverter.GetBytes(value);
Expand Down Expand Up @@ -153,6 +154,7 @@ public static void Write(ulong value, Stream stream)
Write(buffer, stream);
}
*/

public static void Write(byte[] bytes, Stream stream)
{
Expand All @@ -165,8 +167,8 @@ public static void Write<T>(T structure, byte[] buffer, int startIndex, int coun

try
{
Marshal.StructureToPtr(structure,ptr,false);
Marshal.Copy(ptr,buffer,startIndex,count);
Marshal.StructureToPtr(structure, ptr, false);
Marshal.Copy(ptr, buffer, startIndex, count);
}
finally
{
Expand All @@ -178,7 +180,7 @@ public static void Write<T>(T structure, Stream stream) where T : struct
{
var size = Utils.SizeOf<T>();

Write<T>(structure,stream,size);
Write<T>(structure, stream, size);
}

public static void Write<T>(T structure, Stream stream, int size) where T : struct
Expand Down
80 changes: 43 additions & 37 deletions dotNET PE.sln
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2042
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Workshell.PE", "src\Workshell.PE\Workshell.PE.csproj", "{2DF6E85A-7269-46C6-9032-1C3A8F029AE3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Workshell.PE.Resources", "src\Workshell.PE.Resources\Workshell.PE.Resources.csproj", "{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{85C1A49C-0316-4C2D-B2E3-17B1F8200EE7}"
ProjectSection(SolutionItems) = preProject
src\CommonAssemblyInfo.cs = src\CommonAssemblyInfo.cs
readme.md = readme.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2DF6E85A-7269-46C6-9032-1C3A8F029AE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2DF6E85A-7269-46C6-9032-1C3A8F029AE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DF6E85A-7269-46C6-9032-1C3A8F029AE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DF6E85A-7269-46C6-9032-1C3A8F029AE3}.Release|Any CPU.Build.0 = Release|Any CPU
{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D281E078-262D-4616-9157-CE1C42C0026C}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29503.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Workshell.PE", "src\Workshell.PE\Workshell.PE.csproj", "{2DF6E85A-7269-46C6-9032-1C3A8F029AE3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Workshell.PE.Resources", "src\Workshell.PE.Resources\Workshell.PE.Resources.csproj", "{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{85C1A49C-0316-4C2D-B2E3-17B1F8200EE7}"
ProjectSection(SolutionItems) = preProject
src\CommonAssemblyInfo.cs = src\CommonAssemblyInfo.cs
readme.md = readme.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Workshell.PE.Tests", "tests\Workshell.PE.Tests\Workshell.PE.Tests.csproj", "{8A5FC684-11D6-4902-B0E0-7FEBC36BA21F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2DF6E85A-7269-46C6-9032-1C3A8F029AE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2DF6E85A-7269-46C6-9032-1C3A8F029AE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DF6E85A-7269-46C6-9032-1C3A8F029AE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DF6E85A-7269-46C6-9032-1C3A8F029AE3}.Release|Any CPU.Build.0 = Release|Any CPU
{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DBED9C9E-0B8C-46B5-AE1D-865722B68E44}.Release|Any CPU.Build.0 = Release|Any CPU
{8A5FC684-11D6-4902-B0E0-7FEBC36BA21F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A5FC684-11D6-4902-B0E0-7FEBC36BA21F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A5FC684-11D6-4902-B0E0-7FEBC36BA21F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A5FC684-11D6-4902-B0E0-7FEBC36BA21F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D281E078-262D-4616-9157-CE1C42C0026C}
EndGlobalSection
EndGlobal
177 changes: 89 additions & 88 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,89 +1,90 @@
# Workshell PE Class Library

[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/Workshell/pe/blob/master/license.txt)
[![NuGet](https://img.shields.io/nuget/v/Workshell.PE.svg)](https://www.nuget.org/packages/Workshell.PE/)

This is a class library for reading the Portable Executable file format convering all the major data sections including:

* Core Headers (DOS, File, Optional)
* Section Table
* Debug Directory
* Relocations
* Imports
* Delayed Imports
* Exports
* Resources
* TLS
* Load Configuration
* Certificates
* .NET

For help getting started please see the wiki. Any suggestions for improvements and ideas welcome.


## Installation

Stable builds are available as NuGet packages. You can install it via the Package Manager or via the Package Manager Console:

```
Install-Package Workshell.PE
Install-Package Workshell.PE.Resources
```


## Basic Usage

You begin by calling `PortableExecutableImage.FromFileAsync(string)` to load the executable image, such as:

```
var image = await PortableExecutableImage.FromFileAsync("user32.dll");
```

You can then retireve information from the image. Some information is retrieved in a direct manor, like a lot of the core
header information, for example:

```
var imageBase = image.NTHeaders.OptionalHeader.ImageBase;
```

And some information is indirect via classes that work on the image. If you wanted all the exports for example:

```
var exports = await Exports.GetAsync(image);
```

You can then iterate each export:

```
foreach(var export in exports)
{
Console.WriteLine(export.Name);
}
```

At present there is no real documentation so the best option is to explore the source code and experiment!

If you have questions please do get in touch we're always happy to help.


## MIT License

Copyright (c) Workshell Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# Workshell PE Class Library

[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/Workshell/pe/blob/master/license.txt)
[![NuGet](https://img.shields.io/nuget/v/Workshell.PE.svg)](https://www.nuget.org/packages/Workshell.PE/)
[![Build Status](https://dev.azure.com/Workshell-DevOps/Workshell%20PE/_apis/build/status/Build%20Master?branchName=master)](https://dev.azure.com/Workshell-DevOps/Workshell%20PE/_build/latest?definitionId=2&branchName=master)

This is a class library for reading the Portable Executable file format convering all the major data sections including:

* Core Headers (DOS, File, Optional)
* Section Table
* Debug Directory
* Relocations
* Imports
* Delayed Imports
* Exports
* Resources
* TLS
* Load Configuration
* Certificates
* .NET

For help getting started please see the wiki. Any suggestions for improvements and ideas welcome.


## Installation

Stable builds are available as NuGet packages. You can install it via the Package Manager or via the Package Manager Console:

```
Install-Package Workshell.PE
Install-Package Workshell.PE.Resources
```


## Basic Usage

You begin by calling `PortableExecutableImage.FromFileAsync(string)` to load the executable image, such as:

```
var image = await PortableExecutableImage.FromFileAsync("user32.dll");
```

You can then retireve information from the image. Some information is retrieved in a direct manor, like a lot of the core
header information, for example:

```
var imageBase = image.NTHeaders.OptionalHeader.ImageBase;
```

And some information is indirect via classes that work on the image. If you wanted all the exports for example:

```
var exports = await Exports.GetAsync(image);
```

You can then iterate each export:

```
foreach(var export in exports)
{
Console.WriteLine(export.Name);
}
```

At present there is no real documentation so the best option is to explore the source code and experiment!

If you have questions please do get in touch we're always happy to help.


## MIT License

Copyright (c) Workshell Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions src/Workshell.PE/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

#if !SIGNED
[assembly: InternalsVisibleTo("Workshell.PE.Resources")]
[assembly: InternalsVisibleTo("Workshell.PE.Tests")]
#else
[assembly: InternalsVisibleTo("Workshell.PE.Resources, PublicKey=0024000004800000940000000602000000240000525341310004000001000100259ed23116da6a496f873182c31284a428d040b37885524e9b53049cd99d5cc84feb00dbe77278afda8ebc9def14111b20b561f8d958e3f4aea2d492fed946245c528b16cad6ee785995ccfd7e6b7b34fe4be452a651069b2c0bbcf668bfb1dd9b99a7f30ab10d289525d61e82fd45e1ebcc11fc3d286e6096a1ee7edeee6091")]
[assembly: InternalsVisibleTo("Workshell.PE.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100259ed23116da6a496f873182c31284a428d040b37885524e9b53049cd99d5cc84feb00dbe77278afda8ebc9def14111b20b561f8d958e3f4aea2d492fed946245c528b16cad6ee785995ccfd7e6b7b34fe4be452a651069b2c0bbcf668bfb1dd9b99a7f30ab10d289525d61e82fd45e1ebcc11fc3d286e6096a1ee7edeee6091")]
#endif

[assembly: AssemblyTitle("Workshell.PE")]
Expand Down
75 changes: 75 additions & 0 deletions tests/Workshell.PE.Tests/DOSHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Text;

using FluentAssertions;
using NUnit.Framework;

namespace Workshell.PE.Tests
{
[TestFixture]
public sealed class DOSHeaderTests
{
[Test]
public void DOSHeader_Is_Not_Null()
{
var file = TestingUtils.GetFileFromResources(TestingUtils.GetRandomTestFilename());

using (var image = PortableExecutableImage.FromStream(file))
{
image.DOSHeader.Should().NotBeNull();
}
}

[TestCase("clrtest.any.dll", 0x0090)]
[TestCase("clrtest.x86.dll", 0x0090)]
[TestCase("clrtest.x64.dll", 0x0090)]
[TestCase("nativetest.x86.dll", 0x0050)]
[TestCase("nativetest.x64.dll", 0x0050)]
public void Bytes_On_Last_Page_Of_File_Is_Correct(string fileName, int expectedValue)
{
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.DOSHeader.BytesOnLastPage;

value.Should().Be(Convert.ToUInt16(expectedValue));
}
}

[TestCase("clrtest.any.dll", 0x0040)]
[TestCase("clrtest.x86.dll", 0x0040)]
[TestCase("clrtest.x64.dll", 0x0040)]
[TestCase("nativetest.x86.dll", 0x0040)]
[TestCase("nativetest.x64.dll", 0x0040)]
public void File_Address_Relocation_Table_Is_Correct(string fileName, int expectedValue)
{
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.DOSHeader.FileAddressRelocationTable;

value.Should().Be(Convert.ToUInt16(expectedValue));
}
}

[TestCase("clrtest.any.dll", 0x00000080)]
[TestCase("clrtest.x86.dll", 0x00000080)]
[TestCase("clrtest.x64.dll", 0x00000080)]
[TestCase("nativetest.x86.dll", 0x00000100)]
[TestCase("nativetest.x64.dll", 0x00000100)]
public void File_Address_New_Header_Is_Correct(string fileName, int expectedValue)
{
var file = TestingUtils.GetFileFromResources(fileName);

using (var image = PortableExecutableImage.FromStream(file))
{
var value = image.DOSHeader.FileAddressNewHeader;

value.Should().Be(expectedValue);
}
}
}
}
Loading

0 comments on commit e53d0e0

Please sign in to comment.