Method: Rex::Encoding::Xor::DwordAdditive._find_good_key

Defined in:
lib/rex/encoding/xor/dword_additive.rb

._find_good_key(data, badkeys, badchars) ⇒ Object

I realize this algorithm is broken. We invalidate some keys in _find_bad_keys that could actually be perfectly fine. However, it seems to work ok for now, and this is all just a lame adhoc method. Maybe someday we can revisit this and make it a bit less ghetto…



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rex/encoding/xor/dword_additive.rb', line 54

def DwordAdditive._find_good_key(data, badkeys, badchars)

  ksize  = keysize
  kstart = ""
  ksize.times { kstart << rand(256) } # random key starting place

  key = kstart.dup

  #
  # now for the ghettoness of an algorithm:
  #  try the random key we picked
  #  if the key failed, figure out which key byte corresponds
  #  increment that key byte
  #  if we wrapped a byte all the way around, fail :(
  #

  loop do
    # ok, try to encode it, any bad chars present?
    pos = _check(data, key, badchars)

    # yay, no problems, we found a key!
    break if !pos

    strip = pos % ksize

    # increment the offending key byte
    key[strip] = key[strip] + 1 & 0xff

    # We wrapped around!
    if key[strip] == kstart[strip]
      raise KeySearchError, "Key space exhausted on strip #{strip}!", caller
    end
  end

  return key
end