-
Notifications
You must be signed in to change notification settings - Fork 0
/
shuffle.rb
60 lines (45 loc) · 1.59 KB
/
shuffle.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Encoder
def initialize
super(
'Name' => 'Shuffle Encoder',
'Description' => %q{
This encoder Shuffles the uneven bytes in the payload to avoid regex pattern matching algorithms. Byte 1 is swapped with byte 3, byte 5 is swapped with byte 7, etc
},
'Author' => 'Klaus Mueller',
'License' => MSF_LICENSE,
'Arch' => ARCH_ALL,
'EncoderType' => Msf::Encoder::Type::Raw)
end
def encode_block(state, block)
encoded = ''
encoder_len = 0
my_array = block.unpack('C*')
# as the encoder needs blocks of 4 bytes we need to make sure we stop at the proper position
len = my_array.length()
# number of bytes that need to be encoded
encoder_len = len - len % 4
my_array.each_with_index do |val,index|
## shuffle bytes if we have a complete block of 4 left
if index <= encoder_len
# if are at position 1 we take byte number 3
if index % 4 == 1
encoded << my_array[index+2].chr
# if are at position 3 we take byte number 1
elsif index % 4 == 3
encoded << my_array[index-2].chr
# the even bytes remain unchanged
else
encoded << val.chr
end
# append the last bytes as they are
else
encoded << val.chr
end
end
return encoded
end
end