e3aaff8e |
/* |
e6b09cf9 |
* Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net> |
e3aaff8e |
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
|
6d6e8271 |
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
|
e3aaff8e |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h> |
15edd45f |
#include <sys/param.h> |
e3aaff8e |
#include <fcntl.h>
#include <dirent.h> |
d66387b9 |
#include <netinet/in.h>
|
e3aaff8e |
|
888f5794 |
#if HAVE_MMAP
#if HAVE_SYS_MMAN_H
#include <sys/mman.h>
#else /* HAVE_SYS_MMAN_H */
#undef HAVE_MMAP
#endif
#endif
|
414abe87 |
#include <mspack.h>
|
e3aaff8e |
#ifdef CL_THREAD_SAFE
# include <pthread.h>
pthread_mutex_t cli_scanrar_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif |
6d6e8271 |
int cli_scanrar_inuse = 0; |
e3aaff8e |
|
414abe87 |
extern short cli_leavetemps_flag;
|
bbd2d959 |
extern int cli_mbox(const char *dir, int desc, unsigned int options); /* FIXME */ |
8000d078 |
|
e3aaff8e |
#include "clamav.h"
#include "others.h" |
85dd8460 |
#include "scanners.h" |
8000d078 |
#include "matcher-ac.h"
#include "matcher-bm.h" |
e3aaff8e |
#include "matcher.h"
#include "unrarlib.h" |
47bbbc56 |
#include "ole2_extract.h"
#include "vba_extract.h" |
341e5433 |
#include "msexpand.h" |
a5373b64 |
#include "chmunpack.h" |
a9082ea2 |
#include "pe.h" |
888f5794 |
#include "filetypes.h"
#include "htmlnorm.h" |
2b259453 |
#include "untar.h" |
c3a3be2d |
#include "special.h" |
6a31c2b4 |
#include "binhex.h" |
e3aaff8e |
#ifdef HAVE_ZLIB_H
#include <zlib.h>
#include <zzip.h>
#endif
#ifdef HAVE_BZLIB_H
#include <bzlib.h>
#endif
|
2bb229f6 |
#if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
#include <limits.h> |
88794204 |
#include <stddef.h> |
2bb229f6 |
#endif
|
15edd45f |
/* Maximum filenames under various systems - njh */
#ifndef NAME_MAX /* e.g. Linux */
# ifdef MAXNAMELEN /* e.g. Solaris */
# define NAME_MAX MAXNAMELEN
# else
# ifdef FILENAME_MAX /* e.g. SCO */
# define NAME_MAX FILENAME_MAX |
3d42cc7a |
# else
# define NAME_MAX 256 |
15edd45f |
# endif
# endif
#endif |
2bb229f6 |
|
3805ebcb |
#define SCAN_ARCHIVE (options & CL_SCAN_ARCHIVE)
#define SCAN_MAIL (options & CL_SCAN_MAIL)
#define SCAN_OLE2 (options & CL_SCAN_OLE2)
#define SCAN_HTML (options & CL_SCAN_HTML)
#define SCAN_PE (options & CL_SCAN_PE)
#define DISABLE_RAR (options & CL_SCAN_DISABLERAR) |
08d6b1e3 |
#define DETECT_ENCRYPTED (options & CL_SCAN_BLOCKENCRYPTED) |
3805ebcb |
#define BLOCKMAX (options & CL_SCAN_BLOCKMAX) |
6d6e8271 |
|
03a2d04a |
#define MAX_MAIL_RECURSION 15 |
e3aaff8e |
|
3805ebcb |
static int cli_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec); |
e3aaff8e |
|
4048c4f6 |
|
e3aaff8e |
#ifdef CL_THREAD_SAFE |
68a6f51f |
static void cli_unlock_mutex(void *mtx) |
e3aaff8e |
{
cli_dbgmsg("Pthread cancelled. Unlocking mutex.\n");
pthread_mutex_unlock(mtx);
}
#endif
|
3805ebcb |
static int cli_scanrar(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
e3aaff8e |
{ |
fc56deed |
FILE *tmp = NULL; |
a62ae54f |
int files = 0, fd, ret = CL_CLEAN, afiles, encrypted; |
e3aaff8e |
ArchiveList_struct *rarlist = NULL; |
68a6f51f |
ArchiveList_struct *rarlist_head = NULL; |
e3aaff8e |
char *rar_data_ptr;
unsigned long rar_data_size; |
a62ae54f |
struct cli_meta_node *mdata; |
e3aaff8e |
|
2b259453 |
cli_dbgmsg("in scanrar()\n"); |
e3aaff8e |
#ifdef CL_THREAD_SAFE
pthread_cleanup_push(cli_unlock_mutex, &cli_scanrar_mutex);
pthread_mutex_lock(&cli_scanrar_mutex);
cli_scanrar_inuse = 1;
#endif
|
a62ae54f |
if(!(afiles = urarlib_list(desc, (ArchiveList_struct *) &rarlist))) { |
e3aaff8e |
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&cli_scanrar_mutex);
cli_scanrar_inuse = 0;
#endif
return CL_ERAR;
}
|
2b259453 |
cli_dbgmsg("RAR: Number of archived files: %d\n", afiles); |
b5f8af0d |
|
68a6f51f |
rarlist_head = rarlist;
|
e3aaff8e |
while(rarlist) { |
a62ae54f |
files++;
encrypted = rarlist->item.Flags & 0x04;
cli_dbgmsg("RAR: %s, crc32: 0x%x, encrypted: %d, compressed: %u, normal: %u, method: %d, ratio: %d (max: %d)\n", rarlist->item.Name, rarlist->item.FileCRC, encrypted, rarlist->item.PackSize, rarlist->item.UnpSize, rarlist->item.Method, rarlist->item.PackSize ? (rarlist->item.UnpSize / rarlist->item.PackSize) : 0, limits ? limits->maxratio : -1);
/* Scan metadata */
mdata = root->rar_mlist;
if(mdata) do {
if(mdata->encrypted != encrypted)
continue;
if(mdata->crc32 && mdata->crc32 != rarlist->item.FileCRC)
continue;
if(mdata->csize > 0 && mdata->csize != rarlist->item.PackSize)
continue;
if(mdata->size >= 0 && mdata->size != rarlist->item.UnpSize)
continue;
if(mdata->method >= 0 && mdata->method != rarlist->item.Method)
continue;
if(mdata->fileno && mdata->fileno != files)
continue;
if(mdata->maxdepth && *arec > mdata->maxdepth)
continue;
/* TODO add support for regex */
/*if(mdata->filename && !strstr(zdirent.d_name, mdata->filename))*/
if(mdata->filename && strcmp(rarlist->item.Name, mdata->filename))
continue;
break; /* matched */
} while((mdata = mdata->next));
if(mdata) {
*virname = mdata->virname;
ret = CL_VIRUS;
break;
}
|
c464b15f |
if(DETECT_ENCRYPTED && (rarlist->item.Flags & 0x04)) { |
2b259453 |
cli_dbgmsg("RAR: Encrypted files found in archive.\n"); |
32c970f8 |
lseek(desc, 0, SEEK_SET); |
3862f7e1 |
ret = cli_scandesc(desc, virname, scanned, root, 0, 0);
if(ret < 0) {
break;
} else if(ret != CL_VIRUS) { |
32c970f8 |
*virname = "Encrypted.RAR"; |
3862f7e1 |
ret = CL_VIRUS;
} |
dfe7ca62 |
break;
} |
e3aaff8e |
|
c464b15f |
if((rarlist->item.Flags & 0x03) != 0) {
cli_dbgmsg("RAR: Skipping %s (splitted)\n", rarlist->item.Name);
rarlist = rarlist->next;
continue;
}
|
e3aaff8e |
if(limits) { |
d272908a |
if(limits->maxratio && rarlist->item.UnpSize && rarlist->item.PackSize) {
if((unsigned int) rarlist->item.UnpSize / (unsigned int) rarlist->item.PackSize >= limits->maxratio) {
cli_dbgmsg("RAR: Max ratio reached (normal: %d, compressed: %d, max: %ld)\n", (int) rarlist->item.UnpSize, (int) rarlist->item.PackSize, limits->maxratio);
*virname = "Oversized.RAR";
ret = CL_VIRUS;
break;
}
}
|
ba174707 |
if(limits->maxfilesize && (rarlist->item.UnpSize > (unsigned int) limits->maxfilesize)) { |
2b259453 |
cli_dbgmsg("RAR: %s: Size exceeded (%u, max: %lu)\n", rarlist->item.Name, (unsigned int) rarlist->item.UnpSize, limits->maxfilesize); |
e3aaff8e |
rarlist = rarlist->next; |
d272908a |
if(BLOCKMAX) {
*virname = "RAR.ExceededFileSize";
ret = CL_VIRUS;
break;
} |
e3aaff8e |
continue;
}
if(limits->maxfiles && (files > limits->maxfiles)) {
cli_dbgmsg("RAR: Files limit reached (max: %d)\n", limits->maxfiles); |
d272908a |
if(BLOCKMAX) {
*virname = "RAR.ExceededFilesLimit";
ret = CL_VIRUS;
break;
} |
e3aaff8e |
break;
}
}
|
e332ad07 |
if(rarlist->item.FileAttr & RAR_FENTRY_ATTR_DIRECTORY) { |
68a6f51f |
rarlist = rarlist->next;
continue;
}
|
e3aaff8e |
if((tmp = tmpfile()) == NULL) { |
2b259453 |
cli_dbgmsg("RAR: Can't generate temporary file.\n"); |
e3aaff8e |
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&cli_scanrar_mutex);
cli_scanrar_inuse = 0;
#endif
return CL_ETMPFILE;
}
fd = fileno(tmp);
|
68a6f51f |
if( urarlib_get(&rar_data_ptr, &rar_data_size, rarlist->item.Name, desc, "clam")) { |
2b259453 |
cli_dbgmsg("RAR: Extracted: %s, size: %lu\n", rarlist->item.Name, rar_data_size); |
68a6f51f |
if(fwrite(rar_data_ptr, 1, rar_data_size, tmp) != rar_data_size) { |
2b259453 |
cli_dbgmsg("RAR: Can't write to file.\n"); |
cdb0ae9c |
fclose(tmp); |
e3aaff8e |
tmp = NULL;
ret = CL_ERAR; |
cdb0ae9c |
if(rar_data_ptr) { |
e3aaff8e |
free(rar_data_ptr); |
cdb0ae9c |
rar_data_ptr = NULL;
} |
27a3f44a |
break; |
e3aaff8e |
}
|
cdb0ae9c |
if(rar_data_ptr) { |
e3aaff8e |
free(rar_data_ptr); |
cdb0ae9c |
rar_data_ptr = NULL;
}
if(fflush(tmp) != 0) { |
2b259453 |
cli_dbgmsg("RAR: fflush() failed: %s\n", strerror(errno)); |
cdb0ae9c |
fclose(tmp); |
68a6f51f |
urarlib_freelist(rarlist_head); |
e3aaff8e |
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&cli_scanrar_mutex);
cli_scanrar_inuse = 0;
#endif
return CL_EFSYNC;
}
lseek(fd, 0, SEEK_SET); |
22275b15 |
if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) { |
2b259453 |
cli_dbgmsg("RAR: Infected with %s\n", *virname); |
6d6e8271 |
|
cdb0ae9c |
fclose(tmp); |
e3aaff8e |
urarlib_freelist(rarlist);
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&cli_scanrar_mutex);
cli_scanrar_inuse = 0;
#endif |
68a6f51f |
return ret; |
e3aaff8e |
}
} else { |
2b259453 |
cli_dbgmsg("RAR: Can't decompress file %s\n", rarlist->item.Name); |
cdb0ae9c |
fclose(tmp); |
7761c6eb |
tmp = NULL; |
cdb0ae9c |
ret = CL_ERAR; /* WinRAR 3.0 ? */
break; |
e3aaff8e |
}
|
cdb0ae9c |
fclose(tmp); |
e3aaff8e |
tmp = NULL;
rarlist = rarlist->next;
}
|
68a6f51f |
urarlib_freelist(rarlist_head); |
e3aaff8e |
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&cli_scanrar_mutex);
cli_scanrar_inuse = 0;
pthread_cleanup_pop(0);
#endif |
7761c6eb |
|
2b259453 |
cli_dbgmsg("RAR: Exit code: %d\n", ret); |
7761c6eb |
|
e3aaff8e |
return ret;
}
#ifdef HAVE_ZLIB_H |
3805ebcb |
static int cli_scanzip(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
e3aaff8e |
{
ZZIP_DIR *zdir;
ZZIP_DIRENT zdirent;
ZZIP_FILE *zfp; |
fc56deed |
FILE *tmp = NULL; |
f7148839 |
char *buff; |
e5916a51 |
int fd, bytes, files = 0, ret = CL_CLEAN, encrypted; |
fc56deed |
struct stat source; |
a62ae54f |
struct cli_meta_node *mdata; |
ee039e40 |
zzip_error_t err; |
e3aaff8e |
|
2b259453 |
cli_dbgmsg("in scanzip()\n"); |
e3aaff8e |
|
049a18b9 |
if((zdir = zzip_dir_fdopen(dup(desc), &err)) == NULL) { |
2b259453 |
cli_dbgmsg("Zip: Not supported file format ?.\n");
cli_dbgmsg("Zip: zzip_dir_fdopen() return code: %d\n", err); |
77be7ea9 |
/* no return with CL_EZIP due to password protected zips */
return CL_CLEAN; |
e3aaff8e |
}
|
fc56deed |
fstat(desc, &source);
|
33f40ee5 |
if(!(buff = (char *) cli_malloc(FILEBUFF))) { |
2b259453 |
cli_dbgmsg("Zip: unable to malloc(%d)\n", FILEBUFF); |
33f40ee5 |
zzip_dir_close(zdir);
return CL_EMEM;
}
|
e3aaff8e |
while(zzip_dir_read(zdir, &zdirent)) { |
19e2ac07 |
files++; |
fc56deed |
if(!zdirent.d_name || !strlen(zdirent.d_name)) { /* Mimail fix */ |
2b259453 |
cli_dbgmsg("Zip: strlen(zdirent.d_name) == %d\n", strlen(zdirent.d_name)); |
a0977bfe |
*virname = "Suspected.Zip"; |
fc56deed |
ret = CL_VIRUS;
break;
}
|
e5916a51 |
encrypted = zdirent.d_flags;
|
a62ae54f |
cli_dbgmsg("Zip: %s, crc32: 0x%x, encrypted: %d, compressed: %u, normal: %u, method: %d, ratio: %d (max: %d)\n", zdirent.d_name, zdirent.d_crc32, encrypted, zdirent.d_csize, zdirent.st_size, zdirent.d_compr, zdirent.d_csize ? (zdirent.st_size / zdirent.d_csize) : 0, limits ? limits->maxratio : -1); |
fc56deed |
|
0c9d7f15 |
if(!zdirent.st_size) { |
d3779101 |
if(zdirent.d_crc32) {
cli_dbgmsg("Zip: Broken file or modified information in local header part of archive\n");
*virname = "Suspected.Zip";
ret = CL_VIRUS;
break;
} |
e3aaff8e |
continue;
}
|
e5916a51 |
/* Scan metadata */
mdata = root->zip_mlist; |
1de1c2b8 |
if(mdata) do { |
e5916a51 |
if(mdata->encrypted != encrypted)
continue;
if(mdata->crc32 && mdata->crc32 != zdirent.d_crc32)
continue;
if(mdata->csize > 0 && mdata->csize != zdirent.d_csize)
continue;
if(mdata->size >= 0 && mdata->size != zdirent.st_size)
continue;
|
a62ae54f |
if(mdata->method >= 0 && mdata->method != zdirent.d_compr) |
e5916a51 |
continue;
|
19e2ac07 |
if(mdata->fileno && mdata->fileno != files)
continue;
if(mdata->maxdepth && *arec > mdata->maxdepth)
continue;
/* TODO add support for regex */ |
e5916a51 |
/*if(mdata->filename && !strstr(zdirent.d_name, mdata->filename))*/
if(mdata->filename && strcmp(zdirent.d_name, mdata->filename))
continue;
break; /* matched */
} while((mdata = mdata->next));
if(mdata) {
*virname = mdata->virname;
ret = CL_VIRUS;
break;
}
|
ba5d2f0b |
/*
* Workaround for archives created with ICEOWS.
* ZZIP_DIRENT does not contain information on file type
* so we try to determine a directory via a filename
*/
if(zdirent.d_name[strlen(zdirent.d_name) - 1] == '/') {
cli_dbgmsg("Zip: Directory entry with st_size != 0\n");
continue;
}
|
e3aaff8e |
/* work-around for problematic zips (zziplib crashes with them) */ |
19ca43de |
if(zdirent.d_csize <= 0 || zdirent.st_size < 0) { |
2b259453 |
cli_dbgmsg("Zip: Malformed archive detected.\n"); |
a0977bfe |
*virname = "Suspected.Zip"; |
fc56deed |
ret = CL_VIRUS; |
e3aaff8e |
break;
}
|
19ca43de |
if(limits && limits->maxratio > 0 && ((unsigned) zdirent.st_size / (unsigned) zdirent.d_csize) >= limits->maxratio) {
*virname = "Oversized.Zip";
ret = CL_VIRUS;
break;
}
|
e5916a51 |
if(DETECT_ENCRYPTED && encrypted) { |
2b259453 |
cli_dbgmsg("Zip: Encrypted files found in archive.\n"); |
32c970f8 |
lseek(desc, 0, SEEK_SET); |
3862f7e1 |
ret = cli_scandesc(desc, virname, scanned, root, 0, 0);
if(ret < 0) {
break;
} else if(ret != CL_VIRUS) { |
32c970f8 |
*virname = "Encrypted.Zip"; |
3862f7e1 |
ret = CL_VIRUS;
} |
0f34221a |
break;
}
|
e3aaff8e |
if(limits) {
if(limits->maxfilesize && (zdirent.st_size > limits->maxfilesize)) { |
2b259453 |
cli_dbgmsg("Zip: %s: Size exceeded (%d, max: %ld)\n", zdirent.d_name, zdirent.st_size, limits->maxfilesize); |
6ccc6990 |
/* ret = CL_EMAXSIZE; */ |
d272908a |
if(BLOCKMAX) {
*virname = "Zip.ExceededFileSize";
ret = CL_VIRUS;
break;
}
continue; /* continue scanning */ |
e3aaff8e |
}
if(limits->maxfiles && (files > limits->maxfiles)) {
cli_dbgmsg("Zip: Files limit reached (max: %d)\n", limits->maxfiles); |
d272908a |
if(BLOCKMAX) {
*virname = "Zip.ExceededFilesLimit";
ret = CL_VIRUS;
break;
} |
e3aaff8e |
break;
}
}
|
c4098620 |
if((zfp = zzip_file_open(zdir, zdirent.d_name, 0)) == NULL) {
cli_dbgmsg("Zip: Can't open file %s\n", zdirent.d_name);
ret = CL_EZIP;
break;
}
|
e3aaff8e |
/* generate temporary file and get its descriptor */
if((tmp = tmpfile()) == NULL) { |
2b259453 |
cli_dbgmsg("Zip: Can't generate tmpfile().\n"); |
bde7f6d1 |
zzip_file_close(zfp); |
fc56deed |
ret = CL_ETMPFILE;
break; |
e3aaff8e |
}
|
f7148839 |
while((bytes = zzip_file_read(zfp, buff, FILEBUFF)) > 0) { |
467f8b1e |
if(fwrite(buff, 1, bytes, tmp) != (size_t) bytes) { |
2b259453 |
cli_dbgmsg("Zip: Can't write to file.\n"); |
e3aaff8e |
zzip_file_close(zfp); |
33f40ee5 |
zzip_dir_close(zdir);
fclose(tmp); |
f7148839 |
free(buff); |
c4098620 |
return CL_EIO; |
e3aaff8e |
}
}
zzip_file_close(zfp);
|
cdb0ae9c |
if(fflush(tmp) != 0) { |
2b259453 |
cli_dbgmsg("Zip: fflush() failed: %s\n", strerror(errno)); |
fc56deed |
ret = CL_EFSYNC;
break; |
e3aaff8e |
}
|
cdb0ae9c |
fd = fileno(tmp);
|
e3aaff8e |
lseek(fd, 0, SEEK_SET); |
22275b15 |
if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) { |
2b259453 |
cli_dbgmsg("Zip: Infected with %s\n", *virname); |
e3aaff8e |
ret = CL_VIRUS;
break;
} else if(ret == CL_EMALFZIP) {
/* |
fc56deed |
* The trick with detection of ZoD only works with higher (>= 5) |
e3aaff8e |
* recursion limit level.
*/ |
2b259453 |
cli_dbgmsg("Zip: Malformed Zip file, scanning stopped.\n"); |
a0977bfe |
*virname = "Suspected.Zip"; |
e3aaff8e |
ret = CL_VIRUS;
break;
}
|
cdb0ae9c |
if (tmp) {
fclose(tmp);
tmp = NULL;
} |
e3aaff8e |
}
zzip_dir_close(zdir); |
cdb0ae9c |
if (tmp) {
fclose(tmp);
tmp = NULL;
} |
33f40ee5 |
free(buff); |
e3aaff8e |
return ret;
}
|
3805ebcb |
static int cli_scangzip(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
e3aaff8e |
{
int fd, bytes, ret = CL_CLEAN;
long int size = 0; |
f7148839 |
char *buff; |
fc56deed |
FILE *tmp = NULL; |
e3aaff8e |
gzFile gd;
|
fc56deed |
cli_dbgmsg("in cli_scangzip()\n");
|
e3aaff8e |
if((gd = gzdopen(dup(desc), "rb")) == NULL) { |
2b259453 |
cli_dbgmsg("GZip: Can't open descriptor %d\n", desc); |
e3aaff8e |
return CL_EGZIP;
}
if((tmp = tmpfile()) == NULL) { |
2b259453 |
cli_dbgmsg("GZip: Can't generate temporary file.\n"); |
e3aaff8e |
gzclose(gd);
return CL_ETMPFILE;
}
fd = fileno(tmp);
|
fc56deed |
if(!(buff = (char *) cli_malloc(FILEBUFF))) { |
2b259453 |
cli_dbgmsg("GZip: Unable to malloc %d bytes.\n", FILEBUFF); |
fc56deed |
gzclose(gd); |
f7148839 |
return CL_EMEM; |
fc56deed |
} |
f7148839 |
|
fc56deed |
while((bytes = gzread(gd, buff, FILEBUFF)) > 0) { |
e3aaff8e |
size += bytes;
if(limits) |
fc56deed |
if(limits->maxfilesize && (size + FILEBUFF > limits->maxfilesize)) { |
2b259453 |
cli_dbgmsg("GZip: Size exceeded (stopped at %ld, max: %ld)\n", size, limits->maxfilesize); |
6ccc6990 |
/* ret = CL_EMAXSIZE; */ |
e3aaff8e |
break;
}
|
4152ad50 |
if(cli_writen(fd, buff, bytes) != bytes) { |
2b259453 |
cli_dbgmsg("GZip: Can't write to file.\n"); |
cdb0ae9c |
fclose(tmp); |
e3aaff8e |
gzclose(gd); |
f7148839 |
free(buff); |
e3aaff8e |
return CL_EGZIP;
}
}
|
f7148839 |
free(buff); |
e3aaff8e |
gzclose(gd);
if(fsync(fd) == -1) { |
2b259453 |
cli_dbgmsg("GZip: Can't synchronise descriptor %d\n", fd); |
cdb0ae9c |
fclose(tmp); |
e3aaff8e |
return CL_EFSYNC;
}
lseek(fd, 0, SEEK_SET); |
22275b15 |
if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) { |
2b259453 |
cli_dbgmsg("GZip: Infected with %s\n", *virname); |
cdb0ae9c |
fclose(tmp); |
e3aaff8e |
return CL_VIRUS;
} |
cdb0ae9c |
fclose(tmp); |
e3aaff8e |
return ret;
}
#endif
#ifdef HAVE_BZLIB_H
#ifdef NOBZ2PREFIX
#define BZ2_bzReadOpen bzReadOpen
#define BZ2_bzReadClose bzReadClose
#define BZ2_bzRead bzRead
#endif
|
3805ebcb |
static int cli_scanbzip(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
e3aaff8e |
{
int fd, bytes, ret = CL_CLEAN, bzerror = 0;
short memlim = 0;
long int size = 0; |
f7148839 |
char *buff; |
fc56deed |
FILE *fs, *tmp = NULL; |
e3aaff8e |
BZFILE *bfd;
|
9e431a95 |
if((fs = fdopen(dup(desc), "rb")) == NULL) { |
2b259453 |
cli_dbgmsg("Bzip: Can't open descriptor %d.\n", desc); |
e3aaff8e |
return CL_EBZIP;
}
if(limits)
if(limits->archivememlim)
memlim = 1;
|
68b96877 |
if((bfd = BZ2_bzReadOpen(&bzerror, fs, 0, memlim, NULL, 0)) == NULL) { |
2b259453 |
cli_dbgmsg("Bzip: Can't initialize bzip2 library (descriptor: %d).\n", desc); |
9e431a95 |
fclose(fs); |
e3aaff8e |
return CL_EBZIP;
}
if((tmp = tmpfile()) == NULL) { |
2b259453 |
cli_dbgmsg("Bzip: Can't generate temporary file.\n"); |
e3aaff8e |
BZ2_bzReadClose(&bzerror, bfd); |
9e431a95 |
fclose(fs); |
e3aaff8e |
return CL_ETMPFILE;
}
fd = fileno(tmp);
|
9e431a95 |
if(!(buff = (char *) malloc(FILEBUFF))) { |
2b259453 |
cli_dbgmsg("Bzip: Unable to malloc %d bytes.\n", FILEBUFF); |
9e431a95 |
fclose(tmp);
fclose(fs);
BZ2_bzReadClose(&bzerror, bfd); |
f7148839 |
return CL_EMEM; |
9e431a95 |
} |
f7148839 |
|
fc56deed |
while((bytes = BZ2_bzRead(&bzerror, bfd, buff, FILEBUFF)) > 0) { |
e3aaff8e |
size += bytes;
if(limits) |
fc56deed |
if(limits->maxfilesize && (size + FILEBUFF > limits->maxfilesize)) { |
2b259453 |
cli_dbgmsg("Bzip: Size exceeded (stopped at %ld, max: %ld)\n", size, limits->maxfilesize); |
6ccc6990 |
/* ret = CL_EMAXSIZE; */ |
e3aaff8e |
break;
}
|
4152ad50 |
if(cli_writen(fd, buff, bytes) != bytes) { |
2b259453 |
cli_dbgmsg("Bzip: Can't write to file.\n"); |
e3aaff8e |
BZ2_bzReadClose(&bzerror, bfd); |
cdb0ae9c |
fclose(tmp); |
f7148839 |
free(buff); |
9e431a95 |
fclose(fs); |
e3aaff8e |
return CL_EGZIP;
}
}
|
f7148839 |
free(buff); |
e3aaff8e |
BZ2_bzReadClose(&bzerror, bfd);
if(fsync(fd) == -1) { |
2b259453 |
cli_dbgmsg("Bzip: Synchronisation failed for descriptor %d\n", fd); |
cdb0ae9c |
fclose(tmp); |
9e431a95 |
fclose(fs); |
e3aaff8e |
return CL_EFSYNC;
}
lseek(fd, 0, SEEK_SET); |
22275b15 |
if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS ) { |
2b259453 |
cli_dbgmsg("Bzip: Infected with %s\n", *virname); |
e3aaff8e |
} |
cdb0ae9c |
fclose(tmp); |
9e431a95 |
fclose(fs); |
e3aaff8e |
return ret;
}
#endif
|
3805ebcb |
static int cli_scanszdd(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
341e5433 |
{
int fd, ret = CL_CLEAN;
FILE *tmp = NULL, *in;
|
2b259453 |
|
341e5433 |
cli_dbgmsg("in cli_scanmscomp()\n");
if((in = fdopen(dup(desc), "rb")) == NULL) { |
2b259453 |
cli_dbgmsg("SZDD: Can't open descriptor %d\n", desc); |
341e5433 |
return CL_EMSCOMP;
}
if((tmp = tmpfile()) == NULL) { |
2b259453 |
cli_dbgmsg("SZDD: Can't generate temporary file.\n"); |
341e5433 |
fclose(in);
return CL_ETMPFILE;
}
if(cli_msexpand(in, tmp) == -1) { |
2b259453 |
cli_dbgmsg("SZDD: msexpand failed.\n"); |
341e5433 |
return CL_EMSCOMP;
}
fclose(in);
if(fflush(tmp)) { |
2b259453 |
cli_dbgmsg("SZDD: fflush() failed.\n"); |
341e5433 |
fclose(tmp);
return CL_EFSYNC;
}
fd = fileno(tmp);
lseek(fd, 0, SEEK_SET); |
22275b15 |
if((ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS) { |
2b259453 |
cli_dbgmsg("SZDD: Infected with %s\n", *virname); |
341e5433 |
fclose(tmp);
return CL_VIRUS;
}
fclose(tmp);
return ret;
}
|
3805ebcb |
static int cli_scanmscab(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
414abe87 |
{
struct mscab_decompressor *cabd = NULL;
struct mscabd_cabinet *base, *cab;
struct mscabd_file *file;
char *tempname;
int ret = CL_CLEAN;
cli_dbgmsg("in cli_scanmscab()\n");
if((cabd = mspack_create_cab_decompressor(NULL)) == NULL) { |
2b259453 |
cli_dbgmsg("MSCAB: Can't create libmspack CAB decompressor\n"); |
414abe87 |
return CL_EMSCAB;
}
|
0c9d7f15 |
if((base = cabd->dsearch(cabd, dup(desc))) == NULL) { |
2b259453 |
cli_dbgmsg("MSCAB: I/O error or no valid cabinets found\n"); |
414abe87 |
mspack_destroy_cab_decompressor(cabd);
return CL_EMSCAB;
}
for(cab = base; cab; cab = cab->next) {
for(file = cab->files; file; file = file->next) { |
52ab3176 |
if(limits && limits->maxfilesize && (file->length > (unsigned int) limits->maxfilesize)) {
cli_dbgmsg("MSCAB: %s: Size exceeded (%u, max: %lu)\n", file->filename, file->length, limits->maxfilesize);
if(BLOCKMAX) {
*virname = "MSCAB.ExceededFileSize";
cabd->close(cabd, base);
mspack_destroy_cab_decompressor(cabd);
return CL_VIRUS;
}
continue;
}
|
0c7019c5 |
tempname = cli_gentemp(NULL); |
2b259453 |
cli_dbgmsg("MSCAB: Extracting data to %s\n", tempname); |
414abe87 |
if(cabd->extract(cabd, file, tempname)) { |
2b259453 |
cli_dbgmsg("MSCAB: libmscab error code: %d\n", cabd->last_error(cabd)); |
414abe87 |
} else { |
22275b15 |
ret = cli_scanfile(tempname, virname, scanned, root, limits, options, arec, mrec); |
414abe87 |
}
if(!cli_leavetemps_flag)
unlink(tempname);
free(tempname);
if(ret == CL_VIRUS)
break;
}
if(ret == CL_VIRUS)
break;
}
cabd->close(cabd, base);
mspack_destroy_cab_decompressor(cabd);
return ret;
}
|
3805ebcb |
static int cli_scandir(const char *dirname, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
47bbbc56 |
{ |
467f8b1e |
DIR *dd;
struct dirent *dent; |
72a1b240 |
#if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2) |
88794204 |
union {
struct dirent d;
char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
} result; |
2bb229f6 |
#endif |
467f8b1e |
struct stat statbuf;
char *fname; |
47bbbc56 |
|
467f8b1e |
if((dd = opendir(dirname)) != NULL) { |
72a1b240 |
#ifdef HAVE_READDIR_R_3 |
88794204 |
while(!readdir_r(dd, &result.d, &dent) && dent) { |
72a1b240 |
#elif defined(HAVE_READDIR_R_2) |
88794204 |
while((dent = (struct dirent *) readdir_r(dd, &result.d))) { |
72a1b240 |
#else |
467f8b1e |
while((dent = readdir(dd))) { |
72a1b240 |
#endif |
feeaa333 |
#ifndef C_INTERIX
if(dent->d_ino)
#endif
{ |
467f8b1e |
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
/* build the full name */
fname = cli_calloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char));
sprintf(fname, "%s/%s", dirname, dent->d_name); |
47bbbc56 |
|
467f8b1e |
/* stat the file */
if(lstat(fname, &statbuf) != -1) {
if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) { |
22275b15 |
if (cli_scandir(fname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) { |
467f8b1e |
free(fname);
closedir(dd);
return CL_VIRUS;
}
} else
if(S_ISREG(statbuf.st_mode)) |
22275b15 |
if(cli_scanfile(fname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) { |
467f8b1e |
free(fname);
closedir(dd);
return CL_VIRUS;
} |
47bbbc56 |
|
bbb90786 |
} |
467f8b1e |
free(fname); |
47bbbc56 |
}
}
} |
467f8b1e |
} else { |
2b259453 |
cli_dbgmsg("ScanDir: Can't open directory %s.\n", dirname); |
467f8b1e |
return CL_EOPEN;
} |
47bbbc56 |
|
467f8b1e |
closedir(dd);
return 0; |
47bbbc56 |
} |
467f8b1e |
|
3805ebcb |
static int cli_vba_scandir(const char *dirname, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
e3aaff8e |
{ |
467f8b1e |
int ret = CL_CLEAN, i, fd, data_len;
vba_project_t *vba_project; |
e3aaff8e |
DIR *dd;
struct dirent *dent; |
72a1b240 |
#if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2) |
88794204 |
union {
struct dirent d;
char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
} result; |
2bb229f6 |
#endif |
e3aaff8e |
struct stat statbuf; |
56bfccb2 |
char *fname, *fullname; |
467f8b1e |
unsigned char *data;
|
2b259453 |
cli_dbgmsg("VBADir: %s\n", dirname); |
467f8b1e |
if((vba_project = (vba_project_t *) vba56_dir_read(dirname))) { |
e3aaff8e |
|
467f8b1e |
for(i = 0; i < vba_project->count; i++) {
fullname = (char *) cli_malloc(strlen(vba_project->dir) + strlen(vba_project->name[i]) + 2);
sprintf(fullname, "%s/%s", vba_project->dir, vba_project->name[i]);
fd = open(fullname, O_RDONLY);
if(fd == -1) { |
2b259453 |
cli_dbgmsg("VBADir: Can't open file %s\n", fullname); |
467f8b1e |
free(fullname);
ret = CL_EOPEN;
break;
}
free(fullname); |
2b259453 |
cli_dbgmsg("VBADir: Decompress VBA project '%s'\n", vba_project->name[i]); |
467f8b1e |
data = (unsigned char *) vba_decompress(fd, vba_project->offset[i], &data_len);
close(fd);
if(!data) { |
2b259453 |
cli_dbgmsg("VBADir: WARNING: VBA project '%s' decompressed to NULL\n", vba_project->name[i]); |
467f8b1e |
} else { |
cedc2d2a |
if(scanned)
*scanned += data_len / CL_COUNT_PRECISION;
|
7ec67e94 |
if(cli_scanbuff(data, data_len, virname, root, CL_TYPE_MSOLE2) == CL_VIRUS) { |
467f8b1e |
free(data);
ret = CL_VIRUS;
break;
}
free(data);
}
}
for(i = 0; i < vba_project->count; i++)
free(vba_project->name[i]);
free(vba_project->name);
free(vba_project->dir);
free(vba_project->offset);
free(vba_project); |
c240c32c |
} else if ((fullname = ppt_vba_read(dirname))) { |
22275b15 |
if(cli_scandir(fullname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) { |
c240c32c |
ret = CL_VIRUS;
} |
8d5e0ac0 |
if(!cli_leavetemps_flag)
cli_rmdirs(fullname); |
c240c32c |
free(fullname); |
2e53806b |
} else if ((vba_project = (vba_project_t *) wm_dir_read(dirname))) {
for (i = 0; i < vba_project->count; i++) {
fullname = (char *) cli_malloc(strlen(vba_project->dir) + strlen(vba_project->name[i]) + 2);
sprintf(fullname, "%s/%s", vba_project->dir, vba_project->name[i]);
fd = open(fullname, O_RDONLY);
if(fd == -1) { |
2b259453 |
cli_dbgmsg("VBADir: Can't open file %s\n", fullname); |
2e53806b |
free(fullname);
ret = CL_EOPEN;
break;
}
free(fullname); |
2b259453 |
cli_dbgmsg("VBADir: Decompress WM project '%s' macro:%d key:%d\n", vba_project->name[i], i, vba_project->key[i]); |
2e53806b |
data = (unsigned char *) wm_decrypt_macro(fd, vba_project->offset[i], vba_project->length[i], vba_project->key[i]);
close(fd);
if(!data) { |
2b259453 |
cli_dbgmsg("VBADir: WARNING: WM project '%s' macro %d decrypted to NULL\n", vba_project->name[i], i); |
2e53806b |
} else { |
cedc2d2a |
if(scanned)
*scanned += vba_project->length[i] / CL_COUNT_PRECISION; |
7ec67e94 |
if(cli_scanbuff(data, vba_project->length[i], virname, root, CL_TYPE_MSOLE2) == CL_VIRUS) { |
2e53806b |
free(data);
ret = CL_VIRUS;
break;
}
free(data);
}
}
for(i = 0; i < vba_project->count; i++)
free(vba_project->name[i]);
free(vba_project->key);
free(vba_project->length);
free(vba_project->offset);
free(vba_project->name);
free(vba_project->dir);
free(vba_project); |
467f8b1e |
} |
2e53806b |
|
467f8b1e |
if(ret != CL_CLEAN)
return ret; |
e3aaff8e |
if((dd = opendir(dirname)) != NULL) { |
72a1b240 |
#ifdef HAVE_READDIR_R_3 |
88794204 |
while(!readdir_r(dd, &result.d, &dent) && dent) { |
72a1b240 |
#elif defined(HAVE_READDIR_R_2) |
88794204 |
while((dent = (struct dirent *) readdir_r(dd, &result.d))) { |
72a1b240 |
#else |
e3aaff8e |
while((dent = readdir(dd))) { |
72a1b240 |
#endif |
feeaa333 |
#ifndef C_INTERIX
if(dent->d_ino)
#endif
{ |
e3aaff8e |
if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
/* build the full name */
fname = cli_calloc(strlen(dirname) + strlen(dent->d_name) + 2, sizeof(char));
sprintf(fname, "%s/%s", dirname, dent->d_name);
/* stat the file */
if(lstat(fname, &statbuf) != -1) {
if(S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) |
22275b15 |
if (cli_vba_scandir(fname, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) { |
467f8b1e |
ret = CL_VIRUS;
free(fname);
break;
} |
e3aaff8e |
}
free(fname);
}
}
}
} else { |
2b259453 |
cli_dbgmsg("VBADir: Can't open directory %s.\n", dirname); |
e3aaff8e |
return CL_EOPEN;
}
closedir(dd); |
467f8b1e |
return ret;
}
|
a92110df |
static int cli_scanhtml(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
{
char *tempname, fullname[1024];
int ret=CL_CLEAN, fd;
cli_dbgmsg("in cli_scanhtml()\n");
tempname = cli_gentemp(NULL);
if(mkdir(tempname, 0700)) {
cli_dbgmsg("ScanHTML -> Can't create temporary directory %s\n", tempname);
return CL_ETMPDIR;
}
html_normalise_fd(desc, tempname, NULL);
snprintf(fullname, 1024, "%s/comment.html", tempname);
fd = open(fullname, O_RDONLY);
if (fd >= 0) {
ret = cli_scandesc(fd, virname, scanned, root, 0, CL_TYPE_HTML);
close(fd);
}
if(ret < 0 || ret == CL_VIRUS) {
if(!cli_leavetemps_flag)
cli_rmdirs(tempname);
free(tempname);
return ret;
}
if (ret == CL_CLEAN) {
snprintf(fullname, 1024, "%s/nocomment.html", tempname);
fd = open(fullname, O_RDONLY);
if (fd >= 0) {
ret = cli_scandesc(fd, virname, scanned, root, 0, CL_TYPE_HTML);
close(fd);
}
}
if(ret < 0 || ret == CL_VIRUS) {
if(!cli_leavetemps_flag)
cli_rmdirs(tempname);
free(tempname);
return ret;
}
if (ret == CL_CLEAN) {
snprintf(fullname, 1024, "%s/script.html", tempname);
fd = open(fullname, O_RDONLY);
if (fd >= 0) {
ret = cli_scandesc(fd, virname, scanned, root, 0, CL_TYPE_HTML);
close(fd);
}
}
if(ret < 0 || ret == CL_VIRUS) {
if(!cli_leavetemps_flag)
cli_rmdirs(tempname);
free(tempname);
return ret;
}
if (ret == CL_CLEAN) {
snprintf(fullname, 1024, "%s/rfc2397", tempname);
ret = cli_scandir(fullname, virname, scanned, root, limits, options, arec, mrec);
}
if(!cli_leavetemps_flag)
cli_rmdirs(tempname);
free(tempname);
return ret;
}
|
3805ebcb |
static int cli_scanole2(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
467f8b1e |
{ |
56bfccb2 |
char *dir;
int ret = CL_CLEAN; |
467f8b1e |
|
2b259453 |
|
467f8b1e |
cli_dbgmsg("in cli_scanole2()\n");
/* generate the temporary directory */ |
0c7019c5 |
dir = cli_gentemp(NULL); |
467f8b1e |
if(mkdir(dir, 0700)) { |
2b259453 |
cli_dbgmsg("OLE2: Can't create temporary directory %s\n", dir); |
467f8b1e |
return CL_ETMPDIR;
}
|
e5a5b2f2 |
if((ret = cli_ole2_extract(desc, dir, limits))) { |
2b259453 |
cli_dbgmsg("OLE2: %s\n", cl_strerror(ret)); |
8d5e0ac0 |
if(!cli_leavetemps_flag)
cli_rmdirs(dir); |
467f8b1e |
free(dir);
return ret;
}
|
22275b15 |
if((ret = cli_vba_scandir(dir, virname, scanned, root, limits, options, arec, mrec)) != CL_VIRUS) {
if(cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) { |
56bfccb2 |
ret = CL_VIRUS; |
467f8b1e |
}
}
|
8d5e0ac0 |
if(!cli_leavetemps_flag)
cli_rmdirs(dir); |
467f8b1e |
free(dir);
return ret; |
e3aaff8e |
}
|
3805ebcb |
static int cli_scantar(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
2b259453 |
{
char *dir;
int ret = CL_CLEAN;
cli_dbgmsg("in cli_scantar()\n");
/* generate temporary directory */ |
0c7019c5 |
dir = cli_gentemp(NULL); |
2b259453 |
if(mkdir(dir, 0700)) {
cli_errmsg("Tar: Can't create temporary directory %s\n", dir);
return CL_ETMPDIR;
}
if((ret = cli_untar(dir, desc)))
cli_dbgmsg("Tar: %s\n", cl_strerror(ret));
else
ret = cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec);
if(!cli_leavetemps_flag)
cli_rmdirs(dir);
free(dir);
return ret;
}
|
667ab9c6 |
static int cli_scanbinhex(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
{
char *dir;
int ret = CL_CLEAN;
cli_dbgmsg("in cli_scanbinhex()\n");
/* generate temporary directory */
dir = cli_gentemp(NULL);
if(mkdir(dir, 0700)) {
cli_errmsg("Binhex: Can't create temporary directory %s\n", dir);
return CL_ETMPDIR;
}
if((ret = cli_binhex(dir, desc)))
cli_dbgmsg("Binhex: %s\n", cl_strerror(ret));
else
ret = cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec);
if(!cli_leavetemps_flag)
cli_rmdirs(dir);
free(dir);
return ret;
}
|
3805ebcb |
static int cli_scanmschm(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
a5373b64 |
{
char *tempname;
int ret = CL_CLEAN;
cli_dbgmsg("in cli_scanmschm()\n");
|
0c7019c5 |
tempname = cli_gentemp(NULL); |
a5373b64 |
if(mkdir(tempname, 0700)) { |
2b259453 |
cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname); |
a5373b64 |
return CL_ETMPDIR;
}
if(chm_unpack(desc, tempname))
ret = cli_scandir(tempname, virname, scanned, root, limits, options, arec, mrec);
if(!cli_leavetemps_flag)
cli_rmdirs(tempname);
free(tempname);
return ret;
}
|
3805ebcb |
static int cli_scanscrenc(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
e57fa318 |
{
char *tempname;
int ret = CL_CLEAN;
cli_dbgmsg("in cli_scanscrenc()\n");
|
0c7019c5 |
tempname = cli_gentemp(NULL); |
e57fa318 |
if(mkdir(tempname, 0700)) {
cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname);
return CL_ETMPDIR;
}
if (html_screnc_decode(desc, tempname))
ret = cli_scandir(tempname, virname, scanned, root, limits, options, arec, mrec);
if(!cli_leavetemps_flag)
cli_rmdirs(tempname);
free(tempname);
return ret;
} |
015e31e1 |
|
eb308794 |
static int cli_scanriff(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
{
int ret = CL_CLEAN;
if(cli_check_riff_exploit(desc) == 2) {
ret = CL_VIRUS;
*virname = "Exploit.W32.MS05-002";
}
return ret;
}
|
3805ebcb |
static int cli_scanmail(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
e3aaff8e |
{
char *dir;
int ret;
|
03a2d04a |
cli_dbgmsg("Starting cli_scanmail(), mrec == %d, arec == %d\n", *mrec, *arec); |
e3aaff8e |
|
0c7019c5 |
/* generate the temporary directory */
dir = cli_gentemp(NULL);
if(mkdir(dir, 0700)) {
cli_dbgmsg("Mail: Can't create temporary directory %s\n", dir);
free(dir);
return CL_ETMPDIR;
} |
e3aaff8e |
|
0c7019c5 |
/*
* Extract the attachments into the temporary directory
*/
if((ret = cli_mbox(dir, desc, options))) { |
8d5e0ac0 |
if(!cli_leavetemps_flag)
cli_rmdirs(dir); |
e3aaff8e |
free(dir);
return ret; |
0c7019c5 |
}
ret = cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec);
if(!cli_leavetemps_flag)
cli_rmdirs(dir);
free(dir);
return ret; |
e3aaff8e |
}
|
3805ebcb |
int cli_magic_scandesc(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
e3aaff8e |
{ |
2b259453 |
char magic[MAGIC_BUFFER_SIZE + 1]; |
888f5794 |
int ret = CL_CLEAN, nret; |
cdb0ae9c |
int bread = 0; |
9b8e00a0 |
cli_file_t type; |
e3aaff8e |
if(!root) { |
414abe87 |
cli_errmsg("CRITICAL: root == NULL\n"); |
322bfd03 |
return CL_EMALFDB; |
e3aaff8e |
}
|
d6b5ae47 |
if(!options) { /* raw mode (stdin, etc.) */ |
b68d11d2 |
cli_dbgmsg("Raw mode: No support for special files\n");
if((ret = cli_scandesc(desc, virname, scanned, root, 0, 0) == CL_VIRUS)) |
2b259453 |
cli_dbgmsg("%s found in descriptor %d\n", *virname, desc); |
d6b5ae47 |
return ret;
}
|
467f8b1e |
if(SCAN_ARCHIVE && limits && limits->maxreclevel) |
22275b15 |
if(*arec > limits->maxreclevel) {
cli_dbgmsg("Archive recursion limit exceeded (arec == %d).\n", *arec); |
031555fe |
if(BLOCKMAX) {
*virname = "Archive.ExceededRecursionLimit";
return CL_VIRUS;
} |
467f8b1e |
return CL_CLEAN; |
22275b15 |
} |
e3aaff8e |
|
22275b15 |
if(SCAN_MAIL)
if(*mrec > MAX_MAIL_RECURSION) {
cli_dbgmsg("Mail recursion level exceeded (mrec == %d).\n", *mrec);
/* return CL_EMAXREC; */
return CL_CLEAN;
} |
e3aaff8e |
|
467f8b1e |
lseek(desc, 0, SEEK_SET); |
2b259453 |
memset(magic, 0, sizeof(magic)); |
e3aaff8e |
|
2b259453 |
if((bread = read(desc, magic, MAGIC_BUFFER_SIZE)) == -1) {
cli_dbgmsg("Can't read from descriptor %d\n");
return CL_EIO;
} else if(bread < 2) {
/* short read - no need to do magic */
return CL_CLEAN; |
467f8b1e |
} |
e3aaff8e |
|
2b259453 |
lseek(desc, 0, SEEK_SET); |
467f8b1e |
type = cli_filetype(magic, bread); |
6d6e8271 |
|
3805ebcb |
type == CL_TYPE_MAIL ? (*mrec)++ : (*arec)++; |
888f5794 |
|
467f8b1e |
switch(type) { |
3805ebcb |
case CL_TYPE_RAR: |
467f8b1e |
if(!DISABLE_RAR && SCAN_ARCHIVE && !cli_scanrar_inuse) |
22275b15 |
ret = cli_scanrar(desc, virname, scanned, root, limits, options, arec, mrec); |
467f8b1e |
break; |
6d6e8271 |
|
3805ebcb |
case CL_TYPE_ZIP: |
467f8b1e |
if(SCAN_ARCHIVE) |
22275b15 |
ret = cli_scanzip(desc, virname, scanned, root, limits, options, arec, mrec); |
467f8b1e |
break; |
6d6e8271 |
|
3805ebcb |
case CL_TYPE_GZ: |
467f8b1e |
if(SCAN_ARCHIVE) |
22275b15 |
ret = cli_scangzip(desc, virname, scanned, root, limits, options, arec, mrec); |
467f8b1e |
break; |
6d6e8271 |
|
3805ebcb |
case CL_TYPE_BZ: |
68a6f51f |
#ifdef HAVE_BZLIB_H |
467f8b1e |
if(SCAN_ARCHIVE) |
22275b15 |
ret = cli_scanbzip(desc, virname, scanned, root, limits, options, arec, mrec); |
e3aaff8e |
#endif |
467f8b1e |
break; |
6d6e8271 |
|
3805ebcb |
case CL_TYPE_MSSZDD: |
341e5433 |
if(SCAN_ARCHIVE) |
3805ebcb |
ret = cli_scanszdd(desc, virname, scanned, root, limits, options, arec, mrec); |
341e5433 |
break;
|
3805ebcb |
case CL_TYPE_MSCAB: |
414abe87 |
if(SCAN_ARCHIVE) |
22275b15 |
ret = cli_scanmscab(desc, virname, scanned, root, limits, options, arec, mrec); |
414abe87 |
break;
|
3805ebcb |
case CL_TYPE_MAIL: |
467f8b1e |
if(SCAN_MAIL) |
22275b15 |
ret = cli_scanmail(desc, virname, scanned, root, limits, options, arec, mrec); |
467f8b1e |
break; |
6d6e8271 |
|
3805ebcb |
case CL_TYPE_MSCHM: |
a5373b64 |
if(SCAN_ARCHIVE)
ret = cli_scanmschm(desc, virname, scanned, root, limits, options, arec, mrec);
break;
|
3805ebcb |
case CL_TYPE_MSOLE2: |
467f8b1e |
if(SCAN_OLE2) |
22275b15 |
ret = cli_scanole2(desc, virname, scanned, root, limits, options, arec, mrec); |
467f8b1e |
break; |
6d6e8271 |
|
3805ebcb |
case CL_TYPE_TAR: |
2b259453 |
if(SCAN_ARCHIVE)
ret = cli_scantar(desc, virname, scanned, root, limits, options, arec, mrec); |
667ab9c6 |
break;
case CL_TYPE_BINHEX:
if(SCAN_ARCHIVE)
ret = cli_scanbinhex(desc, virname, scanned, root, limits, options, arec, mrec); |
2b259453 |
break;
|
3805ebcb |
case CL_TYPE_SCRENC: |
e57fa318 |
ret = cli_scanscrenc(desc, virname, scanned, root, limits, options, arec, mrec); |
eb308794 |
break;
case CL_TYPE_RIFF:
ret = cli_scanriff(desc, virname, scanned, root, limits, options, arec, mrec); |
e57fa318 |
break;
|
3805ebcb |
case CL_TYPE_DATA: |
467f8b1e |
/* it could be a false positive and a standard DOS .COM file */
{
struct stat s;
if(fstat(desc, &s) == 0 && S_ISREG(s.st_mode) && s.st_size < 65536) |
3805ebcb |
type = CL_TYPE_UNKNOWN_DATA; |
467f8b1e |
}
|
3805ebcb |
case CL_TYPE_UNKNOWN_DATA: |
c3a3be2d |
ret = cli_check_mydoom_log(desc, virname); |
467f8b1e |
break; |
216a697f |
default:
break; |
e3aaff8e |
}
|
3805ebcb |
type == CL_TYPE_MAIL ? (*mrec)-- : (*arec)--; |
467f8b1e |
|
3805ebcb |
if(type != CL_TYPE_DATA && ret != CL_VIRUS) { /* scan the raw file */ |
77e4bb11 |
int typerec; |
888f5794 |
|
3805ebcb |
type == CL_TYPE_UNKNOWN_TEXT ? (typerec = 1) : (typerec = 0); |
0c9d7f15 |
if(lseek(desc, 0, SEEK_SET) < 0)
cli_errmsg("lseek() failed, trying to continue anyway...\n"); |
22275b15 |
|
b68d11d2 |
if((nret = cli_scandesc(desc, virname, scanned, root, typerec, type)) == CL_VIRUS) { |
2b259453 |
cli_dbgmsg("%s found in descriptor %d.\n", *virname, desc); |
e3aaff8e |
return CL_VIRUS; |
888f5794 |
|
3862f7e1 |
} else if(nret < 0) {
return nret;
|
888f5794 |
} else if(nret >= CL_TYPENO) {
lseek(desc, 0, SEEK_SET);
|
6ee0243f |
nret == CL_TYPE_MAIL ? (*mrec)++ : (*arec)++; |
888f5794 |
switch(nret) { |
3805ebcb |
case CL_TYPE_HTML: |
888f5794 |
if(SCAN_HTML) |
22275b15 |
if(cli_scanhtml(desc, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) |
888f5794 |
return CL_VIRUS;
break; |
ece009c0 |
|
3805ebcb |
case CL_TYPE_MAIL: |
ece009c0 |
if(SCAN_MAIL) |
22275b15 |
if(cli_scanmail(desc, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) |
ece009c0 |
return CL_VIRUS;
break; |
888f5794 |
} |
6ee0243f |
nret == CL_TYPE_MAIL ? (*mrec)-- : (*arec)--; |
e3aaff8e |
} |
46c2e927 |
} |
e3aaff8e |
|
77e4bb11 |
(*arec)++;
lseek(desc, 0, SEEK_SET);
switch(type) {
/* Due to performance reasons all executables were first scanned
* in raw mode. Now we will try to unpack them
*/ |
3805ebcb |
case CL_TYPE_MSEXE: |
77e4bb11 |
if(SCAN_PE)
ret = cli_scanpe(desc, virname, scanned, root, limits, options, arec, mrec);
break; |
8000d078 |
default:
break; |
77e4bb11 |
}
(*arec)--;
|
322bfd03 |
if(ret == CL_EFORMAT) { |
6ee0243f |
cli_dbgmsg("Descriptor[%d]: %s\n", desc, cl_strerror(CL_EFORMAT)); |
322bfd03 |
return CL_CLEAN;
} else {
return ret;
} |
e3aaff8e |
}
|
3805ebcb |
int cl_scandesc(int desc, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options) |
e3aaff8e |
{ |
22275b15 |
int arec = 0, mrec = 0; |
e3aaff8e |
|
22275b15 |
return cli_magic_scandesc(desc, virname, scanned, root, limits, options, &arec, &mrec); |
e3aaff8e |
}
|
3805ebcb |
static int cli_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec) |
21cf4aeb |
{
int fd, ret;
|
a9082ea2 |
|
22275b15 |
/* internal version of cl_scanfile with arec/mrec preserved */ |
21cf4aeb |
if((fd = open(filename, O_RDONLY)) == -1)
return CL_EOPEN;
|
22275b15 |
ret = cli_magic_scandesc(fd, virname, scanned, root, limits, options, arec, mrec); |
21cf4aeb |
close(fd);
return ret;
}
|
3805ebcb |
int cl_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options) |
e3aaff8e |
{
int fd, ret;
|
ece009c0 |
|
e3aaff8e |
if((fd = open(filename, O_RDONLY)) == -1)
return CL_EOPEN;
ret = cl_scandesc(fd, virname, scanned, root, limits, options);
close(fd);
return ret;
} |