Class: Ronin::Payloads::Encoders::XOR

Inherits:
Encoder
  • Object
show all
Defined in:
lib/ronin/payloads/encoders/xor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Encoder

#inspect, #to_s

Methods included from Model::TargetsOS

included

Methods included from Model::TargetsArch

included

Constructor Details

#initialize(options = {}, &block) ⇒ XOR

Creates a new XOR Encoder object using the given options. If a block is given it will be passed the newly created Encoder object.

options may include the following keys:

:allow

The set of characters allowed in the encoded result. Defaults to (1..255).

:disallow

The set of characters that are not allowed in the encoded result.



45
46
47
48
49
50
51
52
53
# File 'lib/ronin/payloads/encoders/xor.rb', line 45

def initialize(options={},&block)
  @allow = Chars::CharSet.new(options[:allow] || (1..255))

  if options[:disallow]
    @allow -= options[:disallow]
  end

  super(&block)
end

Instance Attribute Details

#allowObject

Set of characters to allow in the encoded data



32
33
34
# File 'lib/ronin/payloads/encoders/xor.rb', line 32

def allow
  @allow
end

Instance Method Details

#encode(data) ⇒ Object

XOR encodes the specified data prefixing the XOR key to the encoded data.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ronin/payloads/encoders/xor.rb', line 59

def encode(data)
  alphabet = Chars.ascii.select { |b| data.include?(b.chr) }
  excluded = (Chars.ascii - alphabet)

  key = excluded.select { |b|
    @allow.include?(b) && alphabet.all? { |i|
      @allow.include?(i ^ b)
    }
  }.last

  text = ''

  text << key.chr
  data.each_byte { |b| text << (b ^ key).chr }
  return text
end