453 字
2 分钟
浅浅谈一谈一题的perl的open函数
浅浅谈一谈一题的perl的open函数
给了源码,就是perl的审计,问题不大,很快看懂了
#!/usr/bin/perl
use strict;use warnings;use HTTP::Daemon;use HTTP::Status;use File::Spec;use File::MimeInfo::Simple; # cpan install File::MimeInfo::Simpleuse File::Basename;use CGI qw(escapeHTML);
my $webroot = "./files";
my $d = HTTP::Daemon->new(LocalAddr => '0.0.0.0', LocalPort => 8080, Reuse => 1) || die "Failed to start server: $!";
print "Server running at: ", $d->url, "\n";
while (my $c = $d->accept) { while (my $r = $c->get_request) { if ($r->method eq 'GET') { my $path = CGI::unescape($r->uri->path); $path =~ s|^/||; # Remove leading slash $path ||= 'index.html';
my $fullpath = File::Spec->catfile($webroot, $path);
if ($fullpath =~ /\.\.|[,\`\)\(;&]|\|.*\|/) { $c->send_error(RC_BAD_REQUEST, "Invalid path"); next; }
if (-d $fullpath) { # Serve directory listing opendir(my $dh, $fullpath) or do { $c->send_error(RC_FORBIDDEN, "Cannot open directory."); next; };
my @files = readdir($dh); closedir($dh);
my $html = "<html><body><h1>Index of /$path</h1><ul>"; foreach my $f (@files) { next if $f =~ /^\./; # Skip dotfiles my $link = "$path/$f"; $link =~ s|//|/|g; $html .= qq{<li><a href="/$link">} . escapeHTML($f) . "</a></li>"; } $html .= "</ul></body></html>";
my $resp = HTTP::Response->new(RC_OK); $resp->header("Content-Type" => "text/html"); $resp->content($html); $c->send_response($resp);
} else { open(my $fh, $fullpath) or do { $c->send_error(RC_INTERNAL_SERVER_ERROR, "Could not open file."); next; }; binmode $fh; my $content = do { local $/; <$fh> }; close $fh;
my $mime = 'text/html';
my $resp = HTTP::Response->new(RC_OK); $resp->header("Content-Type" => $mime); $resp->content($content); $c->send_response($resp); } } else { $c->send_error(RC_METHOD_NOT_ALLOWED); } } $c->close; undef($c);}解释一下,大致就是服务端捕获客户端请求,然后去进行一系列的过滤,最后去拼接的一个过程,这个过程没有什么命令执行啥的,但是,这里open有个特性,如果open后面有俩参数,而我们可以控制其中一个的时候,我们可以用管道符,就像open( my kk)如果$kk后面跟|,那么就会被当成命令丢给shell,然后本来是要拼接./file/xxxx的,我们要使它是一个独立的命令,所以用换行符编码%0a,然后就
/bin/cat /flag注意这个cat是一个ELF可执行文件,而/flag是作为参数的,所以不能/bin/bash cat /flag,后面的只会当成参数而不是命令,ok完结。
浅浅谈一谈一题的perl的open函数
https://steins-gate.cn/posts/perl的open/ 部分信息可能已经过时





