Commit graph

97 commits

Author SHA1 Message Date
Val S.
a77a271fb5
Reduce unnecessary scanning of embedded file FPs (#1571)
When embedded file type recognition finds a possible embedded file, it
is being scanned as a new embedded file even if it turns out it was a
false positive and parsing fails. My solution is to pre-parse the file
headers as little possible to determine if it is valid. If possible,
also determine the file size based on the headers. That will make it so
we don't have to scan additional data when the embedded file is not at
the very end.

This commit adds header checks prior to embedded ZIP, ARJ, and CAB
scanning. For these types I was also able to use the header checks to
determine the object size so as to prevent excessive pattern matching.

TODO: Add the same for RAR, EGG, 7Z, NULSFT, AUTOIT, IShield, and PDF.

This commit also removes duplicate matching for embedded MSEXE.
The embedded MSEXE detection and scanning logic was accidentally
creating an extra duplicate layer in between scanning and detection
because of the logic within the `cli_scanembpe()` function.
That function was effectively doing the header check which this commit
adds for ZIP, ARJ, and CAB but minus the size check.
Note: It is unfortunately not possible to get an accurage size from PE
file headers.
The `cli_scanembpe()` function also used to dump to a temp file for no
reason since FMAPs were extended to support windows into other FMAPs.
So this commit removes the intermediate layer as well as dropping a temp
file for each embedded PE file.

Further, this commit adds configuration and DCONF safeguards around all
embedded file type scanning.

Finally, this commit adds a set of tests to validate proper extraction
of embedded ZIP, ARJ, CAB, and MSEXE files.

CLAM-2862

Co-authored-by: TheRaynMan <draynor@sourcefire.com>
2025-09-23 15:57:28 -04:00
Valerie Snyder
aa7b7e9421
Swap clean cache from MD5 to SHA2-256
Change the clean-cache to use SHA2-256 instead of MD5.
Note that all references are changed to specify "SHA2-256" now instead
of "SHA256", for clarity. But there is no plan to add support for SHA3
algorithms at this time.

Significant code cleanup. E.g.:
- Implemented goto-done error handling.
- Used `uint8_t *` instead of `unsigned char *`.
- Use `bool` for boolean checks, rather than `int.
- Used `#defines` instead of magic numbers.
- Removed duplicate `#defines` for things like hash length.

Add new option to calculate and record additional hash types when the
"generate metadata JSON" feature is enabled:
- libclamav option: `CL_SCAN_GENERAL_STORE_EXTRA_HASHES`
- clamscan option: `--json-store-extra-hashes` (default off)
- clamd.conf option: `JsonStoreExtraHashes` (default 'no')

Renamed the sigtool option `--sha256` to `--sha2-256`.
The original option is still functional, but is deprecated.

For the "generate metadata JSON" feature, the file hash is now stored as
"sha2-256" instead of "FileMD5". If you enable the "extra hashes" option,
then it will also record "md5" and "sha1".

Deprecate and disable the internal "SHA collect" feature.
This option had been hidden behind C #ifdef checks for an option that
wasn't exposed through CMake, so it was basically unavailable anyways.

Changes to calculate file hashes when they're needed and no sooner.

For the FP feature in the matcher module, I have mimiced the
optimization in the FMAP scan routine which makes it so that it can
calculate multiple hashes in a single pass of the file.

The `HandlerType` feature stores a hash of the file in the scan ctx to
prevent retyping the exact same data more than once.
I removed that hash field and replaced it with an attribute flag that is
applied to the new recursion stack layer when retyping a file.
This also closes a minor bug that would prevent retyping a file with an
all-zero hash. :)

The work upgrading cache.c to support SHA2-256 sized hashes thanks to:
https://github.com/m-sola

CLAM-255
CLAM-1858
CLAM-1859
CLAM-1860
2025-08-14 21:23:30 -04:00
Valerie Snyder
0cc5d75093
ZIP: Fix infinite loop + significant code cleanup
An infinite loop may occur when scanning some malformed ZIP files.

I introduced this issue in 96c00b6d80
with this line:

```c
// decrement coff by 1 to account for the increment at the end of the loop
coff -= 1;
```

The problem is that the function may return 0, which should
indicate that there are no more files. The result was that
`coff` would stay the same and the loop would repeat.

This issue is in 1.5 development and affects the 1.5.0 beta but
does not affect any production versions.

Fixes: https://github.com/Cisco-Talos/clamav/issues/1534

