PHP HEAD Method Trick (GDG Algiers CTF 2022)

2022. 10. 10. 14:12·🖌️ Theory/WEB

While playing CTF, I found a interesting idea so I note in here.

 

● ezphp(fixed)

You can find a hint to solve this problem in Header. When you enter the site, you can find response header like this.

The point is the version of PHP. PHP 5.3.x Version has "HEAD Method Trick" vulnerability. 

To solve this challenge, you must use "HEAD" method to bypass code "$_SESSION["admin"]=0;". That is to say you send HEAD method to break the code and stop at "echo" not to run code "$_SESSION["admin"]=0". As a result, you can get flag.

 

About "HEAD Method Trick" in PHP version 5.3.x

When "HEAD" method is used, PHP stops on the first output. How does it work like this? Let's check PHP code.

//    php-5.3.5\main\SAPI.c line 315:
if (SG(request_info).request_method &&
!strcmp(SG(request_info).request_method, "HEAD")) {
SG(request_info).headers_only = 1;
php-5.3.5\main\output.c line 699
(fucntion php_ub_body_write which is executed when
output data arrives):
if (SG(request_info).headers_only) {
if(SG(headers_sent)) {
    return 0;
}
php_header(TSRMLS_C);
zend_bailout(); // <--- this will stop script
}

In short, function named "zend_bailout()" has script stop where "echo" function exists. This provides some security holes.

 

Examples

<?php
session_start();
$_SESSION[’admin’]=1;
if (!isset($_POST[’pass’]) || $_POST[’pass’]!=’somepassword’)
{
    echo ’<b>Wrong or empty password.</b><br>’;
    $_SESSION[’admin_level’]=0;
}
?>

If attacker use HEAD method, "$_SESSION['admin_level']" will stay set to 1 because script stops running in the middle. It's vulnerable.

<?php
$line=’Nick: ’.htmlspecialchars($_POST[’nick’]).’<br>
Text: ’.htmlspecialchars($_POST[’text’]).’<hr>’;
$f=fopen("book.txt","r");
$data=fread($f,filesize("book.txt"));
fclose($f);
$f=fopen("book.txt","w");
$data=$line.$data;
echo $data;
fwrite($f,$data);
fclose($f);
?>

If attacker opens that script while using HEAD method, the script will stop "echo $data". As a result, no input in book.txt so it will be empty.

 

 

저작자표시 비영리 변경금지 (새창열림)
'🖌️ Theory/WEB' 카테고리의 다른 글
  • XXE Injection (Basic Concept & Real World Case)
  • Cache Poisoning Attack
  • Advanced Tips For SSRF Attack
  • Small Tip
Cronus
Cronus
Offensive Security Researcher
  • Cronus
    Cronus
    Striving to be the best.
    • 분류 전체보기 (251)
      • AboutMe (1)
      • Portfolio (1)
        • Things (1)
      • Bug Report (1)
      • 🚩 CTF (23)
        • Former Doc (9)
        • 2023 (9)
      • 💻 Security (5)
      • 🖌️ Theory (22)
        • WEB (9)
        • PWN (13)
      • 📄 Project (6)
        • Edu_Siri (6)
      • Dreamhack (156)
        • WEB (95)
        • PWN (41)
        • Crypto (14)
        • ETC (6)
      • Wargame (22)
        • HackCTF (22)
      • Bug Bounty (1)
        • Hacking Zone (1)
      • Tips (7)
      • Development (2)
        • Machine Learning & Deep Lea.. (1)
      • Offensive Tools (1)
  • 블로그 메뉴

    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Ubuntu 기초
    Deep learning
    python
    ubuntu 명령어
    TsukuCTF2022
    sqli
    GPNCTF
    Remote Code Execution
    bug report
    justCTF
    pwntools
    Machine Learning
    TFCCTF2022
    RCE
    cache
    bug hunter
    Text Summarization
    cache poisoning
    Ubuntu 기초 셋팅
    Crypto
  • 최근 댓글

  • 최근 글

Cronus
PHP HEAD Method Trick (GDG Algiers CTF 2022)
상단으로

티스토리툴바