Change StageEncoder behaviour

Following https://github.com/rapid7/metasploit-framework/pull/3770, some
change have been done as multiple encoders is managed by encoded_payload
to be used by Encoder and StageEncoder in the same time.
bug/bundler_fix
agix 2014-11-04 14:09:09 +01:00
parent 24bd814376
commit f81269428d
1 changed files with 17 additions and 30 deletions

View File

@ -17,7 +17,7 @@ module Msf::Payload::Stager
Msf::OptBool.new("EnableStageEncoding", [ false, "Encode the second stage payload", false ]), Msf::OptBool.new("EnableStageEncoding", [ false, "Encode the second stage payload", false ]),
Msf::OptString.new("StageEncoder", [ false, "Encoder to use if EnableStageEncoding is set", nil ]), Msf::OptString.new("StageEncoder", [ false, "Encoder to use if EnableStageEncoding is set", nil ]),
Msf::OptString.new("StageEncoderSaveRegisters", [ false, "Additional registers to preserve in the staged payload if EnableStageEncoding is set", "" ]), Msf::OptString.new("StageEncoderSaveRegisters", [ false, "Additional registers to preserve in the staged payload if EnableStageEncoding is set", "" ]),
Msf::OptBool.new("StageEncodingFallback", [ false, "Fallback to default encoders or no encoding if the selected StageEncoder is not compatible", true ]) Msf::OptBool.new("StageEncodingFallback", [ false, "Fallback to no encoding if the selected StageEncoder is not compatible", true ])
], Msf::Payload::Stager) ], Msf::Payload::Stager)
end end
@ -221,22 +221,11 @@ module Msf::Payload::Stager
# @return [String] Encoded version of +stg+ # @return [String] Encoded version of +stg+
def encode_stage(stg) def encode_stage(stg)
return stg unless encode_stage? return stg unless encode_stage?
stage_enc_mod = [] stage_enc_mod = nil
# Handle StageEncoder if specified by the user # Handle StageEncoder if specified by the user
if datastore['StageEncoder'].to_s.length > 0 if datastore['StageEncoder'].to_s.length > 0
# Allow multiple encoders separated by commas stage_enc_mod = datastore["StageEncoder"]
stage_enc_mod = datastore["StageEncoder"].split(',').map(&:strip).select{|x| x.to_s.length > 0}.uniq
end
# Add automatic encoding as a fallback if needed
if datastore['StageEncodingFallback']
stage_enc_mod << nil
end
# If fallback has been disabled and no encoder was parsed, exit early and rop the session
if stage_enc_mod.length == 0
raise RuntimeError, "StageEncoder is invalid and StageEncodingFallback is disabled"
end end
# Allow the user to specify additional registers to preserve # Allow the user to specify additional registers to preserve
@ -247,35 +236,33 @@ module Msf::Payload::Stager
saved_registers.strip! saved_registers.strip!
estg = nil estg = nil
begin
stage_enc_mod.each do |encoder_refname_from_user|
# Generate an encoded version of the stage. We tell the encoding system # Generate an encoded version of the stage. We tell the encoding system
# to save certain registers to ensure that it does not get clobbered. # to save certain registers to ensure that it does not get clobbered.
encp = Msf::EncodedPayload.create( encp = Msf::EncodedPayload.create(
self, self,
'Raw' => stg, 'Raw' => stg,
'Encoder' => encoder_refname_from_user, 'Encoder' => stage_enc_mod,
'EncoderOptions' => { 'SaveRegisters' => saved_registers }, 'EncoderOptions' => { 'SaveRegisters' => saved_registers },
'ForceSaveRegisters' => true, 'ForceSaveRegisters' => true,
'ForceEncode' => true) 'ForceEncode' => true)
if encp.encoder if encp.encoder
if stage_enc_mod
print_status("Encoded stage with #{stage_enc_mod}")
else
print_status("Encoded stage with #{encp.encoder.refname}") print_status("Encoded stage with #{encp.encoder.refname}")
end
estg = encp.encoded estg = encp.encoded
break
end end
end rescue
if datastore['StageEncodingFallback'] && estg.nil? if datastore['StageEncodingFallback'] && estg.nil?
print_warning("StageEncoder failed, falling back to no encoding") print_warning("StageEncoder failed, falling back to no encoding")
estg = stg estg = stg
end else
unless estg
raise RuntimeError, "Stage encoding failed and StageEncodingFallback is disabled" raise RuntimeError, "Stage encoding failed and StageEncodingFallback is disabled"
end end
end
estg estg
end end