Special thanks to Sophie0x2E for an initial fix, proposed in
https://github.com/Cisco-Talos/clamav/pull/1539
In review, I was uncomfortable with other existing code and
decided to to a more significant overhaul of the error handling
in the ZIP module.

In addition to cleanup, this commit has some functional changes:

- When parsing a central directory file header inside of
  `parse_central_directory_file_header()`, it will now fail out if the
  "extra length" or "comment length" fields would exceced the length of
  the archive. That doesn't mean the associated local file header won't
  be parsed later, but it won't use the central directory file header
  to find it. Instead, the ZIP module will have to find the local file
  header by searching for extra records not listed in the central directory.

  This change was mostly to tidy up complex error handling.

- Add two FTM new signatures to identify split ZIP archives.

  This signature identifies the first segment (first file) in a split or
  spanned ZIP archive. It may also be found on a single-segment "split"
  archive, depending on the ZIP archiver.
  ```
  0:0:504b0708504b0304:ZIP (First segment split/spanned):CL_TYPE_ANY:CL_TYPE_ZIP
  ```

  Practically speaking, this new signature makes it so ClamAV identifies
  the file as a ZIP right away without having to rely on SFX_ZIP detection.
  Extraction is then handled by the ZIP `cli_unzip` function rather than
  extracting each with `cli_unzip_single` which handles SFX_ZIP entries.

  Note: ClamAV isn't capable of finding additional files on disk to support
  handling the additional segments. So it doesn't make any difference with
  handling those other files.

  This signature is for single-segment split/spanned archives, depending
  on the ZIP archiver.
  ```
  0:0:504b0303504b0304:ZIP (Single-segment split/spanned):CL_TYPE_ANY:CL_TYPE_ZIP
  ```
  Like the first one, this also means we won't rely on SFX_ZIP detection
  and will treat this files as regular ZIPs.

- Added a test file to verify that ClamAV can extract a single-file
  "split" ZIP.

- Added a clamscan test with test files to verify that scanning a split
  archive across two segments correctly extracts the properly formed zip
  file entries. Sadly, we can't join the segments to extract everything.
2025-08-11 18:14:19 -04:00
Val S.
e86919789f
Merge pull request #1482 from jhumlick/CLAM-2588-PDF-Urls
Clam 2588 Record PDF URIs if generating scan metadata
2025-05-31 18:57:00 -04:00
John Humlick
e1e3d4c64d
libclamav: Add URI scanning support to PDF parser
Threat Research requests scanning URIs in PDF files and adding them to
the json report file.

This change adds URI scanning support to the PDF parser, including
support for object references to URIs in PDF files.

Jira: CLAM-2588

Fix out-of-order references and other minor improvements.

CLAM-2588, CLAM-2757
2025-05-30 12:41:17 -07:00
Valerie Snyder
544fb9fef2
Codesign: fix test files & upgrade clamav-signature-util for related fix
The .sign test files have the min flevel set to 220.
It should be 230.

Also upgrade clamav-signature-util to v1.2.4 for fix so new .sign
files will have the correct min flevel.
2025-05-05 16:54:07 -04:00
Val Snyder
afb3d490e1
Fix several codesign bugs
We were signing with the signing key + signing cert and verifying
with the intermediate cert + root cert. However, we should have been
signing with the signing key + signing cert + intermediate cert, and
verifying with just the root cert.
To fix this, I...
1. Provided new certs and test file .sign files to use the correct
   signing method.
2. Restructured the `unit_tests/input/signing` directory to highlight
   which files are for signing and which are for verification.

There is a multi-arch build issue because I previously used i8 to
represent a C character. I switched it to c_char, which should fix the
clamav-debian multi-arch Docker image build.

It turns out we weren't failing out when signing if one of the provided
intermediate certificate paths is incorrect. Instead of using
`filter_map()`, I switched to just iterate the list to populate the
vector of intermediate certs.
2025-03-29 20:38:08 -04:00
Val Snyder
8d485b9bfd
FIPS-compliant CVD signing and verification
Add X509 certificate chain based signing with PKCS7-PEM external
signatures distributed alongside CVD's in a custom .cvd.sign format.
This new signing and verification mechanism is primarily in support
of FIPS compliance.

Fixes: https://github.com/Cisco-Talos/clamav/issues/564

Add a Rust implementation for parsing, verifying, and unpacking CVD
files.

Now installs a 'certs' directory in the app config directory
(e.g. <prefix>/etc/certs). The install location is configurable.
The CMake option to configure the CVD certs directory is:
  `-D CVD_CERTS_DIRECTORY=PATH`

