hook stylies! return index of offending byte or nil
# File lib/rex/encoding/xor/generic.rb, line 24 def Generic._check(data, key, badchars) return _check_key(key, badchars) || _check_encode(data, key, badchars) end
# File lib/rex/encoding/xor/generic.rb, line 30 def Generic._check_encode(data, key, badchars) return Rex::Text.badchar_index(encode(data, key), badchars) end
# File lib/rex/encoding/xor/generic.rb, line 27 def Generic._check_key(key, badchars) return Rex::Text.badchar_index(key, badchars) end
kind of ghetto, but very convenient for mutating keys by default, do no key mutations
# File lib/rex/encoding/xor/generic.rb, line 133 def Generic._encode_mutate_key(buf, key, pos, len) return key end
Find a list of bytes that can't be valid xor keys, from the data and badchars. This returns a Array of hashes, length keysize
# File lib/rex/encoding/xor/generic.rb, line 44 def Generic._find_bad_keys(data, badchars) ksize = keysize # array of hashes for the bad characters based # on their position in the data badkeys = [ ] ksize.times { badkeys << { } } badchars.each_byte { |badchar| pos = 0 data.each_byte { |char| badkeys[pos % ksize][char ^ badchar] = true pos += 1 } } return badkeys end
(Hopefully) find a good key, from badkeys and badchars
# File lib/rex/encoding/xor/generic.rb, line 67 def Generic._find_good_key(data, badkeys, badchars) ksize = keysize strip = 0 key = "" while strip < keysize kbyte = rand(256) catch(:found_kbyte) do 256.times { if !badkeys[strip][kbyte] && !badchars[kbyte.chr] throw :found_kbyte end kbyte = (kbyte + 1) & 0xff } raise KeySearchError, "Exhausted byte space for strip #{strip}!", caller end key << kbyte strip += 1 end # ok, we should have a good key now, lets double check... if _check(data, key, badchars) raise KeySearchError, "Key found, but bad character check failed!", caller end return key end
# File lib/rex/encoding/xor/generic.rb, line 102 def Generic.encode(buf, key) if !key.kind_of?(String) raise ::ArgumentError, "Key must be a string!", caller end len = key.length if len == 0 raise ::ArgumentError, "Zero key length!", caller end if keysize != 0 && keysize != len raise ::ArgumentError, "Key length #{len}, expected #{keysize}", caller end encoded = "" pos = 0 while pos < buf.length encoded += (buf[pos,1].unpack("C*")[0] ^ key[pos % len, 1].unpack("C*")[0]).chr key = _encode_mutate_key(buf, key, pos, len) pos += 1 end return [ encoded, key ] end
# File lib/rex/encoding/xor/generic.rb, line 34 def Generic.find_key(data, badchars) return _find_good_key(data, _find_bad_keys(data, badchars), badchars) end
Generated with the Darkfish Rdoc Generator 2.