Updated utilities

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-01 01:17:32 +01:00
parent 8a26a4d858
commit c71801145e
6 changed files with 3804 additions and 262 deletions

3553
classes/BigInteger.class.php Normal file

File diff suppressed because it is too large Load Diff

View File

@ -49,6 +49,75 @@ class CustomPacketHandler{
$this->offset = 0;
$this->c = (bool) $create;
switch($pid){
case 0x09:
if($this->c === false){
$this->data["clientID"] = $this->get(8);
$this->data["unknown1"] = $this->get(4);
$this->data["session"] = $this->get(2);
$this->data["unknown2"] = $this->get(2);
}else{
$this->raw .= $this->data["clientID"];
$this->raw .= "\x00\x00\x00\x00\x00";
$this->raw .= $this->data["session"];
$this->raw .= "\x19\x00";
}
break;
case 0x10:
if($this->c === false){
$this->data["cookie"] = $this->get(4); // 043f57ff
$this->data["unknown1"] = $this->get(1);
$this->data["port"] = Utils::readShort($this->get(2), false);
$this->data["dataArray"] = Utils::readDataArray($this->get(true, false), 10, $offset);
$this->get($offset);
$this->data["unknown2"] = $this->get(8);
$this->data["unknown3"] = $this->get(2);
$this->data["unknown4"] = $this->get(5);
$this->data["session"] = $this->get(2);
}else{
$this->raw .= "\x04\x3f\x57\xff";
$this->raw .= "\x3f";
$this->raw .= Utils::writeShort($this->data["port"]);
$this->raw .= Utils::writeDataArray(array(
"\x80\xff\xff\xfe",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
));
$this->raw .= "\x00\x00\x00\x00\x00\x00\x00\x00";
$this->raw .= "\x0c\x98";
$this->raw .= "\x00\x00\x00\x00\x00\x00\x00\x00";
$this->raw .= $this->data["session"];
}
break;
case 0x13:
if($this->c === false){
$this->data["cookie"] = $this->get(4); // 043f57ff
$this->data["unknown1"] = $this->get(1);
$this->data["port"] = Utils::readShort($this->get(2), false);
$this->data["dataArray0"] = $this->get(ord($this->get(1)));
$this->data["dataArray"] = Utils::readDataArray($this->get(true, false), 9, $offset);
$this->get($offset);
$this->data["unknown2"] = $this->get(13);
}else{
$this->raw .= "\x04\x3f\x57\xff";
$this->raw .= "\x3e";
$this->raw .= Utils::writeShort($this->data["port"]);
$w = array_shift($this->data["dataArray"]);
$this->raw .= chr(strlen($w)).$w;
$this->raw .= Utils::writeDataArray($this->data["dataArray"]);
$this->raw .= "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
$this->raw .= $this->data["session"]."\x5e";
}
break;
case 0x15:
//null
break;
case 0x82:
if($this->c === false){
$this->data["username"] = $this->get(Utils::readShort($this->get(2), false));
@ -106,73 +175,6 @@ class CustomPacketHandler{
$this->raw .= Utils::writeShort(strlen($this->data["message"])).$this->data["message"];
}
break;
case 0x09:
if($this->c === false){
$this->data["clientID"] = $this->get(8);
$this->data["unknown1"] = $this->get(1);
$this->data["unknown2"] = $this->get(4);
$this->data["session"] = $this->get(4);
}else{
$this->raw .= $this->data["clientID"];
$this->raw .= "\x00";
$this->raw .= "\x00\x00\x00\x00";
$this->raw .= $this->data["session"];
}
break;
case 0x10:
if($this->c === false){
$this->data["cookie"] = $this->get(4); // 043f57ff
$this->data["unknown1"] = $this->get(1);
$this->data["port"] = Utils::readShort($this->get(2), false);
$this->data["dataArray"] = Utils::readDataArray($this->get(true, false), 10, $offset);
$this->get($offset);
$this->data["unknown2"] = $this->get(7);
$this->data["session"] = $this->get(4);
$this->data["unknown3"] = $this->get(7);
}else{
$this->raw .= "\x04\x3f\x57\xff";
$this->raw .= "\x00";
$this->raw .= Utils::writeShort($this->data["port"]);
$this->raw .= Utils::writeDataArray(array(
"\x80\xff\xff\xfe",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
"\xff\xff\xff\xff",
));
$this->raw .= "\x00\x00\x00\x00\x00\x00\x00";
$this->raw .= $this->data["session"];
$this->raw .= "\x00\x00\x00\x00\x00\x00\x00";
}
break;
case 0x13:
if($this->c === false){
$this->data["cookie"] = $this->get(4); // 043f57ff
$this->data["unknown1"] = $this->get(1);
$this->data["port"] = Utils::readShort($this->get(2), false);
$this->data["dataArray0"] = $this->get(ord($this->get(1)));
$this->data["dataArray"] = Utils::readDataArray($this->get(true, false), 9, $offset);
$this->get($offset);
$this->data["unknown2"] = $this->get(13);
}else{
$this->raw .= "\x04\x3f\x57\xff";
$this->raw .= "\x3e";
$this->raw .= Utils::writeShort($this->data["port"]);
$w = array_shift($this->data["dataArray"]);
$this->raw .= chr(strlen($w)).$w;
$this->raw .= Utils::writeDataArray($this->data["dataArray"]);
$this->raw .= "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
$this->raw .= "\x00\xae\x21\x4e";
}
break;
case 0x15:
//null
break;
case 0x00:
if($this->c === false){
$this->data["payload"] = $this->get(8);

View File

@ -35,7 +35,7 @@ class PocketMinecraftClient{
$this->username = $username;
$this->connected = false;
$this->cnt = 1;
$this->clientID = substr(Utils::generateKey(), 0, 8);
$this->clientID = Utils::getRandomBytes(8);
$this->events = array("disabled" => array());
$this->actions = array();
$this->interface = new MinecraftInterface("255.255.255.255", $protocol, 19132);
@ -118,7 +118,7 @@ class PocketMinecraftClient{
array(
"id" => 0x09,
"clientID" => $this->clientID,
"session" => "\x00\x0c\x98\x00",
"session" => Utils::getRandomBytes(2),
),
));
++$this->counter[0];
@ -141,6 +141,15 @@ class PocketMinecraftClient{
),
));
++$this->counter[0];
$this->send(0x84, array(
$this->counter[0],
0x00,
array(
"id" => 0x00,
"payload" => "\x00\x00\x00\x00\x00".$data["session"]."\x5e",
),
));
++$this->counter[0];
$this->send(0x84, array(
$this->counter[0],
0x00,

View File

@ -35,7 +35,7 @@ class PocketMinecraftServer{
$this->version = (int) $version;
$this->username = $username;
$this->cnt = 1;
$this->serverID = substr(Utils::generateKey(), 0, 8);
$this->serverID = Utils::getRandomBytes(8);
$this->seed = "\x4f\xf0\x2d\x84";
$this->events = array("disabled" => array());
$this->actions = array();

View File

@ -26,118 +26,33 @@ the Free Software Foundation, either version 3 of the License, or
*/
if(!defined("GMPEXT")){
@define("GMPEXT", false);
}
if(!defined("HEX2BIN")){
@define("HEX2BIN", false);
}
define("BIG_ENDIAN", 0x00);
define("LITTLE_ENDIAN", 0x01);
define("ENDIANNESS", (pack('d', 1) === "\77\360\0\0\0\0\0\0" ? BIG_ENDIAN:LITTLE_ENDIAN));
define("ENDIANNESS", (pack("d", 1) === "\77\360\0\0\0\0\0\0" ? BIG_ENDIAN:LITTLE_ENDIAN));
console("[DEBUG] Endianness: ".(ENDIANNESS === LITTLE_ENDIAN ? "Little Endian":"Big Endian"), true, true, 2);
class Utils{
public static $hexToBin = array(
"0" => "0000",
"1" => "0001",
"2" => "0010",
"3" => "0011",
"4" => "0100",
"5" => "0101",
"6" => "0110",
"7" => "0111",
"8" => "1000",
"9" => "1001",
"a" => "1010",
"b" => "1011",
"c" => "1100",
"d" => "1101",
"e" => "1110",
"f" => "1111",
);
public static function round($number){
return round($number, 0, PHP_ROUND_HALF_DOWN);
public static function getOS(){
$uname = strtoupper(php_uname("s"));
if(strpos($uname, "WIN") !== false){
return "win";
}else{
return "linux";
}
}
public static function generateKey($startEntropy = ""){
//not much entropy, but works ^^
$entropy = array(
implode(stat(__FILE__)),
lcg_value(),
print_r($_SERVER, true),
implode(mt_rand(0,394),get_defined_constants()),
get_current_user(),
print_r(ini_get_all(),true),
(string) memory_get_usage(),
php_uname(),
phpversion(),
zend_version(),
getmypid(),
mt_rand(),
rand(),
implode(get_loaded_extensions()),
sys_get_temp_dir(),
disk_free_space("."),
disk_total_space("."),
(function_exists("openssl_random_pseudo_bytes") and version_compare(PHP_VERSION, "5.3.4", ">=")) ? openssl_random_pseudo_bytes(16):microtime(true),
function_exists("mcrypt_create_iv") ? mcrypt_create_iv(16, MCRYPT_DEV_URANDOM):microtime(true),
uniqid(microtime(true),true),
file_exists("/dev/urandom") ? fread(fopen("/dev/urandom", "rb"),16):microtime(true),
);
shuffle($entropy);
$value = Utils::hexToStr(md5((string) $startEntropy));
unset($startEntropy);
foreach($entropy as $c){
$c = (string) $c;
for($i = 0; $i < 4; ++$i){
$value ^= md5($i . $c . microtime(true), true);
$value ^= substr(sha1($i . $c . microtime(true), true), $i,16);
}
}
return $value;
}
public static function sha1($input){
$binary = Utils::hexToBin(sha1($input));
$negative = false;
$len = strlen($binary);
if($binary{0} === "1"){
$negative = true;
for($i = 0; $i < $len; ++$i){
$binary{$i} = $binary{$i} === "1" ? "0":"1";
}
for($i = $len - 1; $i >= 0; --$i){
if($binary{$i} === "1"){
$binary{$i} = "0";
}else{
$binary{$i} = "1";
break;
}
}
}
$hash = Utils::binToHex($binary);
$len = strlen($hash);
for($i = 0; $i < $len; ++$i){
if($hash{$i} === "0"){
$hash{$i} = "x";
}else{
break;
}
}
return ($negative === true ? "-":"").str_replace("x", "", $hash);
}
public static function hexdump($bin){
$output = "";
$bin = str_split($bin, 16);
foreach($bin as $counter => $line){
$hex = chunk_split(chunk_split(str_pad(bin2hex($line), 32, " ", STR_PAD_RIGHT), 2, " "), 24, " ");
$ascii = preg_replace('#([^\x20-\x7E])#', '.', $line);
$ascii = preg_replace('#([^\x20-\x7E])#', ".", $line);
$output .= str_pad(dechex($counter << 4), 4, "0", STR_PAD_LEFT). " " . $hex . " " . $ascii . PHP_EOL;
}
return $output;
@ -147,13 +62,152 @@ class Utils{
return preg_replace('#([^\x20-\x7E])#', '.', $str);
}
public static function readTriad($str){
list(,$unpacked) = unpack("N", "\x00".$str);
return (int) $unpacked;
}
public static function writeTriad($value){
return substr(pack("N", $value), 1);
}
public static function readDataArray($str, $len = 10, &$offset = null){
$data = array();
$offset = 0;
for($i = 1; $i <= $len; ++$i){
$l = Utils::readTriad(substr($str, $offset, 3));
$offset += 3;
$data[] = substr($str, $offset, $l);
$offset += $l;
}
return $data;
}
public static function writeDataArray($data){
$raw = "";
foreach($data as $v){
$raw .= Utils::writeTriad(strlen($v));
$raw .= $v;
}
return $raw;
}
public static function getRandomBytes($length = 16, $secure = true, $raw = true, $startEntropy = "", &$rounds = 0, &$drop = 0){
$output = b"";
$length = abs((int) $length);
$secureValue = "";
$rounds = 0;
$drop = 0;
while(!isset($output{$length - 1})){
//some entropy, but works ^^
$weakEntropy = array(
is_array($startEntropy) ? implode($startEntropy):$startEntropy,
serialize(stat(__FILE__)),
__DIR__,
PHP_OS,
microtime(),
(string) lcg_value(),
serialize($_SERVER),
serialize(get_defined_constants()),
get_current_user(),
serialize(ini_get_all()),
(string) memory_get_usage(),
php_uname(),
phpversion(),
extension_loaded("gmp") ? gmp_strval(gmp_random(4)):microtime(),
zend_version(),
(string) getmypid(),
(string) mt_rand(),
(string) rand(),
function_exists("zend_thread_id") ? ((string) zend_thread_id()):microtime(),
var_export(@get_browser(), true),
function_exists("sys_getloadavg") ? implode(";", sys_getloadavg()):microtime(),
serialize(get_loaded_extensions()),
sys_get_temp_dir(),
(string) disk_free_space("."),
(string) disk_total_space("."),
uniqid(microtime(),true),
);
shuffle($weakEntropy);
$value = hash("sha256", implode($weakEntropy), true);
foreach($weakEntropy as $k => $c){ //mixing entropy values with XOR and hash randomness extractor
$c = (string) $c;
str_shuffle($c); //randomize characters
$value ^= hash("md5", $c . microtime() . $k, true) . hash("md5", microtime() . $k . $c, true);
$value ^= hash("sha256", $c . microtime() . $k, true);
}
unset($weakEntropy);
if($secure === true){
$strongEntropy = array(
is_array($startEntropy) ? $startEntropy[($rounds + $drop) % count($startEntropy)]:$startEntropy, //Get a random index of the startEntropy, or just read it
file_exists("/dev/urandom") ? fread(fopen("/dev/urandom", "rb"), 512):"",
(function_exists("openssl_random_pseudo_bytes") and version_compare(PHP_VERSION, "5.3.4", ">=")) ? openssl_random_pseudo_bytes(512):"",
function_exists("mcrypt_create_iv") ? mcrypt_create_iv(512, MCRYPT_DEV_URANDOM) : "",
$value,
);
shuffle($strongEntropy);
$strongEntropy = implode($strongEntropy);
$value = "";
//Von Neumann randomness extractor, increases entropy
$len = strlen($strongEntropy) * 8;
for($i = 0; $i < $len; $i += 2){
$a = ord($strongEntropy{$i >> 3});
$b = 1 << ($i % 8);
$c = 1 << (($i % 8) + 1);
$b = ($a & $b) === $b ? "1":"0";
$c = ($a & $c) === $c ? "1":"0";
if($b !== $c){
$secureValue .= $b;
if(isset($secureValue{7})){
$value .= chr(bindec($secureValue));
$secureValue = "";
}
++$drop;
}else{
$drop += 2;
}
}
}
$output .= substr($value, 0, min($length - strlen($output), $length));
unset($value);
++$rounds;
}
return $raw === false ? bin2hex($output):$output;
}
public static function round($number){
return round($number, 0, PHP_ROUND_HALF_DOWN);
}
public static function distance($pos1, $pos2){
return sqrt(pow($pos1["x"] - $pos2["x"], 2) + pow($pos1["y"] - $pos2["y"], 2) + pow($pos1["z"] - $pos2["z"], 2));
}
public static function angle3D($pos1, $pos2){
$X = $pos1["x"] - $pos2["x"];
$Z = $pos1["z"] - $pos2["z"];
$dXZ = sqrt(pow($X, 2) + pow($Z, 2));
$Y = $pos1["y"] - $pos2["y"];
$hAngle = rad2deg(atan2($Z, $X) - M_PI_2);
$vAngle = rad2deg(-atan2($Y, $dXZ));
return array("yaw" => $hAngle, "pitch" => $vAngle);
}
public static function sha1($input){
$number = new Math_BigInteger(sha1($input, true), -256);
$zero = new Math_BigInteger(0);
return ($zero->compare($number) <= 0 ? "":"-") . ltrim($number->toHex(), "0");
}
public static function microtime(){
return microtime(true);
}
public static function curl_get($page){
$ch = curl_init($page);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Minecraft PHP Client 2'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("User-Agent: Minecraft PHP Client 2"));
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
@ -171,7 +225,7 @@ class Utils{
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Minecraft PHP Client 2'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("User-Agent: Minecraft PHP Client 2"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
$ret = curl_exec($ch);
@ -179,42 +233,6 @@ class Utils{
return $ret;
}
public static function strToBin($str){
return Utils::hexToBin(Utils::strToHex($str));
}
public static function hexToBin($hex){
$bin = "";
$len = strlen($hex);
for($i = 0; $i < $len; ++$i){
$bin .= Utils::$hexToBin[$hex{$i}];
}
return $bin;
}
public static function binToStr($bin){
$len = strlen($bin);
if(($len % 8) != 0){
$bin = substr($bin, 0, -($len % 8));
}
$str = "";
for($i = 0; $i < $len; $i += 8){
$str .= chr(bindec(substr($bin, $i, 8)));
}
return $str;
}
public static function binToHex($bin){
$len = strlen($bin);
if(($len % 8) != 0){
$bin = substr($bin, 0, -($len % 8));
}
$hex = "";
for($i = 0; $i < $len; $i += 4){
$hex .= dechex(bindec(substr($bin, $i, 4)));
}
return $hex;
}
public static function strToHex($str){
return bin2hex($str);
}
@ -275,38 +293,11 @@ class Utils{
return pack("n", $value);
}
public static function readTriad($str){
list(,$unpacked) = unpack("N", "\x00".$str);
return (int) $unpacked;
}
public static function writeTriad($value){
return substr(pack("N", $value), 1);
}
public static function readDataArray($str, $len = 10, &$offset = null){
$data = array();
$offset = 0;
for($i = 1; $i <= $len; ++$i){
$l = Utils::readTriad(substr($str, $offset, 3));
$offset += 3;
$data[] = substr($str, $offset, $l);
$offset += $l;
}
return $data;
}
public static function writeDataArray($data){
$raw = "";
foreach($data as $v){
$raw .= Utils::writeTriad(strlen($v));
$raw .= $v;
}
return $raw;
}
public static function readInt($str){
list(,$unpacked) = unpack("N", $str);
if($unpacked >= 2147483648){
$unpacked -= 4294967296;
}
return (int) $unpacked;
}
@ -318,42 +309,35 @@ class Utils{
}
public static function readFloat($str){
list(,$value) = ENDIANNESS === BIG_ENDIAN?unpack('f', $str):unpack('f', strrev($str));
list(,$value) = ENDIANNESS === BIG_ENDIAN ? unpack("f", $str):unpack("f", strrev($str));
return $value;
}
public static function writeFloat($value){
return ENDIANNESS === BIG_ENDIAN?pack('f', $value):strrev(pack('f', $value));
return ENDIANNESS === BIG_ENDIAN ? pack("f", $value):strrev(pack("f", $value));
}
public static function printFloat($value){
return preg_replace("/(\.\d+?)0+$/", "$1", sprintf("%F", $value));
}
public static function readDouble($str){
list(,$value) = ENDIANNESS === BIG_ENDIAN?unpack('d', $str):unpack('d', strrev($str));
list(,$value) = ENDIANNESS === BIG_ENDIAN ? unpack("d", $str):unpack("d", strrev($str));
return $value;
}
public static function writeDouble($value){
return ENDIANNESS === BIG_ENDIAN?pack('d', $value):strrev(pack('d', $value));
return ENDIANNESS === BIG_ENDIAN ? pack("d", $value):strrev(pack("d", $value));
}
public static function readLong($str){
list(,$firstHalf,$secondHalf) = unpack("N*", $str);
if(GMPEXT === true){
$value = gmp_add($secondHalf, gmp_mul($firstHalf, "0x100000000"));
if(gmp_cmp($value, "0x8000000000000000") >= 0){
$value = gmp_sub($value, "0x10000000000000000");
}
return gmp_strval($value);
}else{
$value = bcadd($secondHalf, bcmul($firstHalf, "4294967296"));
if(bccomp($value, "9223372036854775808") >= 0){
$value = bcsub($value, "18446744073709551616");
}
return $value;
}
public static function readLong($str){
$long = new Math_BigInteger($str, -256);
return $long->toString();
}
public static function writeLong($value){
return ENDIANNESS === BIG_ENDIAN?pack('d', $value):strrev(pack('d', $value));
$long = new Math_BigInteger($value, -10);
return $long->toBytes(true);
}
}

View File

@ -48,13 +48,6 @@ if(php_sapi_name()!=="cli"){
++$errors;
}
if(!extension_loaded("gmp")){
console("[NOTICE] Enable GMP extension to increase performance", true, true, 0);
define("GMPEXT", false);
}else{
define("GMPEXT", true);
}
if(!function_exists("gzinflate")){
console("[ERROR] Unable to find Zlib extension", true, true, 0);
++$errors;
@ -75,5 +68,6 @@ require_once("classes/Packet.class.php");
require_once("classes/SerializedPacketHandler.class.php");
require_once("classes/CustomPacketHandler.class.php");
require_once("classes/MinecraftInterface.class.php");
require_once("classes/BigInteger.class.php");
?>