2004-09-30 00:26:52 +00:00
|
|
|
/*
|
2025-02-14 10:24:30 -05:00
|
|
|
* Copyright (C) 2013-2025 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
|
2019-01-25 10:15:50 -05:00
|
|
|
* Copyright (C) 2007-2013 Sourcefire, Inc.
|
2008-04-02 15:24:51 +00:00
|
|
|
*
|
2008-12-02 19:55:57 +00:00
|
|
|
* Authors: Trog, Török Edvin
|
2004-09-30 00:26:52 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2008-04-02 15:24:51 +00:00
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
2004-09-30 00:26:52 +00:00
|
|
|
*
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-04-09 19:59:28 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
2004-09-30 00:26:52 +00:00
|
|
|
*/
|
|
|
|
|
2009-09-24 16:08:52 +02:00
|
|
|
#if HAVE_CONFIG_H
|
2005-02-05 15:50:18 +00:00
|
|
|
#include "clamav-config.h"
|
2009-09-24 16:08:52 +02:00
|
|
|
#endif
|
2005-02-05 15:50:18 +00:00
|
|
|
|
2004-09-30 00:26:52 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2018-12-03 12:40:13 -05:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-09-30 00:26:52 +00:00
|
|
|
#include <unistd.h>
|
2006-10-09 15:23:50 +00:00
|
|
|
#endif
|
2005-02-05 15:50:18 +00:00
|
|
|
#include <string.h>
|
2008-12-02 19:55:57 +00:00
|
|
|
#include <ctype.h>
|
2009-09-25 15:16:21 +02:00
|
|
|
#ifndef _WIN32
|
2009-09-25 00:09:13 +02:00
|
|
|
#include <netinet/in.h>
|
2009-09-25 15:16:21 +02:00
|
|
|
#endif
|
2014-02-08 00:31:12 -05:00
|
|
|
|
2004-09-30 00:26:52 +00:00
|
|
|
#include "clamav.h"
|
|
|
|
#include "others.h"
|
2005-12-10 18:50:26 +00:00
|
|
|
#include "special.h"
|
2009-04-23 13:24:21 +00:00
|
|
|
#include "matcher.h"
|
2004-09-30 00:26:52 +00:00
|
|
|
|
2005-03-10 13:34:30 +00:00
|
|
|
/* NOTE: Photoshop stores data in BIG ENDIAN format, this is the opposite
|
2022-02-16 00:13:55 +01:00
|
|
|
to virtually everything else */
|
2005-03-10 13:34:30 +00:00
|
|
|
|
2006-04-07 23:31:41 +00:00
|
|
|
#define special_endian_convert_16(v) be16_to_host(v)
|
|
|
|
#define special_endian_convert_32(v) be32_to_host(v)
|
2005-03-10 13:34:30 +00:00
|
|
|
|
2011-06-15 12:00:23 +02:00
|
|
|
int cli_check_mydoom_log(cli_ctx *ctx)
|
2004-09-30 00:26:52 +00:00
|
|
|
{
|
2024-06-26 23:01:47 +02:00
|
|
|
uint32_t record[16];
|
|
|
|
const uint32_t *ptr;
|
2018-12-03 12:40:13 -05:00
|
|
|
uint32_t check, key;
|
libclamav: Fix scan recursion tracking
Scan recursion is the process of identifying files embedded in other
files and then scanning them, recursively.
Internally this process is more complex than it may sound because a file
may have multiple layers of types before finding a new "file".
At present we treat the recursion count in the scanning context as an
index into both our fmap list AND our container list. These two lists
are conceptually a part of the same thing and should be unified.
But what's concerning is that the "recursion level" isn't actually
incremented or decremented at the same time that we add a layer to the
fmap or container lists but instead is more touchy-feely, increasing
when we find a new "file".
To account for this shadiness, the size of the fmap and container lists
has always been a little longer than our "max scan recursion" limit so
we don't accidentally overflow the fmap or container arrays (!).
I've implemented a single recursion-stack as an array, similar to before,
which includes a pointer to each fmap at each layer, along with the size
and type. Push and pop functions add and remove layers whenever a new
fmap is added. A boolean argument when pushing indicates if the new layer
represents a new buffer or new file (descriptor). A new buffer will reset
the "nested fmap level" (described below).
This commit also provides a solution for an issue where we detect
embedded files more than once during scan recursion.
For illustration, imagine a tarball named foo.tar.gz with this structure:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| └── baz.exe | PE | 2 | 1 |
But suppose baz.exe embeds a ZIP archive and a 7Z archive, like this:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| baz.exe | PE | 0 | 0 |
| ├── sfx.zip | ZIP | 1 | 1 |
| │ └── hello.txt | ASCII | 2 | 0 |
| └── sfx.7z | 7Z | 1 | 1 |
| └── world.txt | ASCII | 2 | 0 |
(A) If we scan for embedded files at any layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| ├── foo.tar | TAR | 1 | 0 |
| │ ├── bar.zip | ZIP | 2 | 1 |
| │ │ └── hola.txt | ASCII | 3 | 0 |
| │ ├── baz.exe | PE | 2 | 1 |
| │ │ ├── sfx.zip | ZIP | 3 | 1 |
| │ │ │ └── hello.txt | ASCII | 4 | 0 |
| │ │ └── sfx.7z | 7Z | 3 | 1 |
| │ │ └── world.txt | ASCII | 4 | 0 |
| │ ├── sfx.zip | ZIP | 2 | 1 |
| │ │ └── hello.txt | ASCII | 3 | 0 |
| │ └── sfx.7z | 7Z | 2 | 1 |
| │ └── world.txt | ASCII | 3 | 0 |
| ├── sfx.zip | ZIP | 1 | 1 |
| └── sfx.7z | 7Z | 1 | 1 |
(A) is bad because it scans content more than once.
Note that for the GZ layer, it may detect the ZIP and 7Z if the
signature hits on the compressed data, which it might, though
extracting the ZIP and 7Z will likely fail.
The reason the above doesn't happen now is that we restrict embedded
type scans for a bunch of archive formats to include GZ and TAR.
(B) If we scan for embedded files at the foo.tar layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| ├── baz.exe | PE | 2 | 1 |
| ├── sfx.zip | ZIP | 2 | 1 |
| │ └── hello.txt | ASCII | 3 | 0 |
| └── sfx.7z | 7Z | 2 | 1 |
| └── world.txt | ASCII | 3 | 0 |
(B) is almost right. But we can achieve it easily enough only scanning for
embedded content in the current fmap when the "nested fmap level" is 0.
The upside is that it should safely detect all embedded content, even if
it may think the sfz.zip and sfx.7z are in foo.tar instead of in baz.exe.
The biggest risk I can think of affects ZIPs. SFXZIP detection
is identical to ZIP detection, which is why we don't allow SFXZIP to be
detected if insize of a ZIP. If we only allow embedded type scanning at
fmap-layer 0 in each buffer, this will fail to detect the embedded ZIP
if the bar.exe was not compressed in foo.zip and if non-compressed files
extracted from ZIPs aren't extracted as new buffers:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.zip | ZIP | 0 | 0 |
| └── bar.exe | PE | 1 | 1 |
| └── sfx.zip | ZIP | 2 | 2 |
Provided that we ensure all files extracted from zips are scanned in
new buffers, option (B) should be safe.
(C) If we scan for embedded files at the baz.exe layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| └── baz.exe | PE | 2 | 1 |
| ├── sfx.zip | ZIP | 3 | 1 |
| │ └── hello.txt | ASCII | 4 | 0 |
| └── sfx.7z | 7Z | 3 | 1 |
| └── world.txt | ASCII | 4 | 0 |
(C) is right. But it's harder to achieve. For this example we can get it by
restricting 7ZSFX and ZIPSFX detection only when scanning an executable.
But that may mean losing detection of archives embedded elsewhere.
And we'd have to identify allowable container types for each possible
embedded type, which would be very difficult.
So this commit aims to solve the issue the (B)-way.
Note that in all situations, we still have to scan with file typing
enabled to determine if we need to reassign the current file type, such
as re-identifying a Bzip2 archive as a DMG that happens to be Bzip2-
compressed. Detection of DMG and a handful of other types rely on
finding data partway through or near the ned of a file before
reassigning the entire file as the new type.
Other fixes and considerations in this commit:
- The utf16 HTML parser has weak error handling, particularly with respect
to creating a nested fmap for scanning the ascii decoded file.
This commit cleans up the error handling and wraps the nested scan with
the recursion-stack push()/pop() for correct recursion tracking.
Before this commit, each container layer had a flag to indicate if the
container layer is valid.
We need something similar so that the cli_recursion_stack_get_*()
functions ignore normalized layers. Details...
Imagine an LDB signature for HTML content that specifies a ZIP
container. If the signature actually alerts on the normalized HTML and
you don't ignore normalized layers for the container check, it will
appear as though the alert is in an HTML container rather than a ZIP
container.
This commit accomplishes this with a boolean you set in the scan context
before scanning a new layer. Then when the new fmap is created, it will
use that flag to set similar flag for the layer. The context flag is
reset those that anything after this doesn't have that flag.
The flag allows the new recursion_stack_get() function to ignore
normalized layers when iterating the stack to return a layer at a
requested index, negative or positive.
Scanning normalized extracted/normalized javascript and VBA should also
use the 'layer is normalized' flag.
- This commit also fixes Heuristic.Broken.Executable alert for ELF files
to make sure that:
A) these only alert if cli_append_virus() returns CL_VIRUS (aka it
respects the FP check).
B) all broken-executable alerts for ELF only happen if the
SCAN_HEURISTIC_BROKEN option is enabled.
- This commit also cleans up the error handling in cli_magic_scan_dir().
This was needed so we could correctly apply the layer-is-normalized-flag
to all VBA macros extracted to a directory when scanning the directory.
- Also fix an issue where exceeding scan maximums wouldn't cause embedded
file detection scans to abort. Granted we don't actually want to abort
if max filesize or max recursion depth are exceeded... only if max
scansize, max files, and max scantime are exceeded.
Add 'abort_scan' flag to scan context, to protect against depending on
correct error propagation for fatal conditions. Instead, setting this
flag in the scan context should guarantee that a fatal condition deep in
scan recursion isn't lost which result in more stuff being scanned
instead of aborting. This shouldn't be necessary, but some status codes
like CL_ETIMEOUT never used to be fatal and it's easier to do this than
to verify every parser only returns CL_ETIMEOUT and other "fatal
status codes" in fatal conditions.
- Remove duplicate is_tar() prototype from filestypes.c and include
is_tar.h instead.
- Presently we create the fmap hash when creating the fmap.
This wastes a bit of CPU if the hash is never needed.
Now that we're creating fmap's for all embedded files discovered with
file type recognition scans, this is a much more frequent occurence and
really slows things down.
This commit fixes the issue by only creating fmap hashes as needed.
This should not only resolve the perfomance impact of creating fmap's
for all embedded files, but also should improve performance in general.
- Add allmatch check to the zip parser after the central-header meta
match. That way we don't multiple alerts with the same match except in
allmatch mode. Clean up error handling in the zip parser a tiny bit.
- Fixes to ensure that the scan limits such as scansize, filesize,
recursion depth, # of embedded files, and scantime are always reported
if AlertExceedsMax (--alert-exceeds-max) is enabled.
- Fixed an issue where non-fatal alerts for exceeding scan maximums may
mask signature matches later on. I changed it so these alerts use the
"possibly unwanted" alert-type and thus only alert if no other alerts
were found or if all-match or heuristic-precedence are enabled.
- Added the "Heuristics.Limits.Exceeded.*" events to the JSON metadata
when the --gen-json feature is enabled. These will show up once under
"ParseErrors" the first time a limit is exceeded. In the present
implementation, only one limits-exceeded events will be added, so as to
prevent a malicious or malformed sample from filling the JSON buffer
with millions of events and using a tonne of RAM.
2021-09-11 14:15:21 -07:00
|
|
|
fmap_t *map = ctx->fmap;
|
2018-12-03 12:40:13 -05:00
|
|
|
unsigned int blocks = map->len / (8 * 4);
|
2004-09-30 00:26:52 +00:00
|
|
|
|
|
|
|
cli_dbgmsg("in cli_check_mydoom_log()\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
if (blocks < 2)
|
|
|
|
return CL_CLEAN;
|
|
|
|
if (blocks > 5)
|
|
|
|
blocks = 5;
|
|
|
|
|
2024-06-26 23:01:47 +02:00
|
|
|
/*
|
|
|
|
* The following pointer might not be properly aligned. There there is
|
|
|
|
* memcmp() + memcpy() workaround to avoid performing an unaligned access
|
|
|
|
* while reading the uint32_t.
|
|
|
|
*/
|
|
|
|
ptr = fmap_need_off_once(map, 0, 8 * 4 * blocks);
|
|
|
|
if (!ptr)
|
2018-12-03 12:40:13 -05:00
|
|
|
return CL_CLEAN;
|
2024-06-26 23:01:47 +02:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
while (blocks) { /* This wasn't probably intended but that's what the current code does anyway */
|
2024-06-26 23:01:47 +02:00
|
|
|
const uint32_t marker_ff = 0xffffffff;
|
|
|
|
|
2024-09-09 12:46:33 -04:00
|
|
|
if (!memcmp(&ptr[--blocks], &marker_ff, sizeof(uint32_t)))
|
2018-12-03 12:40:13 -05:00
|
|
|
return CL_CLEAN;
|
2004-09-30 00:26:52 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 23:01:47 +02:00
|
|
|
memcpy(record, ptr, sizeof(record));
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
key = ~be32_to_host(record[0]);
|
|
|
|
check = (be32_to_host(record[1]) ^ key) +
|
|
|
|
(be32_to_host(record[2]) ^ key) +
|
|
|
|
(be32_to_host(record[3]) ^ key) +
|
|
|
|
(be32_to_host(record[4]) ^ key) +
|
|
|
|
(be32_to_host(record[5]) ^ key) +
|
|
|
|
(be32_to_host(record[6]) ^ key) +
|
|
|
|
(be32_to_host(record[7]) ^ key);
|
2011-06-15 12:00:23 +02:00
|
|
|
if ((~check) != key)
|
2018-12-03 12:40:13 -05:00
|
|
|
return CL_CLEAN;
|
|
|
|
|
|
|
|
key = ~be32_to_host(record[8]);
|
|
|
|
check = (be32_to_host(record[9]) ^ key) +
|
|
|
|
(be32_to_host(record[10]) ^ key) +
|
|
|
|
(be32_to_host(record[11]) ^ key) +
|
|
|
|
(be32_to_host(record[12]) ^ key) +
|
|
|
|
(be32_to_host(record[13]) ^ key) +
|
|
|
|
(be32_to_host(record[14]) ^ key) +
|
|
|
|
(be32_to_host(record[15]) ^ key);
|
2011-06-15 12:00:23 +02:00
|
|
|
if ((~check) != key)
|
2018-12-03 12:40:13 -05:00
|
|
|
return CL_CLEAN;
|
2004-09-30 00:26:52 +00:00
|
|
|
|
2022-08-19 16:21:42 -07:00
|
|
|
return cli_append_potentially_unwanted(ctx, "Heuristics.Worm.Mydoom.M.log");
|
2004-09-30 00:26:52 +00:00
|
|
|
}
|
|
|
|
|
2005-02-05 09:41:35 +00:00
|
|
|
static uint32_t riff_endian_convert_32(uint32_t value, int big_endian)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
if (big_endian)
|
|
|
|
return be32_to_host(value);
|
|
|
|
else
|
|
|
|
return le32_to_host(value);
|
2005-02-05 09:41:35 +00:00
|
|
|
}
|
2005-02-04 10:04:09 +00:00
|
|
|
|
2011-06-15 20:50:42 +02:00
|
|
|
static int riff_read_chunk(fmap_t *map, off_t *offset, int big_endian, int rec_level)
|
2005-02-04 10:04:09 +00:00
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
uint32_t cache_buf;
|
|
|
|
char *buffer;
|
|
|
|
const uint32_t *buf;
|
|
|
|
uint32_t chunk_size;
|
|
|
|
off_t cur_offset = *offset;
|
|
|
|
|
|
|
|
if (rec_level > 1000) {
|
|
|
|
cli_dbgmsg("riff_read_chunk: recursion level exceeded\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2005-02-04 10:04:09 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!(buf = fmap_need_off_once(map, cur_offset, 4 * 2)))
|
|
|
|
return 0;
|
|
|
|
cur_offset += 4 * 2;
|
2015-12-09 12:15:16 -05:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
buffer = (char *)buf;
|
|
|
|
memcpy(&cache_buf, buffer + sizeof(cache_buf),
|
|
|
|
sizeof(cache_buf));
|
|
|
|
chunk_size = riff_endian_convert_32(cache_buf, big_endian);
|
2011-06-15 20:50:42 +02:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!memcmp(buf, "anih", 4) && chunk_size != 36)
|
|
|
|
return 2;
|
2010-06-16 16:54:32 +02:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (memcmp(buf, "RIFF", 4) == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (memcmp(buf, "RIFX", 4) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((memcmp(buf, "LIST", 4) == 0) ||
|
|
|
|
(memcmp(buf, "PROP", 4) == 0) ||
|
|
|
|
(memcmp(buf, "FORM", 4) == 0) ||
|
|
|
|
(memcmp(buf, "CAT ", 4) == 0)) {
|
|
|
|
if (!fmap_need_ptr_once(map, buf + 2, 4)) {
|
|
|
|
cli_dbgmsg("riff_read_chunk: read list type failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*offset = cur_offset + 4;
|
|
|
|
return riff_read_chunk(map, offset, big_endian, ++rec_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
*offset = cur_offset + chunk_size + (chunk_size & 1);
|
|
|
|
if (*offset < cur_offset) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* FIXME: WTF!?
|
2022-02-16 00:13:55 +01:00
|
|
|
if (lseek(fd, offset, SEEK_SET) != offset) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
*/
|
2018-12-03 12:40:13 -05:00
|
|
|
return 1;
|
2005-02-04 10:04:09 +00:00
|
|
|
}
|
|
|
|
|
2011-06-15 20:50:42 +02:00
|
|
|
int cli_check_riff_exploit(cli_ctx *ctx)
|
2005-02-04 10:04:09 +00:00
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
const uint32_t *buf;
|
|
|
|
int big_endian, retval;
|
|
|
|
off_t offset;
|
libclamav: Fix scan recursion tracking
Scan recursion is the process of identifying files embedded in other
files and then scanning them, recursively.
Internally this process is more complex than it may sound because a file
may have multiple layers of types before finding a new "file".
At present we treat the recursion count in the scanning context as an
index into both our fmap list AND our container list. These two lists
are conceptually a part of the same thing and should be unified.
But what's concerning is that the "recursion level" isn't actually
incremented or decremented at the same time that we add a layer to the
fmap or container lists but instead is more touchy-feely, increasing
when we find a new "file".
To account for this shadiness, the size of the fmap and container lists
has always been a little longer than our "max scan recursion" limit so
we don't accidentally overflow the fmap or container arrays (!).
I've implemented a single recursion-stack as an array, similar to before,
which includes a pointer to each fmap at each layer, along with the size
and type. Push and pop functions add and remove layers whenever a new
fmap is added. A boolean argument when pushing indicates if the new layer
represents a new buffer or new file (descriptor). A new buffer will reset
the "nested fmap level" (described below).
This commit also provides a solution for an issue where we detect
embedded files more than once during scan recursion.
For illustration, imagine a tarball named foo.tar.gz with this structure:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| └── baz.exe | PE | 2 | 1 |
But suppose baz.exe embeds a ZIP archive and a 7Z archive, like this:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| baz.exe | PE | 0 | 0 |
| ├── sfx.zip | ZIP | 1 | 1 |
| │ └── hello.txt | ASCII | 2 | 0 |
| └── sfx.7z | 7Z | 1 | 1 |
| └── world.txt | ASCII | 2 | 0 |
(A) If we scan for embedded files at any layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| ├── foo.tar | TAR | 1 | 0 |
| │ ├── bar.zip | ZIP | 2 | 1 |
| │ │ └── hola.txt | ASCII | 3 | 0 |
| │ ├── baz.exe | PE | 2 | 1 |
| │ │ ├── sfx.zip | ZIP | 3 | 1 |
| │ │ │ └── hello.txt | ASCII | 4 | 0 |
| │ │ └── sfx.7z | 7Z | 3 | 1 |
| │ │ └── world.txt | ASCII | 4 | 0 |
| │ ├── sfx.zip | ZIP | 2 | 1 |
| │ │ └── hello.txt | ASCII | 3 | 0 |
| │ └── sfx.7z | 7Z | 2 | 1 |
| │ └── world.txt | ASCII | 3 | 0 |
| ├── sfx.zip | ZIP | 1 | 1 |
| └── sfx.7z | 7Z | 1 | 1 |
(A) is bad because it scans content more than once.
Note that for the GZ layer, it may detect the ZIP and 7Z if the
signature hits on the compressed data, which it might, though
extracting the ZIP and 7Z will likely fail.
The reason the above doesn't happen now is that we restrict embedded
type scans for a bunch of archive formats to include GZ and TAR.
(B) If we scan for embedded files at the foo.tar layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| ├── baz.exe | PE | 2 | 1 |
| ├── sfx.zip | ZIP | 2 | 1 |
| │ └── hello.txt | ASCII | 3 | 0 |
| └── sfx.7z | 7Z | 2 | 1 |
| └── world.txt | ASCII | 3 | 0 |
(B) is almost right. But we can achieve it easily enough only scanning for
embedded content in the current fmap when the "nested fmap level" is 0.
The upside is that it should safely detect all embedded content, even if
it may think the sfz.zip and sfx.7z are in foo.tar instead of in baz.exe.
The biggest risk I can think of affects ZIPs. SFXZIP detection
is identical to ZIP detection, which is why we don't allow SFXZIP to be
detected if insize of a ZIP. If we only allow embedded type scanning at
fmap-layer 0 in each buffer, this will fail to detect the embedded ZIP
if the bar.exe was not compressed in foo.zip and if non-compressed files
extracted from ZIPs aren't extracted as new buffers:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.zip | ZIP | 0 | 0 |
| └── bar.exe | PE | 1 | 1 |
| └── sfx.zip | ZIP | 2 | 2 |
Provided that we ensure all files extracted from zips are scanned in
new buffers, option (B) should be safe.
(C) If we scan for embedded files at the baz.exe layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │ └── hola.txt | ASCII | 3 | 0 |
| └── baz.exe | PE | 2 | 1 |
| ├── sfx.zip | ZIP | 3 | 1 |
| │ └── hello.txt | ASCII | 4 | 0 |
| └── sfx.7z | 7Z | 3 | 1 |
| └── world.txt | ASCII | 4 | 0 |
(C) is right. But it's harder to achieve. For this example we can get it by
restricting 7ZSFX and ZIPSFX detection only when scanning an executable.
But that may mean losing detection of archives embedded elsewhere.
And we'd have to identify allowable container types for each possible
embedded type, which would be very difficult.
So this commit aims to solve the issue the (B)-way.
Note that in all situations, we still have to scan with file typing
enabled to determine if we need to reassign the current file type, such
as re-identifying a Bzip2 archive as a DMG that happens to be Bzip2-
compressed. Detection of DMG and a handful of other types rely on
finding data partway through or near the ned of a file before
reassigning the entire file as the new type.
Other fixes and considerations in this commit:
- The utf16 HTML parser has weak error handling, particularly with respect
to creating a nested fmap for scanning the ascii decoded file.
This commit cleans up the error handling and wraps the nested scan with
the recursion-stack push()/pop() for correct recursion tracking.
Before this commit, each container layer had a flag to indicate if the
container layer is valid.
We need something similar so that the cli_recursion_stack_get_*()
functions ignore normalized layers. Details...
Imagine an LDB signature for HTML content that specifies a ZIP
container. If the signature actually alerts on the normalized HTML and
you don't ignore normalized layers for the container check, it will
appear as though the alert is in an HTML container rather than a ZIP
container.
This commit accomplishes this with a boolean you set in the scan context
before scanning a new layer. Then when the new fmap is created, it will
use that flag to set similar flag for the layer. The context flag is
reset those that anything after this doesn't have that flag.
The flag allows the new recursion_stack_get() function to ignore
normalized layers when iterating the stack to return a layer at a
requested index, negative or positive.
Scanning normalized extracted/normalized javascript and VBA should also
use the 'layer is normalized' flag.
- This commit also fixes Heuristic.Broken.Executable alert for ELF files
to make sure that:
A) these only alert if cli_append_virus() returns CL_VIRUS (aka it
respects the FP check).
B) all broken-executable alerts for ELF only happen if the
SCAN_HEURISTIC_BROKEN option is enabled.
- This commit also cleans up the error handling in cli_magic_scan_dir().
This was needed so we could correctly apply the layer-is-normalized-flag
to all VBA macros extracted to a directory when scanning the directory.
- Also fix an issue where exceeding scan maximums wouldn't cause embedded
file detection scans to abort. Granted we don't actually want to abort
if max filesize or max recursion depth are exceeded... only if max
scansize, max files, and max scantime are exceeded.
Add 'abort_scan' flag to scan context, to protect against depending on
correct error propagation for fatal conditions. Instead, setting this
flag in the scan context should guarantee that a fatal condition deep in
scan recursion isn't lost which result in more stuff being scanned
instead of aborting. This shouldn't be necessary, but some status codes
like CL_ETIMEOUT never used to be fatal and it's easier to do this than
to verify every parser only returns CL_ETIMEOUT and other "fatal
status codes" in fatal conditions.
- Remove duplicate is_tar() prototype from filestypes.c and include
is_tar.h instead.
- Presently we create the fmap hash when creating the fmap.
This wastes a bit of CPU if the hash is never needed.
Now that we're creating fmap's for all embedded files discovered with
file type recognition scans, this is a much more frequent occurence and
really slows things down.
This commit fixes the issue by only creating fmap hashes as needed.
This should not only resolve the perfomance impact of creating fmap's
for all embedded files, but also should improve performance in general.
- Add allmatch check to the zip parser after the central-header meta
match. That way we don't multiple alerts with the same match except in
allmatch mode. Clean up error handling in the zip parser a tiny bit.
- Fixes to ensure that the scan limits such as scansize, filesize,
recursion depth, # of embedded files, and scantime are always reported
if AlertExceedsMax (--alert-exceeds-max) is enabled.
- Fixed an issue where non-fatal alerts for exceeding scan maximums may
mask signature matches later on. I changed it so these alerts use the
"possibly unwanted" alert-type and thus only alert if no other alerts
were found or if all-match or heuristic-precedence are enabled.
- Added the "Heuristics.Limits.Exceeded.*" events to the JSON metadata
when the --gen-json feature is enabled. These will show up once under
"ParseErrors" the first time a limit is exceeded. In the present
implementation, only one limits-exceeded events will be added, so as to
prevent a malicious or malformed sample from filling the JSON buffer
with millions of events and using a tonne of RAM.
2021-09-11 14:15:21 -07:00
|
|
|
fmap_t *map = ctx->fmap;
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
cli_dbgmsg("in cli_check_riff_exploit()\n");
|
|
|
|
|
|
|
|
if (!(buf = fmap_need_off_once(map, 0, 4 * 3)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (memcmp(buf, "RIFF", 4) == 0) {
|
|
|
|
big_endian = FALSE;
|
|
|
|
} else if (memcmp(buf, "RIFX", 4) == 0) {
|
|
|
|
big_endian = TRUE;
|
|
|
|
} else {
|
|
|
|
/* Not a RIFF file */
|
|
|
|
return 0;
|
|
|
|
}
|
2005-02-04 10:04:09 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (memcmp(&buf[2], "ACON", 4) != 0) {
|
|
|
|
/* Only scan MS animated icon files */
|
|
|
|
/* There is a *lot* of broken software out there that produces bad RIFF files */
|
|
|
|
return 0;
|
|
|
|
}
|
2005-02-08 14:47:06 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
offset = 4 * 3;
|
|
|
|
do {
|
|
|
|
retval = riff_read_chunk(map, &offset, big_endian, 1);
|
|
|
|
} while (retval == 1);
|
2005-02-04 10:04:09 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
return retval;
|
2005-02-04 10:04:09 +00:00
|
|
|
}
|
2008-12-02 19:55:57 +00:00
|
|
|
|
|
|
|
static inline int swizz_j48(const uint16_t n[])
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("swizz_j48: %u, %u, %u\n", n[0], n[1], n[2]);
|
|
|
|
/* rules based on J48 tree */
|
|
|
|
if (n[0] <= 961 || !n[1])
|
|
|
|
return 0;
|
|
|
|
if (n[0] <= 1006)
|
|
|
|
return (n[2] > 0 && n[2] <= 6);
|
|
|
|
else
|
|
|
|
return n[1] <= 10 && n[2];
|
2008-12-02 19:55:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli_detect_swizz_str(const unsigned char *str, uint32_t len, struct swizz_stats *stats, int blob)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
unsigned char stri[4096];
|
|
|
|
size_t i, j = 0;
|
|
|
|
int bad = 0;
|
|
|
|
int lastalnum = 0;
|
|
|
|
uint8_t ngrams[17576];
|
|
|
|
uint16_t all = 0;
|
|
|
|
uint16_t ngram_cnts[3];
|
|
|
|
uint16_t words = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
stats->entries++;
|
|
|
|
for (i = 0; (i < (size_t)len - 1) && (j < sizeof(stri) - 2); i += 2) {
|
|
|
|
unsigned char c = str[i];
|
|
|
|
if (str[i + 1] || !c) {
|
|
|
|
bad++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!isalnum(c)) {
|
|
|
|
if (!lastalnum)
|
|
|
|
continue;
|
|
|
|
lastalnum = 0;
|
|
|
|
c = ' ';
|
|
|
|
} else {
|
|
|
|
lastalnum = 1;
|
|
|
|
if (isdigit(c))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
stri[j++] = tolower(c);
|
|
|
|
}
|
|
|
|
stri[j++] = '\0';
|
|
|
|
if ((!blob && (bad >= 8)) || j < 4)
|
|
|
|
return;
|
|
|
|
memset(ngrams, 0, sizeof(ngrams));
|
|
|
|
memset(ngram_cnts, 0, sizeof(ngram_cnts));
|
|
|
|
for (i = 0; i < j - 2; i++) {
|
|
|
|
if (stri[i] != ' ' && stri[i + 1] != ' ' && stri[i + 2] != ' ') {
|
|
|
|
uint16_t idx = (stri[i] - 'a') * 676 + (stri[i + 1] - 'a') * 26 + (stri[i + 2] - 'a');
|
|
|
|
if (idx < sizeof(ngrams)) {
|
|
|
|
ngrams[idx]++;
|
|
|
|
stats->gngrams[idx]++;
|
|
|
|
}
|
|
|
|
} else if (stri[i] == ' ')
|
|
|
|
words++;
|
|
|
|
}
|
|
|
|
for (i = 0; i < sizeof(ngrams); i++) {
|
|
|
|
uint8_t v = ngrams[i];
|
|
|
|
if (v > 3) v = 3;
|
|
|
|
if (v) {
|
|
|
|
ngram_cnts[v - 1]++;
|
|
|
|
all++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!all)
|
|
|
|
return;
|
|
|
|
cli_dbgmsg("cli_detect_swizz_str: %u, %u, %u\n", ngram_cnts[0], ngram_cnts[1], ngram_cnts[2]);
|
|
|
|
/* normalize */
|
|
|
|
for (i = 0; i < sizeof(ngram_cnts) / sizeof(ngram_cnts[0]); i++) {
|
|
|
|
uint32_t v = ngram_cnts[i];
|
|
|
|
ngram_cnts[i] = (v << 10) / all;
|
|
|
|
}
|
|
|
|
ret = swizz_j48(ngram_cnts) ? CL_VIRUS : CL_CLEAN;
|
|
|
|
if (words < 3) ret = CL_CLEAN;
|
|
|
|
cli_dbgmsg("cli_detect_swizz_str: %s, %u words\n", ret == CL_VIRUS ? "suspicious" : "ok", words);
|
|
|
|
if (ret == CL_VIRUS) {
|
|
|
|
stats->suspicious += j;
|
|
|
|
cli_dbgmsg("cli_detect_swizz_str: %s\n", stri);
|
|
|
|
}
|
|
|
|
stats->total += j;
|
2008-12-02 19:55:57 +00:00
|
|
|
}
|
|
|
|
|
2008-12-07 09:55:00 +00:00
|
|
|
static inline int swizz_j48_global(const uint32_t gn[])
|
2008-12-06 14:49:00 +00:00
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
if (gn[0] <= 24185) {
|
|
|
|
return gn[0] > 22980 && gn[8] > 0 && gn[8] <= 97;
|
|
|
|
}
|
|
|
|
if (!gn[8]) {
|
|
|
|
if (gn[4] <= 311) {
|
|
|
|
if (!gn[4]) {
|
|
|
|
return gn[1] > 0 &&
|
|
|
|
((gn[0] <= 26579 && gn[3] > 0) ||
|
|
|
|
(gn[0] > 28672 && gn[0] <= 30506));
|
|
|
|
}
|
|
|
|
if (gn[5] <= 616) {
|
|
|
|
if (gn[6] <= 104) {
|
|
|
|
return gn[9] <= 167;
|
|
|
|
}
|
|
|
|
return gn[6] <= 286;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2008-12-06 14:49:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-02 19:55:57 +00:00
|
|
|
int cli_detect_swizz(struct swizz_stats *stats)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
uint32_t gn[10];
|
|
|
|
uint32_t all = 0;
|
|
|
|
size_t i;
|
|
|
|
int global_swizz = CL_CLEAN;
|
|
|
|
|
|
|
|
cli_dbgmsg("cli_detect_swizz: %lu/%lu, version:%d, manifest: %d \n",
|
|
|
|
(unsigned long)stats->suspicious, (unsigned long)stats->total,
|
|
|
|
stats->has_version, stats->has_manifest);
|
|
|
|
memset(gn, 0, sizeof(gn));
|
|
|
|
for (i = 0; i < 17576; i++) {
|
|
|
|
uint8_t v = stats->gngrams[i];
|
|
|
|
if (v > 10) v = 10;
|
|
|
|
if (v) {
|
|
|
|
gn[v - 1]++;
|
|
|
|
all++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (all) {
|
|
|
|
/* normalize */
|
|
|
|
cli_dbgmsg("cli_detect_swizz: gn: ");
|
|
|
|
for (i = 0; i < sizeof(gn) / sizeof(gn[0]); i++) {
|
|
|
|
uint32_t v = gn[i];
|
|
|
|
gn[i] = (v << 15) / all;
|
|
|
|
if (cli_debug_flag)
|
2022-04-30 19:11:03 -07:00
|
|
|
cli_eprintf("%lu, ", (unsigned long)gn[i]);
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
|
|
|
global_swizz = swizz_j48_global(gn) ? CL_VIRUS : CL_CLEAN;
|
|
|
|
if (cli_debug_flag) {
|
2022-04-30 19:11:03 -07:00
|
|
|
cli_eprintf("\n");
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("cli_detect_swizz: global: %s\n", global_swizz ? "suspicious" : "clean");
|
|
|
|
}
|
|
|
|
}
|
2008-12-06 14:49:00 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (stats->errors > stats->entries || stats->errors >= SWIZZ_MAXERRORS) {
|
|
|
|
cli_dbgmsg("cli_detect_swizz: resources broken, ignoring\n");
|
|
|
|
return CL_CLEAN;
|
|
|
|
}
|
|
|
|
if (stats->total <= 337)
|
|
|
|
return CL_CLEAN;
|
|
|
|
if (stats->suspicious << 10 > 40 * stats->total)
|
|
|
|
return CL_VIRUS;
|
|
|
|
if (!stats->suspicious)
|
|
|
|
return CL_CLEAN;
|
|
|
|
return global_swizz;
|
2008-12-02 19:55:57 +00:00
|
|
|
}
|