Class: Ronin::Encoders::XOR

Inherits:
Encoder
  • Object
show all
Defined in:
lib/ronin/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 = {}) {|encoder| ... } ⇒ XOR

Creates a new XOR Encoder object.

Options Hash (options):

  • :allow (Array, Range) — default: 1..255

    The set of characters allowed in the encoded result.

  • :disallow (Array, Range)

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

Yields:

  • (encoder)

    If a block is given it will be passed the newly created xor encoder.

Yield Parameters:

  • encoder (XOR)

    The newly created xor encoder.


54
55
56
57
58
59
60
61
62
# File 'lib/ronin/encoders/xor.rb', line 54

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/encoders/xor.rb', line 32

def allow
  @allow
end

Instance Method Details

#encode(data) ⇒ String

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


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ronin/encoders/xor.rb', line 74

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

  key = excluded.enum_for(:reverse_each).find do |b|
    @allow.include?(b) && alphabet.all? { |i|
      @allow.include?(i ^ b)
    }
  end

  text = ''

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