Skip to content

Commit 896d1b1

Browse files
author
Sandesh Grangdan
committed
Fixed alerting and save input.
1 parent 8a031a2 commit 896d1b1

File tree

5 files changed

+143
-35
lines changed

5 files changed

+143
-35
lines changed

Cargo.lock

Lines changed: 79 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "antivirus"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
edition = "2021"
55

66
# Github Repo
@@ -12,6 +12,7 @@ license = "MIT OR Apache-2.0"
1212

1313
[dependencies]
1414
clap = { version = "4.5.16", features = ["derive"] }
15+
rand = "0.8.5"
1516
regex = "1.10.6"
1617
tokio = {version = "1.40.0" , features = ["full"] }
1718

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ A command-line utility for scanning directories with ClamAV, updating virus defi
1717

1818
### *Method 2*: Install prebuilt binaries via shell script (Linux, macOS)
1919
```bash
20-
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/sandeshgrangdan/antivirus/releases/download/v0.1.2/antivirus-installer.sh | sh
20+
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/sandeshgrangdan/antivirus/releases/download/v0.1.3/antivirus-installer.sh | sh
2121
```
2222

23-
### *Method 3*:
23+
### *SETUP PATH*:
2424
To add $HOME/.cargo/bin to your PATH, either restart your shell or run:
2525
```bash
2626
source $HOME/.cargo/env (sh, bash, zsh)
@@ -40,7 +40,6 @@ $ antivirus
4040
```
4141
>You need to first setup freshclam
4242
- *(Linux)*: Create /usr/local/etc/clamav/freshclam.conf from /usr/local/etc/clamav/freshclam.conf.sample.
43-
- *(MacOS)*: Create /opt/homebrew/etc/clamav/freshclam.conf from /opt/homebrew/etc/clamav/freshclam.conf.sample (For Mac).
4443
- Remove or comment-out the Example line from freshclam.conf
4544
- Run freshclam to download the latest malware definitions.
4645
```

src/antivirus.rs

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::{env, thread};
22
use std::process::{Command, Stdio};
33
use std::io::{self, BufRead};
4+
use rand::Rng;
45
use regex::Regex;
56
use clap::Parser;
6-
use std::fs;
7+
use std::fs::{self, File};
78
use std::io::Write;
89

910
mod linux;
@@ -26,7 +27,9 @@ pub struct Antivirus {
2627
home_dir: String,
2728
google_chat_url: String,
2829
summary: String,
29-
args: Args
30+
infected_files: String,
31+
args: Args,
32+
tmp_file : String
3033
}
3134

3235
fn is_clamav_installed() -> io::Result<bool> {
@@ -37,6 +40,14 @@ fn is_clamav_installed() -> io::Result<bool> {
3740
Ok(!output.stdout.is_empty())
3841
}
3942

43+
fn generate_random_file_name() -> String {
44+
let mut rng = rand::thread_rng();
45+
let random_string: String = (0..10)
46+
.map(|_| rng.sample(rand::distributions::Alphanumeric) as char)
47+
.collect();
48+
format!("/tmp/{}.txt", random_string)
49+
}
50+
4051
fn handle_freshclam_copy(path: &str) -> std::io::Result<()>{
4152
let sample_path = format!("{}/freshclam.conf.sample",path);
4253
let config_path = &format!("{}/freshclam.conf",path);
@@ -178,6 +189,8 @@ impl Antivirus {
178189
Self {
179190
home_dir : env::var("HOME").expect("Failed to get HOME directory"),
180191
summary: String::new(),
192+
infected_files: String::new(),
193+
tmp_file: generate_random_file_name(),
181194
google_chat_url,
182195
args
183196
}
@@ -195,8 +208,8 @@ impl Antivirus {
195208
"--archive-verbose",
196209
"--alert-exceeds-max=yes",
197210
"--alert-encrypted=yes",
198-
"--max-filesize=4095M",
199-
"--max-scansize=4095M",
211+
"--max-filesize=10000M",
212+
"--max-scansize=10000M",
200213
"--max-files=1000000",
201214
"--max-recursion=512",
202215
"--max-htmlnotags=256M",
@@ -212,6 +225,7 @@ impl Antivirus {
212225
.expect("Failed to execute clamscan");
213226

214227
let regex_patterns = vec![
228+
Regex::new(r": FOUND$").unwrap(),
215229
Regex::new(r"^----------- SCAN SUMMARY -----------").unwrap(),
216230
Regex::new(r"^Known viruses:").unwrap(),
217231
Regex::new(r"^Engine version:").unwrap(),
@@ -225,6 +239,10 @@ impl Antivirus {
225239
Regex::new(r"^End Date:").unwrap(),
226240
];
227241

242+
let infected_regex_patterns = vec![
243+
Regex::new(r": FOUND$").unwrap(),
244+
];
245+
228246
self.summary.push_str(&format!("{}\n\n", self.home_dir));
229247

230248
if let Some(stdout) = child.stdout.take() {
@@ -236,6 +254,9 @@ impl Antivirus {
236254
if regex_patterns.iter().any(|regex| regex.is_match(&line)) {
237255
self.summary.push_str(&format!("{}\n", line));
238256
}
257+
if infected_regex_patterns.iter().any(|regex| regex.is_match(&line)) {
258+
self.infected_files.push_str(&format!("{}\n", line));
259+
}
239260
},
240261
Err(err) => eprintln!("Error reading line: {}", err),
241262
}
@@ -248,37 +269,44 @@ impl Antivirus {
248269

249270
}
250271

251-
pub async fn notify(&self){
272+
pub async fn notify(&mut self){
252273
if self.google_chat_url != "" {
253-
254-
let message = format!(r#"{{"text": "{}"}}"#, self.summary);
255-
256-
let output = Command::new("curl")
257-
.arg("-X")
258-
.arg("POST")
259-
.arg("-H")
260-
.arg("Content-Type: application/json")
261-
.arg("-d")
262-
.arg(message)
263-
.arg(&self.google_chat_url)
264-
.output()
265-
.expect("Failed to execute curl");
266-
267-
if output.status.success() {
268-
println!("Message sent successfully to google chat!");
269-
} else {
270-
println!("Failed to send message.");
274+
self.google_chat(&self.summary);
275+
if self.infected_files != "" {
276+
self.infected_files.push_str(&format!("\nResult Output: {}\n", self.tmp_file));
277+
self.google_chat(&self.infected_files);
271278
}
272279
}
273280
}
274281

275-
// fn generate_random_file_name() -> String {
276-
// let mut rng = rand::thread_rng();
277-
// let random_string: String = (0..10)
278-
// .map(|_| rng.sample(rand::distributions::Alphanumeric) as char)
279-
// .collect();
280-
// format!("/tmp/{}.txt", random_string)
281-
// }
282+
fn google_chat(&self,message: &String){
283+
let send_message = format!(r#"{{"text": "{}"}}"#, message);
284+
285+
let output = Command::new("curl")
286+
.arg("-X")
287+
.arg("POST")
288+
.arg("-H")
289+
.arg("Content-Type: application/json")
290+
.arg("-d")
291+
.arg(send_message)
292+
.arg(&self.google_chat_url)
293+
.output()
294+
.expect("Failed to execute curl");
295+
296+
if output.status.success() {
297+
println!("Message sent successfully to google chat!");
298+
} else {
299+
println!("Failed to send message.");
300+
}
301+
}
282302

303+
pub fn save_infected_file_on_temp(&self){
304+
let mut output = File::create(&self.tmp_file).unwrap();
305+
306+
write!(output, "{}", self.summary).unwrap();
307+
if self.infected_files != "" {
308+
write!(output, "{}", self.infected_files).unwrap();
309+
}
310+
}
283311

284312
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ async fn main() {
88

99
app.scan();
1010
app.notify().await;
11+
12+
app.save_infected_file_on_temp();
1113
}
1214

0 commit comments

Comments
 (0)