当前位置:网站首页>Thoughts on the security of a PHP file name regular verification
Thoughts on the security of a PHP file name regular verification
2022-06-30 14:18:00 【Artorias Li】
One PHP Thinking about safety issues
background
Last weekend I received a message about being a safe friend , Let me help you look at one PHP Source file vulnerability , At first I thought he was just boring and trying to attack others , So I just glanced at him and subconsciously told him “ The writing is not standard, but there seems to be no big loopholes ”. Later he told me it was a CTF test questions ( This is the network security competition ), Only then did I realize that I was a little sloppy , This should touch my knowledge blind spot , Or take it for granted . Certification is needed to deal with this problem .
problem
Problem description
In short , We have obtained a deployment on the server through WEB Access to the PHP The source code file , You need to find a way to attack it to read other files on the target plane .
PHP Source code
<?php
if(!isset($_GET['file'])) {
header('Location: /?file=log.txt');
die();
}
$file = $_GET['file'];
$re = '/^\w*\.\w*$/m';
preg_match_all($re, $file, $matches, PREG_SET_ORDER, 0);
if(count($matches) != 1) {
die('illegal operation!');
}
echo file_get_contents($file);
?>
Thought analysis
- From the source code point of view , You want to read other files of the target , comparison
preg_match_allfunction ,file_get_contentsMore like our goal , And the variables it accepts can just be constructed$_GET['file']To control . $_GET['file']Variables only use simple regular checks , So obviously , The solution to this problem should be to construct payload Conduct code vulnerability attacks .- From above 2 It seems that , The key to this problem lies in this regularity
/^\w*\.\w*$/m了 , Can you construct the path of other directory files that conform to this rule ?- First ,\w Only letters 、 Numbers and _,^ and $ Limits the beginning and end of a string , Therefore, it can be seen that the author of the source file wants to control that only the current directory can be read *.* File format .
- But there is a big flaw in this rule , It's the last one m,m This means that multiple rows can be matched , In other words, you can control the number and content of a string match by adding a newline character . For example , For example, if there is no m( Regular
/^\w*\.\w*$/), Then there is only something likelog.logSuch a string can match the contents , But with this m after , Such as/www/\nlog.logSuch a string can also match the content .( The following is an example )
<?php
$name_1 = "log.log";
$name_2 = "/www/\nlog.log";
$re = '/^\w*\.\w*$/';
preg_match_all($re, $name_1, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
/* array(1) { [0]=> array(1) { [0]=> string(7) "log.log" } } */
preg_match_all($re, $name_2, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
/* array(0) {} */
$re = '/^\w*\.\w*$/m';
preg_match_all($re, $name_1, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
/* array(1) { [0]=> array(1) { [0]=> string(7) "log.log" } } */
preg_match_all($re, $name_2, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
/* array(1) { [0]=> array(1) { [0]=> string(7) "log.log" } } */
- Of course, the vulnerability is more than that , You can see that the number of matching items in the source code must be 1 The logic of , The combination seems to be suddenly enlightened , This should be the loophole , By constructing 、 The file path with the correct file name is OK .
- Now that we're here , This matter should not be delayed , We immediately began to construct such file paths ( for example
/www/%0a.env) For the request —— However, we all ignore a problem at this time ,%0a(urlencode The newline for ) After being passed in as a path , Line breaks are notfile_get_contentsFunction ignores , So sure enough , Yes warning, No files found .
trouble
Here we are. , In fact, the two of us started a variety of “ make blind and disorderly conjectures ” 了
- “ Since the path cannot be found , Then I set up my own server , Let him read my file remotely is a loophole ”
- “…… What people want to read is the target machine .”
- “……”
look , Unique construction path 、 Use file_get_contents The way to get files has become narrower and narrower .
- “ Speaking of reading remote files , Actually php Read post There is one type of input php://input The way , A similar series php The protocol may meet the following requirements in terms of path format .”
- “ I think it should be very close .”
Trigger from this angle , We found it PHP Pseudo protocol —— Input / Output stream , One of them php://filter The function of is very similar to our requirements :
php://filter It's a meta wrapper , Designed for filtering applications when data flow is open . This is for the all-in-one (all-in-one) The file function of is very useful , similar readfile()、 file() and file_get_contents(), There is no chance to apply other filters before the data stream content is read .
Unfortunately, we fell into Line breaks cannot be added to the file path In the tip of the ox horn , Until the end, we still couldn't come up with a solution .
answer
The answer to the question is : php://filter/read=%0aconvert.base64%0a-encode/resource=xxxx
The answer is what we found later , In fact, when we see the answer, we suddenly realize , It is found that our thinking is actually very close , But in the end, I fell into a misunderstanding —— We always firmly believe that what we construct must be and only a complete path , Whether it is a file path or a protocol path .
But you should have noticed , The answer lies in the source resource The previous specifies a convert.base64-encode Filter , The filter name can meet the regular requirements after inserting a newline character , So here resource The corresponding target file path does not need any processing , All that needs to be handled is the name of the filter .
reflection
Is this the end of the problem ?
A careful friend should want to ask questions again :convert.base64-encode Can I still use the newline character added to ? The answer is that the filter will report because the path cannot be found warning, But I will still read resource Source , Just don't deal with it . So the answer here can actually be
php://filter/read=%0anystring.anystring%0a/resource=xxxx
Just use the wrap line breaks before and after for regular matching .
Last
If interested students want to try it by themselves , Use the top source code , Set up a local web Just the service , If you have time, you might as well spread it out to see what filters there are 、 Which? php agreement .
I am ashamed to write here , Compared with other languages ,PHP It is the one that has been used for the longest time , But this usage ( I don't know if it's advanced usage ) For the first time . I suddenly envy being a safe classmate , Have real and practical access to all languages 、 The details and core of the technology . In the process of our own learning , A single angle does limit vision and imagination , Occasionally, you may find a different world by looking for similar technical attack and defense or learning from others' ideas of analyzing vulnerabilities .
Mutual encouragement .
边栏推荐
- Unity animator parameter
- With the development of industrial Internet, the landing and application of the Internet has become wider
- go channel && select
- Three uses of golang underscores
- Flat shading with unity
- Golang template (text/template)
- Cost forecast of PMP (BAC, EAC, etc)
- Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage
- Solve the error in my QT_ thread_ global_ End(): 3 threads didn't exit
- LeetCode_ Stack_ Medium_ 227. basic calculator II (without brackets)
猜你喜欢

Observable, seulement fiable: première bombe de salon de la série cloudops d'exploitation et d'entretien automatisés dans le nuage

Google Earth Engine(GEE)——将字符串的转化为数字并且应用于时间搜索( ee.Date.fromYMD)

IM即时通讯应用开发中无法解决的“顽疾”

Lifting scanning tool

VisualStudio and SQL

More than 20 years after Hong Kong's return, Tupu digital twin Hong Kong Zhuhai Macao Bridge has shocked

【刷题篇】供暖器

QQ was stolen? The reason is

"Persistent diseases" that cannot be solved in IM application development

【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
随机推荐
Mutex lock, read / write lock, spin lock, pessimistic lock, and optimistic lock
Introduction to the renewal of substrate source code: the pallet alliance is incorporated into the main line,
Why does the folder appear open in another program
深入理解.Net中的线程同步之构造模式(二)内核模式4.内核模式构造物的总结
Prometheus 2.29.0 new features
从控制层返回到js的json数据带“\”转译符,怎么去掉
(8)JMeter元件详解之 Once only Controller 仅一次控制器
golang模板(text/template)
【刷题篇】避免洪水泛滥
提权扫描工具
可觀測,才可靠:雲上自動化運維CloudOps系列沙龍 第一彈
【刷题篇】供暖器
DNS resolution home network access public DNS practice
I want to ask how to open an account at China Merchants Securities? Is it safe to open a stock account through the link
The JSON data returned from the control layer to JS has a "\" translator. How to remove it
Logiciel de récupération de données easyrecovery15 téléchargement
Realize a simple LAN communication (similar to feiqiu)
Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
Initial attack and defense world Misc
Inexplicable error occurred in unity's frequent switching branch result model