-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Land #679, Add at rest encryption to Meterpreter payloads
- Loading branch information
Showing
5 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
require 'openssl' | ||
|
||
module MetasploitPayloads | ||
module Crypto | ||
CIPHERS = { | ||
chacha20: { | ||
name: 'chacha20'.b, | ||
version: 1, | ||
iv: { | ||
value: "\x52\x25\xd7\xab\x52\x8f\x3f\xf8\x94\x97\x08\x42\x33\xb9\xd3\xb6".b, # 16 bytes | ||
version: 1 | ||
}, | ||
key: { | ||
value: "\x28\x39\x97\x4c\x95\x11\x9d\x42\x6c\x8b\xff\x43\x3e\x5d\x3c\x33\x1b\x95\xd3\xea\xeb\xc9\xae\x71\x0a\x36\xe7\x98\x3d\x9d\x09\x52".b, # 32 bytes | ||
version: 1 | ||
} | ||
} | ||
}.freeze | ||
CURRENT_CIPHER = CIPHERS[:chacha20] | ||
CIPHER_VERSION = CURRENT_CIPHER[:version] | ||
KEY_VERSION = CURRENT_CIPHER[:key][:version] | ||
IV_VERSION = CURRENT_CIPHER[:iv][:version] | ||
# Binary String, unsigned char, unsigned char, unsigned char | ||
ENCRYPTED_PAYLOAD_HEADER = ['msf', CIPHER_VERSION, IV_VERSION, KEY_VERSION].pack('A*CCC') | ||
|
||
private_constant :CIPHERS | ||
private_constant :CURRENT_CIPHER | ||
private_constant :CIPHER_VERSION | ||
private_constant :KEY_VERSION | ||
private_constant :IV_VERSION | ||
|
||
def self.encrypt(plaintext: '') | ||
raise ::ArgumentError, 'Unable to encrypt plaintext: ' << plaintext, caller unless plaintext.to_s | ||
|
||
cipher = ::OpenSSL::Cipher.new(CURRENT_CIPHER[:name]) | ||
|
||
cipher.encrypt | ||
cipher.iv = CURRENT_CIPHER[:iv][:value] | ||
cipher.key = CURRENT_CIPHER[:key][:value] | ||
|
||
output = ENCRYPTED_PAYLOAD_HEADER.dup | ||
output << cipher.update(plaintext) | ||
output << cipher.final | ||
|
||
output | ||
end | ||
|
||
def self.decrypt(ciphertext: '') | ||
raise ::ArgumentError, 'Unable to decrypt ciphertext: ' << ciphertext, caller unless ciphertext.to_s | ||
|
||
cipher = ::OpenSSL::Cipher.new(CURRENT_CIPHER[:name]) | ||
|
||
cipher.decrypt | ||
cipher.iv = CURRENT_CIPHER[:iv][:value] | ||
cipher.key = CURRENT_CIPHER[:key][:value] | ||
|
||
# Remove encrypted header if present | ||
ciphertext = ciphertext.sub(ENCRYPTED_PAYLOAD_HEADER, '') | ||
|
||
output = cipher.update(ciphertext) | ||
output << cipher.final | ||
|
||
output | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'spec_helper' | ||
require 'metasploit-payloads' | ||
|
||
RSpec.describe ::MetasploitPayloads::Crypto do | ||
describe '#encrypt' do | ||
let(:encrypted_header) { ::MetasploitPayloads::Crypto::ENCRYPTED_PAYLOAD_HEADER } | ||
let(:plaintext) { "Hello World!".b } | ||
let(:ciphertext) { encrypted_header + "\x89:^r\xC1\xC9\xD9\xA1\xDC\xEB\xBFm".b } | ||
|
||
it 'can encrypt plaintext' do | ||
expect(described_class.encrypt(plaintext: plaintext)).to eq ciphertext | ||
end | ||
|
||
it 'can decrypt ciphertext' do | ||
expect(described_class.decrypt(ciphertext: ciphertext)).to eq plaintext | ||
end | ||
|
||
it 'is idempotent' do | ||
expect(described_class.decrypt(ciphertext: described_class.encrypt(plaintext: plaintext))).to eq plaintext | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters