diff --git a/README.md b/README.md index 84a8dc3..f9975b7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # AnyExec2C -This is a simple program used wrap any executable into some source code. When the resulting source is compiled and run, it acts as a bootstrap stage. It unpacks the executable from within itself, dumps it to disk and runs it afterwards via Unix `exec` syscall. Historically, main target language has been C. As of now, also C# is supported as a target language. +This is a simple program used wrap any executable into some source code. When the resulting source is compiled and run, it acts as a bootstrap stage. It unpacks the executable from within itself, dumps it to disk and runs it afterwards via Unix `exec` syscall. Historically, main target language has been C. As of now, also C# and Python3 are supported as a target language. ## Purpose @@ -26,4 +26,6 @@ anyexec2c -b src/main.rs > source.c # (Rust using cargo - necessary to call fr # we can also generate C# programs for cases when C/C++ is not an allowed language (using --target or -t switch) # this packs our memory test tool into a C# environement anyexec2c -b tools/memtest.c -t C# > memtest.cs +# or a Python program :) +anyexec2c -b tools/memtest.c -t python > memtest.py ``` diff --git a/src/code/mod.rs b/src/code/mod.rs index 0778e65..64e7007 100644 --- a/src/code/mod.rs +++ b/src/code/mod.rs @@ -1,5 +1,6 @@ pub mod c_code; pub mod csharp_code; +pub mod python; pub trait CodeTemplate { fn render(executable_b64: String, payload_b64: Vec<(String, String)>, comment_files: Vec<(String, Vec)>) -> String; diff --git a/src/code/python.rs b/src/code/python.rs new file mode 100644 index 0000000..bef4c75 --- /dev/null +++ b/src/code/python.rs @@ -0,0 +1,17 @@ +use ::code::CodeTemplate; +use askama::Template; + +#[derive(Template)] +#[template(path = "python.py", escape = "none")] +pub struct PythonCodeTemplate { + comment_files: Vec<(String, Vec)>, + executable: String, + assets: Vec<(String, String)>, +} + +impl CodeTemplate for PythonCodeTemplate { + fn render(executable_b64: String, payload_b64: Vec<(String, String)>, comment_files: Vec<(String, Vec)>) -> String { + let template = PythonCodeTemplate {executable: executable_b64, assets: payload_b64, comment_files}; + template.render().unwrap() + } +} diff --git a/src/main.rs b/src/main.rs index 00093ef..47bec51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,8 @@ use code::CodeTemplate; enum OutputLanguage { C, CWithChecks, - CSharp + CSharp, + Python } @@ -78,6 +79,7 @@ fn parse_args() -> CmdArgs { "c" => OutputLanguage::C, "c_with_checks" => OutputLanguage::CWithChecks, "csharp" | "c#" => OutputLanguage::CSharp, + "python" | "py" => OutputLanguage::Python, _ => { eprintln!("Unsupported target type '{}'.", target); exit(1); @@ -187,6 +189,7 @@ fn main() { OutputLanguage::C => generate_source::<::code::c_code::CCodeTemplate>, OutputLanguage::CWithChecks => generate_source::<::code::c_code::CCodeWithChecksTemplate>, OutputLanguage::CSharp => generate_source::<::code::csharp_code::CSharpCodeTemplate>, + OutputLanguage::Python => generate_source::<::code::python::PythonCodeTemplate>, }(exec_file, args.asset_files, args.comment_files)); } diff --git a/templates/python.py b/templates/python.py new file mode 100644 index 0000000..998893b --- /dev/null +++ b/templates/python.py @@ -0,0 +1,42 @@ +# This source code was generated by anyexec2c. +# Link: https://github.com/exyi/anyexec2C + +{% for (filename, content) in comment_files %} +# ============================== +# {{ filename }} +# ============================== +# +{% for line in content %}# {{ line }} +{% endfor %} + + +{% endfor %} + +import base64 +import os +import sys + + +binaryName = "myBinaryPayload" + +def extract(payload, filename): + length = 0 + data = base64.b64decode(payload) + with open(filename, 'wb') as f: + f.write(data) + os.chmod(filename, 511) + +executable = "{{ executable }}" + +# extract the main binary +extract(executable, binaryName) + +# extract assets +{% for (name, asset) in assets %} +extract("{{ asset }}", "{{ name }}") +{% endfor %} + +args = list(sys.argv) +args[0] = binaryName +os.execv(binaryName, args) +exit(2)