Crates.io | rustclr |
lib.rs | rustclr |
version | 0.1.1 |
source | src |
created_at | 2024-11-01 15:12:06.605328 |
updated_at | 2024-11-01 19:34:59.289995 |
description | Host CLR and run .NET binaries using Rust |
homepage | https://github.com/joaoviictorti/rustclr |
repository | https://github.com/joaoviictorti/rustclr |
max_upload_size | |
id | 1431926 |
size | 205,770 |
rustclr
is a powerful library for hosting the Common Language Runtime (CLR) and executing .NET binaries directly with Rust, among other operations.
Add rustclr
to your project by updating your Cargo.toml
:
cargo add rustclr
Or manually add the dependency:
[dependencies]
rustclr = "<version>"
The following flags provide full control over your CLR environment and the execution of your .NET assemblies:
.with_runtime_version(RuntimeVersion::V4)
: Sets the .NET runtime version (e.g., RuntimeVersion::V2, RuntimeVersion::V3, RuntimeVersion::V4). This flag ensures that the assembly runs with the specified CLR version..with_output_redirection(true)
: Redirects the output from the .NET assembly's console to the Rust environment, capturing all console output..with_domain("DomainName")
: Sets a custom AppDomain name, which is useful for isolating different .NET assemblies..with_args(vec!["arg1", "arg2"])
: Passes arguments to the .NET application, useful for parameterized entry points in the assembly.Using rustclr
to load and execute a .NET assembly, redirect its output and customize the CLR runtime environment.
use std::fs;
use rustclr::{RustClr, RuntimeVersion};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load a sample .NET assembly into a buffer
let buffer = fs::read("examples/sample.exe")?;
// Create and configure a RustClr instance with runtime version and output redirection
let output = RustClr::new(&buffer)?
.with_runtime_version(RuntimeVersion::V4) // Specify .NET runtime version
.with_output_redirection(true) // Redirect output to capture it in Rust
.with_domain("CustomDomain") // Optionally set a custom application domain
.with_args(vec!["arg1", "arg2"]) // Pass arguments to the .NET assembly's entry point
.run()?; // Execute the assembly
println!("Captured output: {}", output);
Ok(())
}
For more fine-grained control, rustclr provides the RustClrEnv and ClrOutput components:
RustClrEnv
: Allows for low-level customization and initialization of the .NET runtime environment, which is useful if you need to manually control the CLR version, MetaHost, runtime information, and application domain. This struct provides an alternative way to initialize a CLR environment without executing an assembly immediately.use rustclr::{RustClrEnv, RuntimeVersion};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new environment for .NET with a specific runtime version
let clr_env = RustClrEnv::new(Some(RuntimeVersion::V4))?;
println!("CLR environment initialized successfully with version {:?}", clr_env.runtime_version);
Ok(())
}
ClrOutput
: Manages redirection of standard output and error streams from .NET to Rust. This is especially useful if you need to capture and process all output produced by .NET code within a Rust environment.use rustclr::{
RustClrEnv, ClrOutput,
InvocationType, Variant
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create and initialize the CLR environment
let clr = RustClrEnv::new(None)?;
let mscorlib = clr.app_domain.load_lib("mscorlib")?;
let console = mscorlib.resolve_type("System.Console")?;
// Set up output redirection
let mut clr_output = ClrOutput::new(&mscorlib);
clr_output.redirect()?;
// Prepare the arguments
let args = vec!["Hello World".to_variant()];
// Invoke the WriteLine method
console.invoke("WriteLine", None, Some(args), InvocationType::Static)?;
// Restore the original output and capture redirected content
clr_output.restore()?;
let output = clr_output.capture()?;
print!("{output}");
Ok(())
}
For more examples, check the examples folder in the repository.
rustclr
also includes a command-line interface (CLI) for running .NET assemblies with various configuration options. Below is a description of the available flags and usage examples.
The CLI accepts the following options:
-f, --file
: Specifies the path to the .NET assembly file to be executed (required).-i, --inputs
: Provides string arguments to be passed to the .NET program's entry point. This flag can be repeated to add multiple arguments.-r, --runtime-version
: Sets the .NET runtime version to use. Accepted values include "v2"
, "v3"
, and "v4"
. Defaults to "v4"
.-d, --domain
: Allows setting a custom name for the application domain (optional).clr.exe -f Rubeus.exe -i "triage" -i "/consoleoutfile:C:\Path" -r v4 -d "CustomDomain"
To contribute to rustclr, follow these steps:
git checkout -b <branch_name>
.git commit -m '<commit_message>'
.git push origin <branch_name>
.Alternatively, consult the GitHub documentation on how to create a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.