Skip to content

feat: Added result parser#6

Open
LevitatingOrange wants to merge 2 commits intomainfrom
parser-comb
Open

feat: Added result parser#6
LevitatingOrange wants to merge 2 commits intomainfrom
parser-comb

Conversation

@LevitatingOrange
Copy link
Copy Markdown
Owner

This change adds a simple parser to be more strict about the response of clamd to expect. Each result contains now one single scan result again because I looked into the C code of clamd and tried out experimentally that clamd will only respond with multiple results when it also receives multiple streams or files. I want to add a nicer interface for sessions in another pull request, so this is preliminary work for this.

@LevitatingOrange LevitatingOrange self-assigned this Jul 17, 2023
@LevitatingOrange LevitatingOrange changed the title Added result parser feat: Added result parser Jul 17, 2023
Copy link
Copy Markdown
Collaborator

@McPatate McPatate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I like what you did in the parsing :)

FYI in the case of ALLMATCHSCAN, clamav can return multiple results for a single file. That was why I initially had infection_types be a Vec.
It might interesting to implement Add for results

Comment thread src/error.rs
Comment on lines +46 to +49
/// Occurs when the response from clamd is not what the library
/// expects. Contains the invalid response.
#[error("invalid response from clamd: {0}")]
InvalidResponse(String),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this block moved? It's triggering my OCD (it's not in alphabetical order anymore 😄)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it wasn't in the first place anyways ha

Comment thread src/util.rs
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename the file scan.rs rather than util.rs

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm or scan_result.rs

Comment thread src/util.rs
}

#[derive(Debug, PartialEq)]
pub enum ScanCondition {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub enum ScanCondition {
pub enum ScanStatus {

?

Comment thread src/util.rs
Benign,
}

enum ScanAnswer<'a> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enum ScanAnswer<'a> {
enum ScanResponse<'a> {

?

Comment thread src/util.rs
pub scan_id: u64,
/// the scanned entity, can be a file path, the word "stream"
pub scanned_entity: String,
pub condition: ScanCondition,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub condition: ScanCondition,
pub status: ScanStatus,

Comment thread src/lib.rs
clamd_client.end_session().await?;
Ok(())
}
// TODO 2023-07-17 to be replaced by a better interface
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wdym? Is this for another PR?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I have an idea for how to do sessions in a nice, tokio/rust friendly way; but I guess I should've put that into that PR to not tangle it into here. I'll change that back for now.

Comment thread examples/simple.rs
Comment on lines +32 to +40
let mut bytes = BytesMut::new();

let eicar_bytes = reqwest::get("https://secure.eicar.org/eicarcom2.zip")
.await?
.bytes()
.await?;

let result = clamd_client.scan_bytes(&eicar_bytes).await?;
match result {
ScanResult::Malignent { infection_types } => {
debug!("clamd found a virus(es):\n{}", infection_types.join("\n"))
bytes.extend(eicar_bytes);
bytes.extend( "exec('aW1wb3J0IHNvY2tldCxvcwpzbz1zb2NrZXQuc29ja2V0KHNvY2tldC5BRl')\n\nimport base64,sys;exec(base64.b64decode({2:str,3:lambda b:bytes()}))".as_bytes());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reasoning behind adding multiple viruses?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying out whether scan returns multiple signatures. Forgot to remove it again :D

@LevitatingOrange
Copy link
Copy Markdown
Owner Author

LevitatingOrange commented Jul 18, 2023

Yeah you are right, I forgot about that 😀 the issue with the old implementation is that the Framed Codec already splits the result based on the terminating \0 char, so I don't think the splitting based on that would have worked anyway. I think I would prefer if we return a vec of scan results on methods that can return multiple results and a single scan result on methods that return a single scan result. But I like the add idea too, the only problem is that I want to implement sessions and for that, the scan id differs with every scan result.

@McPatate
Copy link
Copy Markdown
Collaborator

Ah yes that makes sense, let's go with Vec<ScanResult> then!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants