在ctf比赛中常常见到md5的身影,对于md5,常见的利用方式有以下几种

在sql注入中

若遇到了类似以下的情况

1
2
3
$password=$_POST['password'];
$sql = "SELECT * FROM admin WHERE username = 'admin' and password = '".md5($password,true)."'";
$result=mysqli_query($link,$sql);

可以传入ffifdyop或者129581926211651571912466741651878684928,这两者md5加密后的结果都包含 ‘ OR ‘ 的字样,可用于引号逃逸

在弱类型比较中

若存在对两个md5加密后的字符串进行比较时,可传入类似s1502113478a、s1836677006a的字符串,加密后都是以0e开头,比较时都为零

1
2
3
4
5
6
7
8
9
<?php
$a = "s1502113478a";
$b = "s1836677006a";

$a_1 = md5($a);
$b_1 = md5($b);

echo $a_1."\n".$b_1."\n";
?>
1
2
3
4
┌──(kali㉿kali)-[~/Documents]
└─$ php md5.php
0e861580163291561247404381396064
0e481036490867661113260034900752

在允许传递数组时

可以传入两个空数组,由于md5无法对数组进行加密,故返回null,而 null = null 为 true

1
2
3
4
5
6
7
8
9
<?php
$a = array();
$b = array();

$a_1 = md5($a);
$b_1 = md5($b);

echo $a_1."\n".$b_1."\n";
?>
1
2
3
4
┌──(kali㉿kali)-[~/Documents]
└─$ php em_array.php
PHP Warning: md5() expects parameter 1 to be string, array given in /home/kali/Documents/em_array.php on line 5
PHP Warning: md5() expects parameter 1 to be string, array given in /home/kali/Documents/em_array.php on line 6

虽然转换失败,但是返回了 null ,需要注意的是,在传递数组时应使用如下形式:

1
a[]=1 & b[1]="b" & c[]

md5碰撞

当上述方法都不奏效时:可以使用md5算法本身的漏洞;md5碰撞指的是不同的输入进行md5加密后得到同样的输出,网上有许多算法可以实现,需要注意的是,由于输入需上传服务器,故需要对输出进行url编码

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
#!coding:utf-8
hexString1 = '4dc968ff0ee35c209572d4777b721587d36fa7b21bdc56b74a3dc0783e7b9518afbfa200a8284bf36e8e4b55b35f427593d849676da0d1555d8360fb5f07fea2'
hexString2 = '4dc968ff0ee35c209572d4777b721587d36fa7b21bdc56b74a3dc0783e7b9518afbfa202a8284bf36e8e4b55b35f427593d849676da0d1d55d8360fb5f07fea2'

hexList1 = []
intList1 = []
asciiString1 =''

while True:
intString1 = hexString1[0:2]
hexString1 = hexString1[2:]
hexList1.append(intString1)
if (hexString1 == ''):
break

for i in hexList1:
intList1.append(int(i,16))
for j in intList1:
asciiString1 += chr(int(j))

f = open('1.bin','w')
f.write(asciiString1)
f.close()

hexList2 = []
intList2 = []
asciiString2 =''

while True:
intString2 = hexString2[0:2]
hexString2 = hexString2[2:]
hexList2.append(intString2)
if (hexString2 == ''):
break

for i in hexList2:
intList2.append(int(i,16))
for j in intList2:
asciiString2 += chr(int(j))

f = open('2.bin','w')
f.write(asciiString2)
f.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!coding:utf-8
import urllib.request

urlString1=''
urlString2 = ''

for line in open('1.bin'):
urlString1 += urllib.parse.quote(line)

for line in open('2.bin'):
urlString2 += urllib.parse.quote(line)

print(urlString1)
print(urlString2)

评论