GithubHelp home page GithubHelp logo

tyrrrz / ressy Goto Github PK

View Code? Open in Web Editor NEW
52.0 4.0 8.0 298 KB

Resource editor for PE files

License: MIT License

C# 100.00%
win32 native-resources portable-executable pe resources binary dotnet assembly parser dotnet-core

ressy's Introduction

Ressy

Status Made in Ukraine Build Coverage Version Downloads Discord Fuck Russia

Development of this project is entirely funded by the community. Consider donating to support!

Icon

Ressy is a library for managing native resources stored in portable executable images (i.e. EXE and DLL files). It offers a high-level abstraction model for working with the resource functions provided by the Windows API.

Terms of use[?]

By using this project or its source code, for any purpose and in any shape or form, you grant your implicit agreement to all the following statements:

  • You condemn Russia and its military aggression against Ukraine
  • You recognize that Russia is an occupant that unlawfully invaded a sovereign state
  • You support Ukraine's territorial integrity, including its claims over temporarily occupied territories of Crimea and Donbas
  • You reject false narratives perpetuated by Russian state propaganda

To learn more about the war and how you can help, click here. Glory to Ukraine! 🇺🇦

Install

  • 📦 NuGet: dotnet add package Ressy

Warning: This library relies on the Windows API and, as such, works only on Windows.

Usage

Ressy's functionality is provided entirely through the PortableExecutable class. You can create an instance of this class by passing a string that specifies the path to a PE file:

using Ressy;

var portableExecutable = new PortableExecutable("some_app.exe");

// ...

Reading resources

Enumerate resource identifiers

To get the list of resources in a PE file, use the GetResourceIdentifiers() method:

using Ressy;

var portableExecutable = new PortableExecutable("some_app.exe");

var identifiers = portableExecutable.GetResourceIdentifiers();

Returned list should contain something similiar to this:

- Type: 16 (RT_VERSION), Name: 1, Language: 1033
- Type: 24 (RT_MANIFEST), Name: 1, Language: 1033
- Type: 3 (RT_ICON), Name: 1, Language: 1033
- Type: 3 (RT_ICON), Name: 2, Language: 1033
- Type: 3 (RT_ICON), Name: 3, Language: 1033
- Type: 14 (RT_GROUP_ICON), Name: 2, Language: 1033
- Type: 4 (RT_MENU), Name: 1, Language: 1033
- Type: 5 (RT_DIALOG), Name: 1, Language: 1033
- Type: 5 (RT_DIALOG), Name: 2, Language: 1033
- Type: 5 (RT_DIALOG), Name: 3, Language: 1033
- Type: "MUI", Name: 1, Language: 1033
- ...

Retrieve resource data

To resolve a specific resource, call the GetResource(...) method. This returns an instance of the Resource class that contains the resource data:

using Ressy;

var portableExecutable = new PortableExecutable("some_app.exe");

var resource = portableExecutable.GetResource(new ResourceIdentifier(
    ResourceType.Manifest,
    ResourceName.FromCode(1),
    new Language(1033)
));

var resourceData = resource.Data; // byte[]
var resourceString = resource.ReadAsString(Encoding.UTF8); // string

If you aren't sure that the requested resource actually exists in the PE file, you can use the TryGetResource(...) method instead. It returns null in case the resource is missing:

using Ressy;

var portableExecutable = new PortableExecutable("some_app.exe");

var resource = portableExecutable.TryGetResource(new ResourceIdentifier(
    ResourceType.Manifest,
    ResourceName.FromCode(100),
    new Language(1033)
)); // resource is null

Modifying resources

Set resource data

To add or overwrite a resource, call the SetResource(...) method:

using Ressy;

var portableExecutable = new PortableExecutable("some_app.exe");

portableExecutable.SetResource(
    new ResourceIdentifier(
        ResourceType.Manifest,
        ResourceName.FromCode(1),
        new Language(1033)
    ),
    new byte[] { 0x01, 0x02, 0x03 }
);

Remove resources

To remove a resource, call the RemoveResource(...) method:

using Ressy;

var portableExecutable = new PortableExecutable("some_app.exe");

portableExecutable.RemoveResource(
    new ResourceIdentifier(
        ResourceType.Manifest,
        ResourceName.FromCode(1),
        new Language(1033)
    )
);

To remove all resources in a PE file, call the ClearResources() method:

using Ressy;

var portableExecutable = new PortableExecutable("some_app.exe");

portableExecutable.ClearResources();

High-level operations

Ressy provides extensions for PortableExecutable that enable you to directly read and manipulate known resource types, such as icons, manifests, versions, etc.

Manifest resources

A manifest resource (type 24) contains XML data that identifies and describes native assemblies that the application should bind to at run-time. It may also contain other information, such as application settings, requested execution level, and more.

To learn more about application manifests, see this article.

Retrieve the manifest

To read the manifest resource as an XML text string, call the GetManifest() extension method:

using Ressy;
using Ressy.HighLevel.Manifests;

var portableExecutable = new PortableExecutable("some_app.exe");

var manifest = portableExecutable.GetManifest();
// -or-
// var manifest = portableExecutable.TryGetManifest();

Note: If there are multiple manifest resources, this method retrieves the first one it finds, giving preference to resources with lower ordinal name (ID) and in the neutral language.

Set the manifest

To add or overwrite a manifest resource, call the SetManifest(...) extension method:

using Ressy;
using Ressy.HighLevel.Manifests;

var portableExecutable = new PortableExecutable("some_app.exe");

portableExecutable.SetManifest("<assembly>...</assembly>");
Remove the manifest