New options to set an alternative CVD certs directory:
- Commandline for freshclam, clamd, clamscan, and sigtool is:
  `--cvdcertsdir PATH`
- Env variable for freshclam, clamd, clamscan, and sigtool is:
  `CVD_CERTS_DIR`
- Config option for freshclam and clamd is:
  `CVDCertsDirectory PATH`

Sigtool:
- Add sign/verify commands.
- Also verify CDIFF external digital signatures when applying CDIFFs.
- Place commonly used commands at the top of --help string.
- Fix up manpage.

Freshclam:
- Will try to download .sign files to verify CVDs and CDIFFs.
- Fix an issue where making a CLD would only include the CFG file for
daily and not if patching any other database.

libclamav.so:
- Bump version to 13:0:1 (aka 12.1.0).
- Also remove libclamav.map versioning.
  Resolves: https://github.com/Cisco-Talos/clamav/issues/1304
- Add two new API's to the public clamav.h header:
  ```c
  extern cl_error_t cl_cvdverify_ex(const char *file,
                                    const char *certs_directory);

  extern cl_error_t cl_cvdunpack_ex(const char *file,
                                    const char *dir,
                                    bool dont_verify,
                                    const char *certs_directory);
  ```
  The original `cl_cvdverify` and `cl_cvdunpack` are deprecated.
- Add `cl_engine_field` enum option `CL_ENGINE_CVDCERTSDIR`.
  You may set this option with `cl_engine_set_str` and get it
  with `cl_engine_get_str`, to override the compiled in default
  CVD certs directory.

libfreshclam.so: Bump version to 4:0:0 (aka 4.0.0).

Add sigtool sign/verify tests and test certs.

Make it so downloadFile doesn't throw a warning if the server
doesn't have the .sign file.

Replace use of md5-based FP signatures in the unit tests with
sha256-based FP signatures because the md5 implementation used
by Python may be disabled in FIPS mode.
Fixes: https://github.com/Cisco-Talos/clamav/issues/1411

CMake: Add logic to enable the Rust openssl-sys / openssl-rs crates
to build against the same OpenSSL library as is used for the C build.
The Rust unit test application must also link directly with libcrypto
and libssl.

Fix some log messages with missing new lines.

Fix missing environment variable notes in --help messages and manpages.

Deconflict CONFDIR/DATADIR/CERTSDIR variable names that are defined in
clamav-config.h.in for libclamav from variable that had the same name
for use in clamav applications that use the optparser.

The 'clamav-test' certs for the unit tests will live for 10 years.
The 'clamav-beta.crt' public cert will only live for 120 days and will
be replaced before the stable release with a production 'clamav.crt'.
2025-03-26 19:33:25 -04:00
Val Snyder
7ff29b8c37
Bump copyright dates for 2025 2025-02-14 10:24:30 -05:00
Andy Ragusa
666e047f2b
Store URLs from HTML when recording scan metadata json
Store URLs found in HTML `<a>` and `<form>` tags during scan of HTML files
when recording scan metadata.

HTML URL recording will be ON by default, but is a part of the
generate-metadata-json feature.
The generate-metadata-json feature is OFF by default.

This introduces a new general scan option:
- libclamav: `CL_SCAN_GENERAL_STORE_HTML_URLS`.
- ClamD: `JsonStoreHTMLUrls`.
- ClamScan: `--json-store-html-urls`

Thank you Matt Jolly for the helpful comment on the pull request.
2024-09-11 13:40:29 -04:00
Andy Ragusa
6d50a60560
Notify user that ole2 files are encrypted
Add keys to the metadata.json file that informs the user that a scanned
ole2 file is encrypted.  Information about the type of encryption is
provided when the information is available.  This feature co-authored by
Micah Snyder.
2024-09-10 11:50:27 -04:00
Micah Snyder
d11590f7a4
Fix unit test caused by expiring signing certificate
The clamscan test "assorted_test.py::TC::test_pe_cert_trust" is about to
fail because the "test.exe" test file was signed with a cert set to
expire after only 2 years, and it has been 23 months.

While attempting to generate a new one that will last 73000 days (200
years), I discovered that any signing certificate set to expire after
2038 will fail the trust-check because the `ca.not_after` variable is
maxed out `time_t` incapable of expressing a higher number.
To fix this, I've upgraded the variables to `uint64_t`.

I also had to replace a bunch of generated signatures to match the new
"test.exe".

Finally, I noticed that "ca.not_before" was being set to the token[8]
instead of token[9], which presumably mean the "NotBefore" field for
Trusted and Revoked Certificates was non-functional, as it was treating
the "CertSign" boolean as the "NotBefore" value.

