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
use aes_gcm::{
    aead::{Aead, OsRng},
    AeadCore, Aes256Gcm, Key, KeyInit, Nonce,
};

use crate::{EncryptedPayload, Payload, RawPayload};

#[cfg(feature = "aes")]
pub struct AesCypher<T: Payload> {
    inner: RawPayload,
    _marker: std::marker::PhantomData<T>,
}

#[cfg(feature = "aes")]
impl<P: Payload> From<RawPayload> for AesCypher<P> {
    fn from(inner: RawPayload) -> Self {
        Self {
            inner,
            _marker: std::marker::PhantomData,
        }
    }
}

#[cfg(feature = "aes")]
impl<'a, P: Payload> EncryptedPayload for AesCypher<P> {
    type Key = [u8; 32];
    type InnerPayload = P;

    fn decrypt(self, key: &Self::Key) -> P {
        let vec = self.inner.to_vec();
        let (nonce, encrypted) = vec.split_at(12);
        let key = Key::<Aes256Gcm>::from_slice(key);
        let nonce = Nonce::from_slice(nonce);

        Aes256Gcm::new(&key)
            .decrypt(nonce, encrypted)
            .expect("Failed to decrypt shellcode using AES")
            .to_vec()
            .into()
    }
}

#[cfg(feature = "aes")]
pub trait AesCypherExt {
    fn aes_encrypt(self, key: &[u8; 32]) -> RawPayload;
}

#[cfg(feature = "aes")]
impl AesCypherExt for RawPayload {
    fn aes_encrypt(self, key: &[u8; 32]) -> RawPayload {
        let key = Key::<Aes256Gcm>::from_slice(key);
        let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
        let encrypted = Aes256Gcm::new(&key)
            .encrypt(&nonce, self.as_slice())
            .expect("Failed to encrypt shellcode using AES");

        [nonce.to_vec(), encrypted].concat()
    }
}