• 首页
  • 渗透
  • 折腾
  • 转载
  • 关于Me
  • 榜上有名
  • 文章存档
  • 友情链接

不妨看看这里

会员面板

Base64 Image

python 执行php代码的小笔记

  • Mr.Wu
  • 2021-04-06
  • 0
在审计的时候遇到某个加密函数,看的我头晕眼花的,完全看不明白加密过程,但是我又需要写加密解密工具... 遇到这种情况就需要用到 python 的 subprocess 库了,他可以完美的调用 php 代码: php代码:
<?php

//$argv[1] 获取参数值
$string = $argv[1];
$operation = $argv[2];

function encrypt($string,$operation,$key='xyb8888'){ 
    $key=md5($key); 
    $key_length=strlen($key); 
      $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string; 
    $string_length=strlen($string); 
    $rndkey=$box=array(); 
    $result=''; 
    for($i=0;$i<=255;$i++){ 
           $rndkey[$i]=ord($key[$i%$key_length]); 
        $box[$i]=$i; 
    } 
    for($j=$i=0;$i<256;$i++){ 
        $j=($j+$box[$i]+$rndkey[$i])%256; 
        $tmp=$box[$i]; 
        $box[$i]=$box[$j]; 
        $box[$j]=$tmp; 
    } 
    for($a=$j=$i=0;$i<$string_length;$i++){ 
        $a=($a+1)%256; 
        $j=($j+$box[$a])%256; 
        $tmp=$box[$a]; 
        $box[$a]=$box[$j]; 
        $box[$j]=$tmp; 
        $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256])); 
    } 
    if($operation=='D'){ 
        if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)){ 
            return substr($result,8); 
        }else{ 
            return''; 
        } 
    }else{ 
        return str_replace('=','',base64_encode($result)); 
    } 
}

$e_keyid = encrypt($string,$operation);
$e_keyid = str_replace('/','AAABBB',$e_keyid);
echo $e_keyid;
?>
python代码:
import subprocess

#执行CMD命令
def run_cmd(cmd):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()
    if stdout:
        stdout = stdout.rstrip()
    if stderr:
        stderr = stderr.rstrip()
    return (stdout, stderr, p.returncode)

#加密解密函数
def encrypt(string,operation):
    a,b,c = run_cmd('php D:/Desktop/Python/systemlogined-php函数.php ' + string + " " + operation)
    return (a.decode('utf-8'))

print(encrypt('160794','E'))
需要本地安装了 PHP 环境,并且设置了环境变量,如果没设置的话就需要在 run_cmd('php) 的前面添加php绝对路径
© 2025 MrWu
Theme by Wing-child
  • {{ item.name }}
  • {{ item.name }}