Fixes: https://github.com/Cisco-Talos/clamav/issues/1300
2024-07-22 13:25:52 -04:00
Andy Ragusa
79f2a5f2f6 Add parser for ALZ archives 2024-04-15 10:03:02 -07:00
Micah Snyder
6e60c46114 LHA/LZH: Add clamscan tests
Added a test that verifies extraction of two specific files from a set
of LZH files created with this utility:

https://github.com/jca02266/lha
2024-04-09 10:35:22 -04:00
Micah Snyder
9cb28e51e6 Bump copyright dates for 2024 2024-01-22 11:27:17 -05:00
Micah Snyder
e389c3edac Tests: add 3 test case for OneNote 2007, 2010, and a recent webapp export 2023-12-11 15:18:41 -05:00
Micah Snyder
e4ac4b646a Test: Add test to verify ISO properly extracted w/ empty Joliet tree
Test files created with pycdlib and contain the ClamAV logo.png file.
2023-08-02 22:35:34 -07:00
Micah Snyder
6eebecc303 Bump copyright for 2023 2023-02-12 11:20:22 -08:00
Micah Snyder
dcaaf86a4b HTML <style> image extraction improvement
I found that the `url(data:` type does not matter to a browser.
In addition, whitespace may be placed in a few locations and the browser
will ignore it.

This commit accounts for this, and updates the test accordingly.
2023-02-07 22:02:02 -06:00
Micah Snyder
33eeb46b58 Test: verify clamscan detecting 2 images from same HTML style block 2023-02-07 22:02:02 -06:00
Micah Snyder
bed65c96c0 Update cert trust test after fix, and add new test
The PE cert test can be enabled now that the cert trust feature is
fixed. In so doing I found an issue with it -- it was also using the
block-certificate signature, which overrides the trust-certificate
signatures. This made me realize that we should also have a test to make
sure the block-cert signatures take predence over the trust-cert sigs.

I fixed the original sig and added this second test case.
2022-10-21 17:21:19 -07:00
Micah Snyder
798587c6b3 Tests: add pe-allmatch test set
Adds a test set authored by Andrew Williams that validates correct
allmatch behavior using as many features as possible to alert on a
test.exe program. Source for building the test.exe program is provided,
for those who are curious what it is and what it does, or in case it
needs to be re-built for some reason.

In addition to adding a test that verifies each of the sigs that should
alert, do alert, this adds a test to verify that if an authenticode
trust signature is added, none of the signatures alert.  That test is
presently failing (expected failure, so the tests all pass) and should
be updated when the certificate verification bug is fixed.
2022-10-19 13:13:57 -07:00
Micah Snyder
6b1d93fcf5 Tests: add allmatch regression tests
Add tests:
- Test an import hash with wildcard size when all-match mode is
disabled.
- Test that bytecode rules will run after content match alerts in
all-match mode.
2022-10-19 13:13:57 -07:00
Micah Snyder
d788844cd0 Test: Add basic LDB Container & Intermediates tests
Add basic tests for the Container and Intermediates logical signature
features.

The Intermediates test verifies that a text file containing:
  v1rusv1rus
and wrapped in a 7z and then zip archives can be correctly detected when
Intermediates is set to: CL_TYPE_7Z>CL_TYPE_ZIP

The Container test just checks the container is CL_TYPE_ZIP
2022-07-19 16:44:32 -07:00
micasnyd
140c88aa4e Bump copyright for 2022
Includes minor format corrections.
2022-01-09 14:23:25 -07:00
Alexander Sulfrian
c5c3b7558e CMake: Fix race condition with parallel builds
If running multiple parallel processes of "xor_testfile.py" there was a
race condition between checking for the existence of the directory and
creating it. Now this is handled as a dependency in CMake.
2021-09-27 13:03:24 -07:00
Micah Snyder
4b400b9b1e Test: Verify that pdf bytecode hooks execute 2021-07-19 14:47:25 -07:00
Micah Snyder
b406e7e4d6 Add feature test for XLS image (JPG & PNG) extraction
Added a test to verify that clamscan can extract images from an XLS
document. The document has 2 images: a PNG and JPEG version of the
clamav demon/logo. The test requires the json metadata feature to verify
that the MD5 of the images are correct.