To remove all manifest resources, call the RemoveManifest() extension method:

using Ressy;
using Ressy.HighLevel.Manifests;

var portableExecutable = new PortableExecutable("some_app.exe");

portableExecutable.RemoveManifest();

Icon resources

Icon resources (type 3) and icon group resources (type 14) are used to visually identify an application within the operating system. Each portable executable file may contain multiple icon resources (usually in different sizes or color configurations), which are grouped together by the corresponding icon group resource.

Set the icon

To add or overwrite icon resources based on an ICO file, call the SetIcon(...) extension method:

using Ressy;
using Ressy.HighLevel.Icons;

var portableExecutable = new PortableExecutable("some_app.exe");

portableExecutable.SetIcon("new_icon.ico");

Warning: Calling this method does not remove the existing icon and icon group resources, except for those that are overwritten directly. If you want to clean out redundant icon resources, call the RemoveIcon() method first.

Additionally, you can also set the icon by passing a stream that contains ICO-formatted data:

using Ressy;
using Ressy.HighLevel.Icons;

var portableExecutable = new PortableExecutable("some_app.exe");

using var iconFileStream = File.OpenRead("new_icon.ico");
portableExecutable.SetIcon(iconFileStream);
Remove the icon

To remove all icon and icon group resources, call the RemoveIcon() extension method:

using Ressy;
using Ressy.HighLevel.Icons;

var portableExecutable = new PortableExecutable("some_app.exe");

portableExecutable.RemoveIcon();

Version info resources

A version info resource (type 16) contains file version numbers, compatibility flags, and arbitrary string attributes. Some of these attributes (such as, for example, ProductName and Copyright) are recognized by the operating system and may be displayed in certain places.

Retrieve version info

To get the version info resource, call the GetVersionInfo() extension method. This returns a VersionInfo object that represents the deserialized binary data stored in the resource:

using Ressy;
using Ressy.HighLevel.Versions;

var portableExecutable = new PortableExecutable("some_app.exe");

var versionInfo = portableExecutable.GetVersionInfo();
// -or-
// var versionInfo = portableExecutable.TryGetVersionInfo();

Returned object should contain data similar to this:

// Formatted as JSON in this example for better readability
{
  "FileVersion": "10.0.19041.1",
  "ProductVersion": "10.0.19041.1",
  "FileFlags": "None",
  "FileOperatingSystem": "Windows32, WindowsNT",
  "FileType": "Application",
  "FileSubType": "Unknown",
  "AttributeTables": [
    {
      "Language": {
        "Id": 1033
      },
      "CodePage": {
        "Id": 1200
      },
      "Attributes": {
        "CompanyName": "Microsoft Corporation",
        "FileDescription": "Notepad",
        "FileVersion": "10.0.19041.1 (WinBuild.160101.0800)",
        "InternalName": "Notepad",
        "LegalCopyright": "© Microsoft Corporation. All rights reserved.",
        "OriginalFilename": "NOTEPAD.EXE.MUI",
        "ProductName": "Microsoft® Windows® Operating System",
        "ProductVersion": "10.0.19041.1"
      }
    }
  ]
}

Note: If there are multiple version info resources, this method retrieves the first one it finds, giving preference to resources with lower ordinal name (ID) and in the neutral language.

When working with version info resources that include multiple attribute tables (bound to different language and code page pairs), you can use the GetAttribute(...) method to query a specific attribute. This method searches through all attribute tables (giving preference to tables in the neutral language) and returns the first matching value it finds:

// ...

var companyName = versionInfo.GetAttribute(VersionAttributeName.CompanyName); // Microsoft Corporation
// -or-
// var companyName = versionInfo.TryGetAttribute(VersionAttributeName.CompanyName);
Set version info

To add or overwrite a version info resource, call the SetVersionInfo(...) extension method. You can use the VersionInfoBuilder class to drastically simplify the creation of a new VersionInfo instance:

using Ressy;
using Ressy.HighLevel.Versions;

var portableExecutable = new PortableExecutable("some_app.exe");

var versionInfo = new VersionInfoBuilder()
    .SetFileVersion(new Version(1, 2, 3, 4))
    .SetProductVersion(new Version(1, 2, 3, 4))
    .SetFileType(FileType.Application)
    .SetAttribute(VersionAttributeName.FileDescription, "My new description")
    .SetAttribute(VersionAttributeName.CompanyName, "My new company")
    .SetAttribute("Custom Attribute", "My new value")
    .Build();

portableExecutable.SetVersionInfo(versionInfo);

You can also use an alternative overload of this method, which lets you selectively modify only a subset of properties in a version info resource, leaving the rest intact. Properties that are not provided are pulled from the existing version info resource or resolved to their default values in case the resource does not exist:

using Ressy;
using Ressy.HighLevel.Versions;

var portableExecutable = new PortableExecutable("some_app.exe");

portableExecutable.SetVersionInfo(v => v
    .SetFileVersion(new Version(1, 2, 3, 4))
    .SetAttribute("Custom Attribute", "My new value")
);

Note: When using the SetAttribute(...) method on VersionInfoBuilder, you can optionally specify the language and code page of the table that you want to add the attribute to. If you choose to omit these parameters, Ressy will set the attribute in all attribute tables. In case there are no existing attribute tables, this method creates a new one bound to the neutral language and the Unicode code page.

Remove version info

To remove all version info resources, call the RemoveVersionInfo() extension method:

using Ressy;
using Ressy.HighLevel.Versions;

var portableExecutable = new PortableExecutable("some_app.exe");

portableExecutable.RemoveVersionInfo();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.