malwaredb/
types.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use pyo3::{pyclass, pymethods};
4
5/// Information about a label
6#[pyclass(frozen)]
7#[derive(Debug, Clone)]
8pub struct Label {
9    /// Label ID
10    #[pyo3(get)]
11    pub id: u64,
12
13    /// Label value
14    #[pyo3(get)]
15    pub name: String,
16
17    /// Label parent
18    #[pyo3(get)]
19    pub parent: Option<String>,
20}
21
22#[pymethods]
23impl Label {
24    /// Label printable representation
25    #[must_use]
26    pub fn __repr__(&self) -> String {
27        if let Some(parent) = &self.parent {
28            format!("Label {}: {parent}/{}", self.id, self.name)
29        } else {
30            format!("Label {}: {}", self.id, self.name)
31        }
32    }
33
34    /// Label information, just the name
35    #[must_use]
36    pub fn __str__(&self) -> String {
37        self.name.clone()
38    }
39}
40
41/// Information about the Malware DB server
42#[pyclass(frozen)]
43#[derive(Debug, Clone)]
44pub struct ServerInfo {
45    /// Operating System used
46    #[pyo3(get)]
47    pub os_name: String,
48
49    /// Memory footprint
50    #[pyo3(get)]
51    pub memory_used: String,
52
53    /// MDB version
54    #[pyo3(get)]
55    pub mdb_version: String,
56
57    /// Type and version of the database
58    #[pyo3(get)]
59    pub db_version: String,
60
61    /// Size of the database on disk
62    #[pyo3(get)]
63    pub db_size: String,
64
65    /// Total number of samples
66    #[pyo3(get)]
67    pub num_samples: u64,
68
69    /// Total number of users
70    #[pyo3(get)]
71    pub num_users: u32,
72
73    /// Uptime of Malware DB in a human-readable format
74    #[pyo3(get)]
75    pub uptime: String,
76}
77
78#[pymethods]
79impl ServerInfo {
80    /// Server Info printable representation
81    #[must_use]
82    pub fn __repr__(&self) -> String {
83        format!(
84            "MalwareDB {} running on {} for {}",
85            self.mdb_version, self.os_name, self.uptime
86        )
87    }
88
89    /// Server info, just the version
90    #[must_use]
91    pub fn __str__(&self) -> String {
92        format!("MalwareDB {}", self.mdb_version)
93    }
94}
95
96/// Information about a sample source
97#[pyclass(frozen)]
98#[derive(Debug, Clone)]
99pub struct Source {
100    /// Identifier of the source
101    #[pyo3(get)]
102    pub id: u32,
103
104    /// Name of the source
105    #[pyo3(get)]
106    pub name: String,
107
108    /// Description of the source
109    #[pyo3(get)]
110    pub description: Option<String>,
111
112    /// URL of the source, or where the files were found
113    #[pyo3(get)]
114    pub url: Option<String>,
115
116    /// Creation date or first acquisition date of or from the source
117    #[pyo3(get)]
118    pub first_acquisition: String,
119
120    /// Whether the source holds malware
121    #[pyo3(get)]
122    pub malicious: Option<bool>,
123}
124
125#[pymethods]
126impl Source {
127    /// Source printable representation
128    #[must_use]
129    pub fn __repr__(&self) -> String {
130        let url = if let Some(url) = &self.url {
131            format!(" from {url}")
132        } else {
133            String::new()
134        };
135
136        let desc = if let Some(desc) = &self.description {
137            format!(" -- {desc}")
138        } else {
139            String::new()
140        };
141
142        format!("{}({}){url}{desc}", self.name, self.id)
143    }
144
145    /// Simpler source printable representation
146    #[must_use]
147    pub fn __str__(&self) -> String {
148        self.name.clone()
149    }
150}
151
152/// Information about a file type supported by Malware DB
153#[pyclass(frozen)]
154#[derive(Debug, Clone)]
155pub struct SupportedFileType {
156    /// Common name of the file type
157    #[pyo3(get)]
158    pub name: String,
159
160    /// Magic number bytes in hex of the file type
161    #[pyo3(get)]
162    pub magic: Vec<String>,
163
164    /// Whether the file type is executable
165    #[pyo3(get)]
166    pub is_executable: bool,
167
168    /// Description of the file type
169    #[pyo3(get)]
170    pub description: Option<String>,
171}
172
173#[pymethods]
174impl SupportedFileType {
175    /// Supported file type as a printable representation
176    #[must_use]
177    pub fn __repr__(&self) -> String {
178        format!("{}, starting with {}", self.name, self.magic.join(" or "))
179    }
180
181    /// Supported file type as a printable simpler representation
182    #[must_use]
183    pub fn __str__(&self) -> String {
184        self.name.clone()
185    }
186}
187
188/// Information about the user's account
189#[pyclass(frozen)]
190#[derive(Debug, Clone)]
191pub struct UserInfo {
192    /// User's numeric ID
193    #[pyo3(get)]
194    pub id: u32,
195
196    /// User's name
197    #[pyo3(get)]
198    pub username: String,
199
200    /// User's group memberships, if any
201    #[pyo3(get)]
202    pub groups: Vec<String>,
203
204    /// User's available sample sources, if any
205    #[pyo3(get)]
206    pub sources: Vec<String>,
207
208    /// If the user is an admin
209    #[pyo3(get)]
210    pub is_admin: bool,
211
212    /// When the account was created
213    #[pyo3(get)]
214    pub created: String,
215
216    /// User has read-only access, perhaps a guest or demo account
217    #[pyo3(get)]
218    pub is_readonly: bool,
219}
220
221#[pymethods]
222impl UserInfo {
223    /// Simple user information
224    #[must_use]
225    pub fn __str__(&self) -> String {
226        self.username.clone()
227    }
228}