應用程式層級的漏洞可能允許攻擊者嘗試執行多種惡意攻擊,例如路徑遍歷攻擊、程式碼注入、應用程式拒絕服務、SQL 注入等,稱為PHP 物件注入或PHP 反序列化和此漏洞的原因是使用者向PHP 中的unserialize() 函數提供的輸入未經過正確清理,攻擊者可以透過傳遞透過易受攻擊的unserialize() 臨時序列化的字串,將任意PHP 物件注入到應用程式中。 ) 函數,PHP 中的此漏洞會導致遠端程式碼執行。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法
PHP中宣告serialize()函數的語法如下:
unserialize(value);
其中 value 是要反序列化的值,可能會導致物件注入。
PHP 中物件注入的工作原理如下:
以下是範例:
PHP 程式演示物件注入,將給定值轉換為位元序列,以便可以將其儲存在任何位置,然後使用 unserialize() 函數對其進行反序列化:
代碼:
<html> <body> <?php #The array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value $value = serialize(array("Welcome", "to", "PHP")); #The returned string from the serialize() function is displayed as the output on the screen echo "The data after serialization using serialize() function is as follows:\n"; echo $value; #the serialized data is passed through the unserialize function and the result is stroed in a variable called result $result = unserialize($value); echo "<br>"; #The unserialized data is displayed as the output on the screen echo "The data after deserialization using unserialize() function is as follows:\n"; echo "<br>"; var_dump($result); ?> </body> </html>
輸出:
在上面的程式中,要序列化的資料數組被傳遞給序列化函數,並將傳回的字串儲存在名為 value 的變數中。然後,serialize() 函數傳回的字串將作為輸出顯示在螢幕上。然後序列化的資料透過 unserialize 函數傳遞,並將結果儲存在名為 result 的變數中。然後,未序列化的資料將作為輸出顯示在螢幕上。輸出如上面的快照所示。
PHP 程式演示物件注入,將給定值轉換為位元序列,以便可以將其儲存在任何位置,然後使用 unserialize() 函數對其進行反序列化:
代碼:
<html> <body> <?php #The array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value $value = serialize(array("Learning", "is", "fun")); #The returned string from the serialize() function is displayed as the output on the screen echo "The data after serialization using serialize() function is as follows:\n"; echo $value; #the serialized data is passed through the unserialize function and the result is stroed in a variable called result $result = unserialize($value); echo "<br>"; #The unserialized data is displayed as the output on the screen echo "The data after deserialization using unserialize() function is as follows:\n"; echo "<br>"; var_dump($result); ?> </body> </html>
輸出:
在上面的程式中,要序列化的資料數組被傳遞給序列化函數,並將傳回的字串儲存在名為 value 的變數中。然後,serialize() 函數傳回的字串將作為輸出顯示在螢幕上。然後序列化的資料透過 unserialize 函數傳遞,並將結果儲存在名為 result 的變數中。然後,未序列化的資料將作為輸出顯示在螢幕上。輸出如上面的快照所示。
PHP 程式演示物件注入,將給定值轉換為位元序列,以便可以將其儲存在任何位置,然後使用 unserialize() 函數對其進行反序列化:
代碼:
<html> <body> <?php #The array of data to be serialized is passed to the serialize function and the returned string is stored in a variable called value $value = serialize(array("We", "love", "India")); #The returned string from the serialize() function is displayed as the output on the screen echo "The data after serialization using serialize() function is as follows:\n"; echo $value; #the serialized data is passed through the unserialize function and the result is stroed in a variable called result $result = unserialize($value); echo "<br>"; #The unserialized data is displayed as the output on the screen echo "The data after deserialization using unserialize() function is as follows:\n"; echo "<br>"; var_dump($result); ?> </body> </html>
輸出:
在上面的程式中,要序列化的資料數組被傳遞給序列化函數,並將傳回的字串儲存在名為 value 的變數中。然後,serialize() 函數傳回的字串將作為輸出顯示在螢幕上。然後序列化的資料透過 unserialize 函數傳遞,並將結果儲存在名為 result 的變數中。然後,未序列化的資料將作為輸出顯示在螢幕上。輸出如上面的快照所示。
以上是PHP 物件注入的詳細內容。更多資訊請關注PHP中文網其他相關文章!