Skip to content

Commit

Permalink
added Python target
Browse files Browse the repository at this point in the history
  • Loading branch information
vakabus committed Oct 29, 2020
1 parent 4a3faa4 commit 621af9c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
```
1 change: 1 addition & 0 deletions src/code/mod.rs
Original file line number Diff line number Diff line change
@@ -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>)>) -> String;
Expand Down
17 changes: 17 additions & 0 deletions src/code/python.rs
Original file line number Diff line number Diff line change
@@ -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<String>)>,
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>)>) -> String {
let template = PythonCodeTemplate {executable: executable_b64, assets: payload_b64, comment_files};
template.render().unwrap()
}
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use code::CodeTemplate;
enum OutputLanguage {
C,
CWithChecks,
CSharp
CSharp,
Python
}


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand Down
42 changes: 42 additions & 0 deletions templates/python.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 621af9c

Please sign in to comment.