No other image formats were tested because despite the format allegedly
supporting other imate formats, Excel converts TIFF, BMP, and GIF images
to PNG files when you insert them.
2021-07-17 10:39:27 -07:00
Micah Snyder
201e1b12a7 XOR test files; clean up tests directory
The split test files are flagged by some AV's because they look like
broken executables. Instead of splitting the test files to prevent
detections, we should encrypt them. This commit replaces the "reassemble
testfiles" script with a basic "XOR testfiles" script that can be used
to encrypt or decrypt test files. This commit also of course then
replaces all the split files with xor'ed files.

The test and unit_tests directories were a bit of a mess, so I
reorganized them all into unit_tests with all of the test files placed
under "unit_tests/input" using subdirectories for different types of files.
2021-07-17 10:39:27 -07:00
Andrew
319bfb51a5 Fix several coverity warnings
290424 Missing break in switch - In hash_match: Missing break
statement between cases in switch statement

290414 Resource leak - In cli_scanishield_msi: Leak of memory or
pointers to system resources. Memory leak in a fail case

288197 Resource leak - In decrypt_any: Leak of memory or pointers
to system resources. Memory leak in a fail case

290426 Resource leak - In cli_magic_scan: Leak of memory or pointers
to system resources. Leaked a file prefix when running with
--save-temps

192923 Resource leak - In cli_scanrar: Leak of memory or pointers to
system resources. Leaked a file descriptor if a virus was found in
a RAR file comment

225146 Resource leak - In cli_scanegg: Leak of memory or pointers
to system resources. Leaked a file descriptor if unable to write
a comment file to disk

290425 Resource leak - In scan_common: Leak of memory or pointers
to system resources. Memory leaks in various fail cases.

Also changes cli_scanrar to write out the file comment only if
--leave-temps is specified and scan the buffer (like what is done
in cli_scanegg) instead of writing the file out, scanning that,
and then deleting the file if --leave-temps is not specified.

The unit tests stopped working when correcting an issue with a
switch statement that determined what type of signature had matched
on a Google SafeBrowsing GDB rule. Looking into the unit tests, it
looks like the code had always assumed that the test cases would be
detected by a malware test rule in unit_tests/input/daily.gdb, but
now some of the tests get matched on the phishing test rule.
I updated the test logic to be more clear, and added tests for both
cases now.

Fix some memory leaks in libclamav/scanners.c
2020-07-15 08:39:32 -07:00
Micah Snyder (micasnyd)
9c58ba7bd7 Update to clamav-devel to synchronize with the clamav-bytecode-compiler project. 2019-10-02 16:08:24 -04:00
Török Edvin
d5f7afdded testcase for cl_scan APIs
(partially ported from fmapify branch)
2011-06-15 12:22:27 +03:00
Török Edvin
1ab57a63c7 Add bytecode.cvd load test. 2010-05-14 17:19:26 +03:00
Török Edvin
7c394b5a9d Update these tests. 2010-05-14 11:19:09 +03:00
Török Edvin
8a06c645ad Fix unit test. 2010-05-14 11:04:04 +03:00
Török Edvin
3d2808c218 bytecode: update unit tests for improved arithmetic test. 2010-05-14 10:41:50 +03:00
Török Edvin
fc01c6476f Fix interpreter. 2010-05-13 23:25:11 +03:00
Török Edvin
a969167b6c Add new bytecode API unit tests. 2010-05-13 22:44:29 +03:00
Török Edvin
08d7e5f07d Restore the previous cbc testfiles. 2010-05-12 23:56:12 +03:00
Török Edvin
7a7365efe9 0.96.1 new APIs (cli_map etc.) 2010-05-12 23:51:20 +03:00
Török Edvin
d772904022 Fix matchwithread.cbc
ImageBase is little-endian, need to use conversion
function to access it.
2010-04-02 13:13:17 +03:00
Török Edvin
1bef6a803d Update pdf.cbc. 2010-03-30 00:04:38 +03:00
Török Edvin
1678ef9e43 Fix inflate.cbc for the interpreter. 2010-03-29 11:38:52 +03:00
Török Edvin
e439954b51 Fix valgrind warnings. 2010-03-24 17:37:23 +02:00
Török Edvin
778df8c22f Fix more leaks. 2010-03-24 17:08:20 +02:00
Török Edvin
6ea339aeab Fix bswap. 2010-03-24 15:27:15 +02:00
Török Edvin
48fc8b9852 Leak testcase. 2010-03-24 14:14:33 +02:00
Török Edvin
b26d43809a Add matchwithread.cbc to unit tests. 2010-03-24 12:46:34 +02:00
Török Edvin
b63681a52b Introduce BytecodeTimeout. 2010-03-22 17:16:07 +02:00
Török Edvin
353dafc9c4 Update unit tests. 2010-03-22 11:18:28 +02:00