Переписал функцию на питоне. Планирую использовать ее на google app engine. Пришлось несколько изменить алгоритм. deflate заменил на compress.
сама функция xor:
def xor_it(str,key):
code=''
y=0
for i in xrange(0, len(str)):
if y==len(key): y=0
code+=chr(ord(str[i:i+1])^ord(key[y:y+1]))
y+=1
return code
функция шифрования скрипта:
# -*- coding: utf-8 -*-
def add_armor(code,cookie_name,cookie_key):
def xor_it(str,key):
code=''
y=0
for i in xrange(0, len(str)):
if y==len(key): y=0
code+=chr(ord(str[i:i+1])^ord(key[y:y+1]))
y+=1
return code
def pack_it(code):
#return base64_encode(gzdeflate(code))
deflate=zlib.compress(code)
base64=deflate.encode('base64')
return base64
def add_protection(code,checksum,salt):
checksum=str(checksum)
return '/*'+salt+'*/$i=get_included_files();$s=$i[0];if(filesize($s)=='+checksum+' && sizeof($i)==1){'+code+'}else{unlink($s);die();}'
def add_body(base64_code,cookie_name):
return '<?php function x($s,$k){$c=\'\';for($i=0,$y=0;$i<strlen($s);$i++,$y++){if($y==strlen($k))$y=0;$c.=chr(ord(substr($s,$i,1))^ord(substr($k,$y,1)));}return $c;}if(isset($_COOKIE[\''+cookie_name+'\'])){$k=$_COOKIE[\''+cookie_name+'\'];$l=floor(strlen($k)/2);$k1=substr($k,0,$l);$k2=substr($k,$l);$s=gzuncompress(base64_decode(\''+base64_code+'\'));eval(x(x($s,$k1),$k2));}?>'
code_length=-1
compiled_code=''
i=0
salt=''
key_len=len(cookie_key)
key1_len=int(math.floor(key_len / 2))
cookie_key1=cookie_key[0:key1_len]
cookie_key2=cookie_key[key1_len:]
while code_length!=len(compiled_code):
if i>5:
salt+=chr(rand(0,255))
i=0
code_length=len(compiled_code)
compiled_code=add_protection(code,code_length,salt)
compiled_code=xor_it(compiled_code,cookie_key1)
compiled_code=xor_it(compiled_code,cookie_key2)
compiled_code=pack_it(compiled_code)
compiled_code=add_body(compiled_code,cookie_name)
i+=1
return compiled_code
сама функция xor:
def xor_it(str,key):
code=''
y=0
for i in xrange(0, len(str)):
if y==len(key): y=0
code+=chr(ord(str[i:i+1])^ord(key[y:y+1]))
y+=1
return code
функция шифрования скрипта:
# -*- coding: utf-8 -*-
def add_armor(code,cookie_name,cookie_key):
def xor_it(str,key):
code=''
y=0
for i in xrange(0, len(str)):
if y==len(key): y=0
code+=chr(ord(str[i:i+1])^ord(key[y:y+1]))
y+=1
return code
def pack_it(code):
#return base64_encode(gzdeflate(code))
deflate=zlib.compress(code)
base64=deflate.encode('base64')
return base64
def add_protection(code,checksum,salt):
checksum=str(checksum)
return '/*'+salt+'*/$i=get_included_files();$s=$i[0];if(filesize($s)=='+checksum+' && sizeof($i)==1){'+code+'}else{unlink($s);die();}'
def add_body(base64_code,cookie_name):
return '<?php function x($s,$k){$c=\'\';for($i=0,$y=0;$i<strlen($s);$i++,$y++){if($y==strlen($k))$y=0;$c.=chr(ord(substr($s,$i,1))^ord(substr($k,$y,1)));}return $c;}if(isset($_COOKIE[\''+cookie_name+'\'])){$k=$_COOKIE[\''+cookie_name+'\'];$l=floor(strlen($k)/2);$k1=substr($k,0,$l);$k2=substr($k,$l);$s=gzuncompress(base64_decode(\''+base64_code+'\'));eval(x(x($s,$k1),$k2));}?>'
code_length=-1
compiled_code=''
i=0
salt=''
key_len=len(cookie_key)
key1_len=int(math.floor(key_len / 2))
cookie_key1=cookie_key[0:key1_len]
cookie_key2=cookie_key[key1_len:]
while code_length!=len(compiled_code):
if i>5:
salt+=chr(rand(0,255))
i=0
code_length=len(compiled_code)
compiled_code=add_protection(code,code_length,salt)
compiled_code=xor_it(compiled_code,cookie_key1)
compiled_code=xor_it(compiled_code,cookie_key2)
compiled_code=pack_it(compiled_code)
compiled_code=add_body(compiled_code,cookie_name)
i+=1
return compiled_code
Кошмар конечно. Без форматирования
ОтветитьУдалить