Skip to content

Commit 16b5f40

Browse files
author
Brent Cook
committed
Revert "Rework XOR code to make more sense"
This reverts commit 699a8e9.
1 parent 005d349 commit 16b5f40

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

lib/rex/post/meterpreter/packet.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,11 @@ def initialize(type = nil, method = nil)
673673
#
674674
def to_r
675675
raw = super
676-
xor_key = ''
677-
xor_key << (rand(254) + 1).chr
678-
xor_key << (rand(254) + 1).chr
679-
xor_key << (rand(254) + 1).chr
680-
xor_key << (rand(254) + 1).chr
681-
result = xor_key + xor_bytes(xor_key, raw)
676+
xor_key = rand(254) + 1
677+
xor_key |= (rand(254) + 1) << 8
678+
xor_key |= (rand(254) + 1) << 16
679+
xor_key |= (rand(254) + 1) << 24
680+
result = [xor_key].pack('N') + xor_bytes(xor_key, raw)
682681
result
683682
end
684683

@@ -689,7 +688,7 @@ def to_r
689688
# the TLV values.
690689
#
691690
def from_r(bytes)
692-
xor_key = bytes[0,4]
691+
xor_key = bytes[0,4].unpack('N')[0]
693692
super(xor_bytes(xor_key, bytes[4, bytes.length]))
694693
end
695694

@@ -698,7 +697,7 @@ def from_r(bytes)
698697
#
699698
def xor_bytes(xor_key, bytes)
700699
result = ''
701-
bytes.bytes.zip(xor_key.bytes.cycle).each do |b|
700+
bytes.bytes.zip([xor_key].pack('V').bytes.cycle).each do |b|
702701
result << (b[0].ord ^ b[1].ord).chr
703702
end
704703
result

lib/rex/post/meterpreter/packet_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def recv(sock)
5757
# payload length left to the number of bytes
5858
# specified in the length
5959
if (self.hdr_length_left == 0)
60-
xor_key = raw[0, 4]
60+
xor_key = raw[0, 4].unpack('N')[0]
6161
length_bytes = packet.xor_bytes(xor_key, raw[4, 4])
6262
# header size doesn't include the xor key, which is always tacked on the front
6363
self.payload_length_left = length_bytes.unpack("N")[0] - (HEADER_SIZE - 4)

0 commit comments

Comments
 (0)