2007-10-30 18:53:25 +00:00
|
|
|
/*
|
2024-01-12 17:03:59 -05:00
|
|
|
* Copyright (C) 2013-2024 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
|
2013-10-31 12:54:50 -04:00
|
|
|
* Copyright (C) 2007-2013 Sourcefire, Inc.
|
2008-04-02 15:24:51 +00:00
|
|
|
*
|
|
|
|
* Authors: Alberto Wu
|
2007-10-30 18:53:25 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2007-10-31 16:26:37 +00:00
|
|
|
|
|
|
|
#if HAVE_STRING_H
|
2007-10-30 18:53:25 +00:00
|
|
|
#include <string.h>
|
2007-10-31 16:26:37 +00:00
|
|
|
#endif
|
2007-10-30 18:53:25 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-01 19:38:01 -04:00
|
|
|
#include "clamav.h"
|
2007-10-30 18:53:25 +00:00
|
|
|
#include "others.h"
|
|
|
|
#include "scanners.h"
|
2008-08-21 20:21:43 +00:00
|
|
|
#include "autoit.h"
|
2010-07-29 02:41:15 +02:00
|
|
|
#include "fmap.h"
|
2013-10-31 11:12:11 -07:00
|
|
|
#include "fpu.h"
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2013-10-31 12:54:50 -04:00
|
|
|
static int fpu_words = FPU_ENDIAN_INITME;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
const char *autoit_functions[] = {
|
|
|
|
"ABS",
|
|
|
|
"ACOS",
|
|
|
|
"ADLIBREGISTER",
|
|
|
|
"ADLIBUNREGISTER",
|
|
|
|
"ASC",
|
|
|
|
"ASCW",
|
|
|
|
"ASIN",
|
|
|
|
"ASSIGN",
|
|
|
|
"ATAN",
|
|
|
|
"AUTOITSETOPTION",
|
|
|
|
"AUTOITWINGETTITLE",
|
|
|
|
"AUTOITWINSETTITLE",
|
|
|
|
"BEEP",
|
|
|
|
"BINARY",
|
|
|
|
"BINARYLEN",
|
|
|
|
"BINARYMID",
|
|
|
|
"BINARYTOSTRING",
|
|
|
|
"BITAND",
|
|
|
|
"BITNOT",
|
|
|
|
"BITOR",
|
|
|
|
"BITROTATE",
|
|
|
|
"BITSHIFT",
|
|
|
|
"BITXOR",
|
|
|
|
"BLOCKINPUT",
|
|
|
|
"BREAK",
|
|
|
|
"CALL",
|
|
|
|
"CDTRAY",
|
|
|
|
"CEILING",
|
|
|
|
"CHR",
|
|
|
|
"CHRW",
|
|
|
|
"CLIPGET",
|
|
|
|
"CLIPPUT",
|
|
|
|
"CONSOLEREAD",
|
|
|
|
"CONSOLEWRITE",
|
|
|
|
"CONSOLEWRITEERROR",
|
|
|
|
"CONTROLCLICK",
|
|
|
|
"CONTROLCOMMAND",
|
|
|
|
"CONTROLDISABLE",
|
|
|
|
"CONTROLENABLE",
|
|
|
|
"CONTROLFOCUS",
|
|
|
|
"CONTROLGETFOCUS",
|
|
|
|
"CONTROLGETHANDLE",
|
|
|
|
"CONTROLGETPOS",
|
|
|
|
"CONTROLGETTEXT",
|
|
|
|
"CONTROLHIDE",
|
|
|
|
"CONTROLLISTVIEW",
|
|
|
|
"CONTROLMOVE",
|
|
|
|
"CONTROLSEND",
|
|
|
|
"CONTROLSETTEXT",
|
|
|
|
"CONTROLSHOW",
|
|
|
|
"CONTROLTREEVIEW",
|
|
|
|
"COS",
|
|
|
|
"DEC",
|
|
|
|
"DIRCOPY",
|
|
|
|
"DIRCREATE",
|
|
|
|
"DIRGETSIZE",
|
|
|
|
"DIRMOVE",
|
|
|
|
"DIRREMOVE",
|
|
|
|
"DLLCALL",
|
|
|
|
"DLLCALLADDRESS",
|
|
|
|
"DLLCALLBACKFREE",
|
|
|
|
"DLLCALLBACKGETPTR",
|
|
|
|
"DLLCALLBACKREGISTER",
|
|
|
|
"DLLCLOSE",
|
|
|
|
"DLLOPEN",
|
|
|
|
"DLLSTRUCTCREATE",
|
|
|
|
"DLLSTRUCTGETDATA",
|
|
|
|
"DLLSTRUCTGETPTR",
|
|
|
|
"DLLSTRUCTGETSIZE",
|
|
|
|
"DLLSTRUCTSETDATA",
|
|
|
|
"DRIVEGETDRIVE",
|
|
|
|
"DRIVEGETFILESYSTEM",
|
|
|
|
"DRIVEGETLABEL",
|
|
|
|
"DRIVEGETSERIAL",
|
|
|
|
"DRIVEGETTYPE",
|
|
|
|
"DRIVEMAPADD",
|
|
|
|
"DRIVEMAPDEL",
|
|
|
|
"DRIVEMAPGET",
|
|
|
|
"DRIVESETLABEL",
|
|
|
|
"DRIVESPACEFREE",
|
|
|
|
"DRIVESPACETOTAL",
|
|
|
|
"DRIVESTATUS",
|
2021-04-02 22:15:00 -07:00
|
|
|
"DUMMYSPEEDTEST",
|
2020-02-17 10:14:01 -08:00
|
|
|
"ENVGET",
|
|
|
|
"ENVSET",
|
|
|
|
"ENVUPDATE",
|
|
|
|
"EVAL",
|
|
|
|
"EXECUTE",
|
|
|
|
"EXP",
|
|
|
|
"FILECHANGEDIR",
|
|
|
|
"FILECLOSE",
|
|
|
|
"FILECOPY",
|
|
|
|
"FILECREATENTFSLINK",
|
|
|
|
"FILECREATESHORTCUT",
|
|
|
|
"FILEDELETE",
|
|
|
|
"FILEEXISTS",
|
|
|
|
"FILEFINDFIRSTFILE",
|
|
|
|
"FILEFINDNEXTFILE",
|
|
|
|
"FILEFLUSH",
|
|
|
|
"FILEGETATTRIB",
|
|
|
|
"FILEGETENCODING",
|
|
|
|
"FILEGETLONGNAME",
|
|
|
|
"FILEGETPOS",
|
|
|
|
"FILEGETSHORTCUT",
|
|
|
|
"FILEGETSHORTNAME",
|
|
|
|
"FILEGETSIZE",
|
|
|
|
"FILEGETTIME",
|
|
|
|
"FILEGETVERSION",
|
|
|
|
"FILEINSTALL",
|
|
|
|
"FILEMOVE",
|
|
|
|
"FILEOPEN",
|
|
|
|
"FILEOPENDIALOG",
|
|
|
|
"FILEREAD",
|
|
|
|
"FILEREADLINE",
|
|
|
|
"FILEREADTOARRAY",
|
|
|
|
"FILERECYCLE",
|
|
|
|
"FILERECYCLEEMPTY",
|
|
|
|
"FILESAVEDIALOG",
|
|
|
|
"FILESELECTFOLDER",
|
|
|
|
"FILESETATTRIB",
|
|
|
|
"FILESETEND",
|
|
|
|
"FILESETPOS",
|
|
|
|
"FILESETTIME",
|
|
|
|
"FILEWRITE",
|
|
|
|
"FILEWRITELINE",
|
|
|
|
"FLOOR",
|
|
|
|
"FTPSETPROXY",
|
|
|
|
"FUNCNAME",
|
|
|
|
"GUICREATE",
|
|
|
|
"GUICTRLCREATEAVI",
|
|
|
|
"GUICTRLCREATEBUTTON",
|
|
|
|
"GUICTRLCREATECHECKBOX",
|
|
|
|
"GUICTRLCREATECOMBO",
|
|
|
|
"GUICTRLCREATECONTEXTMENU",
|
|
|
|
"GUICTRLCREATEDATE",
|
|
|
|
"GUICTRLCREATEDUMMY",
|
|
|
|
"GUICTRLCREATEEDIT",
|
|
|
|
"GUICTRLCREATEGRAPHIC",
|
|
|
|
"GUICTRLCREATEGROUP",
|
|
|
|
"GUICTRLCREATEICON",
|
|
|
|
"GUICTRLCREATEINPUT",
|
|
|
|
"GUICTRLCREATELABEL",
|
|
|
|
"GUICTRLCREATELIST",
|
|
|
|
"GUICTRLCREATELISTVIEW",
|
|
|
|
"GUICTRLCREATELISTVIEWITEM",
|
|
|
|
"GUICTRLCREATEMENU",
|
|
|
|
"GUICTRLCREATEMENUITEM",
|
|
|
|
"GUICTRLCREATEMONTHCAL",
|
|
|
|
"GUICTRLCREATEOBJ",
|
|
|
|
"GUICTRLCREATEPIC",
|
|
|
|
"GUICTRLCREATEPROGRESS",
|
|
|
|
"GUICTRLCREATERADIO",
|
|
|
|
"GUICTRLCREATESLIDER",
|
|
|
|
"GUICTRLCREATETAB",
|
|
|
|
"GUICTRLCREATETABITEM",
|
|
|
|
"GUICTRLCREATETREEVIEW",
|
|
|
|
"GUICTRLCREATETREEVIEWITEM",
|
|
|
|
"GUICTRLCREATEUPDOWN",
|
|
|
|
"GUICTRLDELETE",
|
|
|
|
"GUICTRLGETHANDLE",
|
|
|
|
"GUICTRLGETSTATE",
|
|
|
|
"GUICTRLREAD",
|
|
|
|
"GUICTRLRECVMSG",
|
|
|
|
"GUICTRLREGISTERLISTVIEWSORT",
|
|
|
|
"GUICTRLSENDMSG",
|
|
|
|
"GUICTRLSENDTODUMMY",
|
|
|
|
"GUICTRLSETBKCOLOR",
|
|
|
|
"GUICTRLSETCOLOR",
|
|
|
|
"GUICTRLSETCURSOR",
|
|
|
|
"GUICTRLSETDATA",
|
|
|
|
"GUICTRLSETDEFBKCOLOR",
|
|
|
|
"GUICTRLSETDEFCOLOR",
|
|
|
|
"GUICTRLSETFONT",
|
|
|
|
"GUICTRLSETGRAPHIC",
|
|
|
|
"GUICTRLSETIMAGE",
|
|
|
|
"GUICTRLSETLIMIT",
|
|
|
|
"GUICTRLSETONEVENT",
|
|
|
|
"GUICTRLSETPOS",
|
|
|
|
"GUICTRLSETRESIZING",
|
|
|
|
"GUICTRLSETSTATE",
|
|
|
|
"GUICTRLSETSTYLE",
|
|
|
|
"GUICTRLSETTIP",
|
|
|
|
"GUIDELETE",
|
|
|
|
"GUIGETCURSORINFO",
|
|
|
|
"GUIGETMSG",
|
|
|
|
"GUIGETSTYLE",
|
|
|
|
"GUIREGISTERMSG",
|
|
|
|
"GUISETACCELERATORS",
|
|
|
|
"GUISETBKCOLOR",
|
|
|
|
"GUISETCOORD",
|
|
|
|
"GUISETCURSOR",
|
|
|
|
"GUISETFONT",
|
|
|
|
"GUISETHELP",
|
|
|
|
"GUISETICON",
|
|
|
|
"GUISETONEVENT",
|
|
|
|
"GUISETSTATE",
|
|
|
|
"GUISETSTYLE",
|
|
|
|
"GUISTARTGROUP",
|
|
|
|
"GUISWITCH",
|
|
|
|
"HEX",
|
|
|
|
"HOTKEYSET",
|
|
|
|
"HTTPSETPROXY",
|
|
|
|
"HTTPSETUSERAGENT",
|
|
|
|
"HWND",
|
|
|
|
"INETCLOSE",
|
|
|
|
"INETGET",
|
|
|
|
"INETGETINFO",
|
|
|
|
"INETGETSIZE",
|
|
|
|
"INETREAD",
|
|
|
|
"INIDELETE",
|
|
|
|
"INIREAD",
|
|
|
|
"INIREADSECTION",
|
|
|
|
"INIREADSECTIONNAMES",
|
|
|
|
"INIRENAMESECTION",
|
|
|
|
"INIWRITE",
|
|
|
|
"INIWRITESECTION",
|
|
|
|
"INPUTBOX",
|
|
|
|
"INT",
|
|
|
|
"ISADMIN",
|
|
|
|
"ISARRAY",
|
|
|
|
"ISBINARY",
|
|
|
|
"ISBOOL",
|
|
|
|
"ISDECLARED",
|
|
|
|
"ISDLLSTRUCT",
|
|
|
|
"ISFLOAT",
|
|
|
|
"ISFUNC",
|
|
|
|
"ISHWND",
|
|
|
|
"ISINT",
|
|
|
|
"ISKEYWORD",
|
2021-04-02 22:15:00 -07:00
|
|
|
"ISMAP",
|
2020-02-17 10:14:01 -08:00
|
|
|
"ISNUMBER",
|
|
|
|
"ISOBJ",
|
|
|
|
"ISPTR",
|
|
|
|
"ISSTRING",
|
|
|
|
"LOG",
|
2021-04-02 22:15:00 -07:00
|
|
|
"MAPAPPEND",
|
|
|
|
"MAPEXISTS",
|
|
|
|
"MAPKEYS",
|
|
|
|
"MAPREMOVE",
|
2020-02-17 10:14:01 -08:00
|
|
|
"MEMGETSTATS",
|
|
|
|
"MOD",
|
|
|
|
"MOUSECLICK",
|
|
|
|
"MOUSECLICKDRAG",
|
|
|
|
"MOUSEDOWN",
|
|
|
|
"MOUSEGETCURSOR",
|
|
|
|
"MOUSEGETPOS",
|
|
|
|
"MOUSEMOVE",
|
|
|
|
"MOUSEUP",
|
|
|
|
"MOUSEWHEEL",
|
|
|
|
"MSGBOX",
|
|
|
|
"NUMBER",
|
|
|
|
"OBJCREATE",
|
|
|
|
"OBJCREATEINTERFACE",
|
|
|
|
"OBJEVENT",
|
|
|
|
"OBJGET",
|
|
|
|
"OBJNAME",
|
|
|
|
"ONAUTOITEXITREGISTER",
|
|
|
|
"ONAUTOITEXITUNREGISTER",
|
|
|
|
"OPT",
|
|
|
|
"PING",
|
|
|
|
"PIXELCHECKSUM",
|
|
|
|
"PIXELGETCOLOR",
|
|
|
|
"PIXELSEARCH",
|
|
|
|
"PROCESSCLOSE",
|
|
|
|
"PROCESSEXISTS",
|
|
|
|
"PROCESSGETSTATS",
|
|
|
|
"PROCESSLIST",
|
|
|
|
"PROCESSSETPRIORITY",
|
|
|
|
"PROCESSWAIT",
|
|
|
|
"PROCESSWAITCLOSE",
|
|
|
|
"PROGRESSOFF",
|
|
|
|
"PROGRESSON",
|
|
|
|
"PROGRESSSET",
|
|
|
|
"PTR",
|
|
|
|
"RANDOM",
|
|
|
|
"REGDELETE",
|
|
|
|
"REGENUMKEY",
|
|
|
|
"REGENUMVAL",
|
|
|
|
"REGREAD",
|
|
|
|
"REGWRITE",
|
|
|
|
"ROUND",
|
|
|
|
"RUN",
|
|
|
|
"RUNAS",
|
|
|
|
"RUNASWAIT",
|
|
|
|
"RUNWAIT",
|
|
|
|
"SEND",
|
|
|
|
"SENDKEEPACTIVE",
|
|
|
|
"SETERROR",
|
|
|
|
"SETEXTENDED",
|
|
|
|
"SHELLEXECUTE",
|
|
|
|
"SHELLEXECUTEWAIT",
|
|
|
|
"SHUTDOWN",
|
|
|
|
"SIN",
|
|
|
|
"SLEEP",
|
|
|
|
"SOUNDPLAY",
|
|
|
|
"SOUNDSETWAVEVOLUME",
|
|
|
|
"SPLASHIMAGEON",
|
|
|
|
"SPLASHOFF",
|
|
|
|
"SPLASHTEXTON",
|
|
|
|
"SQRT",
|
|
|
|
"SRANDOM",
|
|
|
|
"STATUSBARGETTEXT",
|
|
|
|
"STDERRREAD",
|
|
|
|
"STDINWRITE",
|
|
|
|
"STDIOCLOSE",
|
|
|
|
"STDOUTREAD",
|
|
|
|
"STRING",
|
|
|
|
"STRINGADDCR",
|
|
|
|
"STRINGCOMPARE",
|
|
|
|
"STRINGFORMAT",
|
|
|
|
"STRINGFROMASCIIARRAY",
|
|
|
|
"STRINGINSTR",
|
|
|
|
"STRINGISALNUM",
|
|
|
|
"STRINGISALPHA",
|
|
|
|
"STRINGISASCII",
|
|
|
|
"STRINGISDIGIT",
|
|
|
|
"STRINGISFLOAT",
|
|
|
|
"STRINGISINT",
|
|
|
|
"STRINGISLOWER",
|
|
|
|
"STRINGISSPACE",
|
|
|
|
"STRINGISUPPER",
|
|
|
|
"STRINGISXDIGIT",
|
|
|
|
"STRINGLEFT",
|
|
|
|
"STRINGLEN",
|
|
|
|
"STRINGLOWER",
|
|
|
|
"STRINGMID",
|
|
|
|
"STRINGREGEXP",
|
|
|
|
"STRINGREGEXPREPLACE",
|
|
|
|
"STRINGREPLACE",
|
|
|
|
"STRINGREVERSE",
|
|
|
|
"STRINGRIGHT",
|
|
|
|
"STRINGSPLIT",
|
|
|
|
"STRINGSTRIPCR",
|
|
|
|
"STRINGSTRIPWS",
|
|
|
|
"STRINGTOASCIIARRAY",
|
|
|
|
"STRINGTOBINARY",
|
|
|
|
"STRINGTRIMLEFT",
|
|
|
|
"STRINGTRIMRIGHT",
|
|
|
|
"STRINGUPPER",
|
|
|
|
"TAN",
|
|
|
|
"TCPACCEPT",
|
|
|
|
"TCPCLOSESOCKET",
|
|
|
|
"TCPCONNECT",
|
|
|
|
"TCPLISTEN",
|
|
|
|
"TCPNAMETOIP",
|
|
|
|
"TCPRECV",
|
|
|
|
"TCPSEND",
|
2021-04-02 22:15:00 -07:00
|
|
|
"TCPSHUTDOWN",
|
|
|
|
"TCPSTARTUP",
|
2020-02-17 10:14:01 -08:00
|
|
|
"TIMERDIFF",
|
|
|
|
"TIMERINIT",
|
|
|
|
"TOOLTIP",
|
|
|
|
"TRAYCREATEITEM",
|
|
|
|
"TRAYCREATEMENU",
|
|
|
|
"TRAYGETMSG",
|
|
|
|
"TRAYITEMDELETE",
|
|
|
|
"TRAYITEMGETHANDLE",
|
|
|
|
"TRAYITEMGETSTATE",
|
|
|
|
"TRAYITEMGETTEXT",
|
|
|
|
"TRAYITEMSETONEVENT",
|
|
|
|
"TRAYITEMSETSTATE",
|
|
|
|
"TRAYITEMSETTEXT",
|
|
|
|
"TRAYSETCLICK",
|
|
|
|
"TRAYSETICON",
|
|
|
|
"TRAYSETONEVENT",
|
|
|
|
"TRAYSETPAUSEICON",
|
|
|
|
"TRAYSETSTATE",
|
|
|
|
"TRAYSETTOOLTIP",
|
|
|
|
"TRAYTIP",
|
|
|
|
"UBOUND",
|
|
|
|
"UDPBIND",
|
|
|
|
"UDPCLOSESOCKET",
|
|
|
|
"UDPOPEN",
|
|
|
|
"UDPRECV",
|
|
|
|
"UDPSEND",
|
2021-04-02 22:15:00 -07:00
|
|
|
"UDPSHUTDOWN",
|
|
|
|
"UDPSTARTUP",
|
2020-02-17 10:14:01 -08:00
|
|
|
"VARGETTYPE",
|
|
|
|
"WINACTIVATE",
|
|
|
|
"WINACTIVE",
|
|
|
|
"WINCLOSE",
|
|
|
|
"WINEXISTS",
|
|
|
|
"WINFLASH",
|
|
|
|
"WINGETCARETPOS",
|
|
|
|
"WINGETCLASSLIST",
|
|
|
|
"WINGETCLIENTSIZE",
|
|
|
|
"WINGETHANDLE",
|
|
|
|
"WINGETPOS",
|
|
|
|
"WINGETPROCESS",
|
|
|
|
"WINGETSTATE",
|
|
|
|
"WINGETTEXT",
|
|
|
|
"WINGETTITLE",
|
|
|
|
"WINKILL",
|
|
|
|
"WINLIST",
|
|
|
|
"WINMENUSELECTITEM",
|
|
|
|
"WINMINIMIZEALL",
|
|
|
|
"WINMINIMIZEALLUNDO",
|
|
|
|
"WINMOVE",
|
|
|
|
"WINSETONTOP",
|
|
|
|
"WINSETSTATE",
|
|
|
|
"WINSETTITLE",
|
|
|
|
"WINSETTRANS",
|
|
|
|
"WINWAIT",
|
|
|
|
"WINWAITACTIVE",
|
|
|
|
"WINWAITCLOSE",
|
2020-03-21 14:15:28 -04:00
|
|
|
"WINWAITNOTACTIVE"};
|
2020-02-17 10:14:01 -08:00
|
|
|
|
|
|
|
const char *autoit_keywords[] = {
|
2021-03-31 18:04:13 -07:00
|
|
|
"UNKNOWN_0", // "".
|
2020-02-17 10:14:01 -08:00
|
|
|
"AND",
|
|
|
|
"OR",
|
|
|
|
"NOT",
|
|
|
|
"IF",
|
|
|
|
"THEN",
|
|
|
|
"ELSE",
|
|
|
|
"ELSEIF",
|
|
|
|
"ENDIF",
|
|
|
|
"WHILE",
|
|
|
|
"WEND",
|
|
|
|
"DO",
|
|
|
|
"UNTIL",
|
|
|
|
"FOR",
|
|
|
|
"NEXT",
|
|
|
|
"TO",
|
|
|
|
"STEP",
|
|
|
|
"IN",
|
|
|
|
"EXITLOOP",
|
|
|
|
"CONTINUELOOP",
|
|
|
|
"SELECT",
|
|
|
|
"CASE",
|
|
|
|
"ENDSELECT",
|
|
|
|
"SWITCH",
|
|
|
|
"ENDSWITCH",
|
|
|
|
"CONTINUECASE",
|
|
|
|
"DIM",
|
|
|
|
"REDIM",
|
|
|
|
"LOCAL",
|
|
|
|
"GLOBAL",
|
|
|
|
"CONST",
|
|
|
|
"STATIC",
|
|
|
|
"FUNC",
|
|
|
|
"ENDFUNC",
|
|
|
|
"RETURN",
|
|
|
|
"EXIT",
|
|
|
|
"BYREF",
|
|
|
|
"WITH",
|
|
|
|
"ENDWITH",
|
|
|
|
"TRUE",
|
|
|
|
"FALSE",
|
|
|
|
"DEFAULT",
|
|
|
|
"NULL",
|
2021-03-31 18:04:13 -07:00
|
|
|
"VOLATILE",
|
2020-02-17 10:14:01 -08:00
|
|
|
"ENUM",
|
|
|
|
};
|
|
|
|
|
2007-10-30 18:53:25 +00:00
|
|
|
/* FIXME: use unicode detection and normalization from edwin */
|
2018-12-03 12:40:13 -05:00
|
|
|
static unsigned int u2a(uint8_t *dest, unsigned int len)
|
|
|
|
{
|
|
|
|
uint8_t *src = dest;
|
|
|
|
unsigned int i, j;
|
2007-11-01 03:40:22 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (len < 2)
|
|
|
|
return len;
|
2007-11-01 03:40:22 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (len > 4 && src[0] == 0xff && src[1] == 0xfe && src[2]) {
|
|
|
|
len -= 2;
|
|
|
|
src += 2;
|
|
|
|
} else {
|
|
|
|
unsigned int cnt = 0;
|
|
|
|
j = (len > 20) ? 20 : (len & ~1);
|
|
|
|
|
|
|
|
for (i = 0; i < j; i += 2)
|
|
|
|
cnt += (src[i] != 0 && src[i + 1] == 0);
|
|
|
|
|
|
|
|
if (cnt * 4 < j)
|
|
|
|
return len;
|
|
|
|
}
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
j = len;
|
|
|
|
len >>= 1;
|
|
|
|
for (i = 0; i < j; i += 2)
|
|
|
|
*dest++ = src[i];
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2007-11-01 21:53:50 +00:00
|
|
|
|
2007-10-30 18:53:25 +00:00
|
|
|
/*********************
|
2024-01-19 09:08:36 -08:00
|
|
|
MT related stuff
|
2007-10-30 18:53:25 +00:00
|
|
|
*********************/
|
|
|
|
|
|
|
|
struct MT {
|
2018-12-03 12:40:13 -05:00
|
|
|
uint32_t *next;
|
|
|
|
uint32_t items;
|
|
|
|
uint32_t mt[624];
|
2007-10-30 18:53:25 +00:00
|
|
|
};
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
static uint8_t MT_getnext(struct MT *MT)
|
|
|
|
{
|
|
|
|
uint32_t r;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!--MT->items) {
|
|
|
|
uint32_t *mt = MT->mt;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
MT->items = 624;
|
|
|
|
MT->next = mt;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
for (i = 0; i < 227; i++)
|
|
|
|
mt[i] = ((((mt[i] ^ mt[i + 1]) & 0x7ffffffe) ^ mt[i]) >> 1) ^ ((0 - (mt[i + 1] & 1)) & 0x9908b0df) ^ mt[i + 397];
|
|
|
|
for (; i < 623; i++)
|
|
|
|
mt[i] = ((((mt[i] ^ mt[i + 1]) & 0x7ffffffe) ^ mt[i]) >> 1) ^ ((0 - (mt[i + 1] & 1)) & 0x9908b0df) ^ mt[i - 227];
|
|
|
|
mt[623] = ((((mt[623] ^ mt[0]) & 0x7ffffffe) ^ mt[623]) >> 1) ^ ((0 - (mt[0] & 1)) & 0x9908b0df) ^ mt[i - 227];
|
|
|
|
}
|
|
|
|
|
|
|
|
r = *(MT->next++);
|
|
|
|
r ^= (r >> 11);
|
|
|
|
r ^= ((r & 0xff3a58ad) << 7);
|
|
|
|
r ^= ((r & 0xffffdf8c) << 15);
|
|
|
|
r ^= (r >> 18);
|
|
|
|
return (uint8_t)(r >> 1);
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
static void MT_decrypt(uint8_t *buf, unsigned int size, uint32_t seed)
|
|
|
|
{
|
|
|
|
struct MT MT;
|
|
|
|
unsigned int i;
|
|
|
|
uint32_t *mt = MT.mt;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
*mt = seed;
|
|
|
|
for (i = 1; i < 624; i++)
|
|
|
|
mt[i] = i + 0x6c078965 * ((mt[i - 1] >> 30) ^ mt[i - 1]);
|
|
|
|
MT.items = 1;
|
|
|
|
MT.next = MT.mt;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
while (size--)
|
|
|
|
*buf++ ^= MT_getnext(&MT);
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************
|
2019-05-04 15:54:54 -04:00
|
|
|
inflate stuff
|
2007-10-30 18:53:25 +00:00
|
|
|
*********************/
|
|
|
|
|
|
|
|
struct UNP {
|
2018-12-03 12:40:13 -05:00
|
|
|
uint8_t *outputbuf;
|
|
|
|
uint8_t *inputbuf;
|
|
|
|
uint32_t cur_output;
|
|
|
|
uint32_t cur_input;
|
|
|
|
uint32_t usize;
|
|
|
|
uint32_t csize;
|
|
|
|
uint32_t bits_avail;
|
|
|
|
union {
|
|
|
|
uint32_t full;
|
|
|
|
struct {
|
2007-10-30 18:53:25 +00:00
|
|
|
#if WORDS_BIGENDIAN != 0
|
2018-12-03 12:40:13 -05:00
|
|
|
uint16_t h; /* BE */
|
|
|
|
uint16_t l;
|
2007-10-30 18:53:25 +00:00
|
|
|
#else
|
2018-12-03 12:40:13 -05:00
|
|
|
uint16_t l; /* LE */
|
|
|
|
uint16_t h;
|
2007-10-30 18:53:25 +00:00
|
|
|
#endif
|
2018-12-03 12:40:13 -05:00
|
|
|
} half;
|
|
|
|
} bitmap;
|
|
|
|
uint32_t error;
|
2007-10-30 18:53:25 +00:00
|
|
|
};
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
static uint32_t getbits(struct UNP *UNP, uint32_t size)
|
|
|
|
{
|
2022-02-16 00:13:55 +01:00
|
|
|
// cli_dbgmsg("In getbits, (size: %u, bits_avail: %u, UNP->cur_input: %u)\n", size, UNP->bits_avail, UNP->cur_input);
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP->bitmap.half.h = 0;
|
|
|
|
if (size > UNP->bits_avail && ((size - UNP->bits_avail - 1) / 16 + 1) * 2 > UNP->csize - UNP->cur_input) {
|
|
|
|
cli_dbgmsg("autoit: getbits() - not enough bits available\n");
|
|
|
|
UNP->error = 1;
|
|
|
|
return 0; /* won't infloop nor spam */
|
|
|
|
}
|
|
|
|
while (size) {
|
|
|
|
if (!UNP->bits_avail) {
|
2022-02-16 00:13:55 +01:00
|
|
|
// cli_dbgmsg("cur_input: %u (size: %u)\n", UNP->cur_input, size);
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP->bitmap.half.l |= UNP->inputbuf[UNP->cur_input++] << 8;
|
|
|
|
UNP->bitmap.half.l |= UNP->inputbuf[UNP->cur_input++];
|
|
|
|
UNP->bits_avail = 16;
|
|
|
|
}
|
|
|
|
UNP->bitmap.full <<= 1;
|
|
|
|
UNP->bits_avail--;
|
|
|
|
size--;
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
return (uint32_t)UNP->bitmap.half.h;
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************
|
2019-05-04 15:54:54 -04:00
|
|
|
autoit3 EA05 handler
|
2007-10-30 18:53:25 +00:00
|
|
|
*********************/
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
static cl_error_t ea05(cli_ctx *ctx, const uint8_t *base, char *tmpd)
|
2018-12-03 12:40:13 -05:00
|
|
|
{
|
2022-08-09 16:56:54 -07:00
|
|
|
cl_error_t status = CL_SUCCESS;
|
|
|
|
cl_error_t ret;
|
|
|
|
uint8_t b[300];
|
|
|
|
uint8_t comp;
|
2018-12-03 12:40:13 -05:00
|
|
|
uint32_t s, m4sum = 0;
|
2022-08-09 16:56:54 -07:00
|
|
|
int i;
|
|
|
|
unsigned int files = 0;
|
|
|
|
char tempfile[1024] = {0};
|
|
|
|
int tempfd = -1;
|
|
|
|
struct UNP UNP = {0};
|
|
|
|
fmap_t *map = ctx->fmap;
|
|
|
|
|
|
|
|
if (!fmap_need_ptr_once(map, base, 16)) {
|
|
|
|
goto done;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
m4sum += *base++;
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
// While we have not exceeded the max files limit or the max time limit...
|
|
|
|
while (CL_SUCCESS == (status = cli_checklimits("autoit", ctx, 0, 0, 0))) {
|
|
|
|
if (!fmap_need_ptr_once(map, base, 8)) {
|
|
|
|
goto done;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
/* MT_decrypt(buf,4,0x16fa); waste of time */
|
|
|
|
if ((uint32_t)cli_readint32(base) != 0xceb06dff) {
|
|
|
|
cli_dbgmsg("autoit: no FILE magic found, extraction complete\n");
|
2022-08-09 16:56:54 -07:00
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
s = cli_readint32(base + 4) ^ 0x29bc;
|
2022-08-09 16:56:54 -07:00
|
|
|
if ((int32_t)s < 0) {
|
|
|
|
/* the original code wouldn't seek back here */
|
|
|
|
goto done;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
base += 8;
|
|
|
|
if (cli_debug_flag && s < sizeof(b)) {
|
2022-08-09 16:56:54 -07:00
|
|
|
if (!fmap_need_ptr_once(map, base, s)) {
|
|
|
|
goto done;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
memcpy(b, base, s);
|
|
|
|
MT_decrypt(b, s, s + 0xa25e);
|
|
|
|
b[s] = '\0';
|
|
|
|
cli_dbgmsg("autoit: magic string '%s'\n", b);
|
|
|
|
}
|
|
|
|
base += s;
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
if (!fmap_need_ptr_once(map, base, 4)) {
|
|
|
|
goto done;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
s = cli_readint32(base) ^ 0x29ac;
|
2022-08-09 16:56:54 -07:00
|
|
|
if ((int32_t)s < 0) {
|
|
|
|
/* the original code wouldn't seek back here */
|
|
|
|
goto done;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
base += 4;
|
|
|
|
if (cli_debug_flag && s < sizeof(b)) {
|
2022-08-09 16:56:54 -07:00
|
|
|
if (!fmap_need_ptr_once(map, base, s)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
memcpy(b, base, s);
|
|
|
|
MT_decrypt(b, s, s + 0xf25e);
|
|
|
|
b[s] = '\0';
|
|
|
|
cli_dbgmsg("autoit: original filename '%s'\n", b);
|
|
|
|
}
|
|
|
|
base += s;
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
if (!fmap_need_ptr_once(map, base, 13)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
comp = *base;
|
|
|
|
UNP.csize = cli_readint32(base + 1) ^ 0x45aa;
|
|
|
|
if ((int32_t)UNP.csize < 0) {
|
|
|
|
cli_dbgmsg("autoit: bad file size - giving up\n");
|
2022-08-09 16:56:54 -07:00
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!UNP.csize) {
|
|
|
|
cli_dbgmsg("autoit: skipping empty file\n");
|
|
|
|
base += 13 + 16;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cli_dbgmsg("autoit: compressed size: %x\n", UNP.csize);
|
|
|
|
cli_dbgmsg("autoit: advertised uncompressed size %x\n", cli_readint32(base + 5) ^ 0x45aa);
|
|
|
|
cli_dbgmsg("autoit: ref chksum: %x\n", cli_readint32(base + 9) ^ 0xc3d2);
|
|
|
|
|
|
|
|
base += 13 + 16;
|
|
|
|
|
|
|
|
if (cli_checklimits("autoit", ctx, UNP.csize, 0, 0) != CL_CLEAN) {
|
|
|
|
base += UNP.csize;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comp == 1 && UNP.csize < sizeof(union unaligned_32)) {
|
|
|
|
cli_dbgmsg("autoit: compressed size too small, skipping\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(UNP.inputbuf = cli_max_malloc(UNP.csize))) {
|
2022-08-09 16:56:54 -07:00
|
|
|
status = CL_EMEM;
|
|
|
|
goto done;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!fmap_need_ptr_once(map, base, UNP.csize)) {
|
|
|
|
cli_dbgmsg("autoit: failed to read compressed stream. broken/truncated file?\n");
|
2022-08-09 16:56:54 -07:00
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
memcpy(UNP.inputbuf, base, UNP.csize);
|
|
|
|
base += UNP.csize;
|
|
|
|
MT_decrypt(UNP.inputbuf, UNP.csize, 0x22af + m4sum);
|
|
|
|
|
|
|
|
if (comp == 1) {
|
2022-08-09 16:56:54 -07:00
|
|
|
/*
|
|
|
|
* File is compressed. Decompress!
|
|
|
|
*/
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: file is compressed\n");
|
|
|
|
if (cli_readint32(UNP.inputbuf) != 0x35304145) {
|
|
|
|
cli_dbgmsg("autoit: bad magic or unsupported version\n");
|
2023-04-17 11:39:18 -07:00
|
|
|
// Free this inputbuf and set back to NULL.
|
|
|
|
free(UNP.inputbuf);
|
|
|
|
UNP.inputbuf = NULL;
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
if (!(UNP.usize = be32_to_host(*(uint32_t *)(UNP.inputbuf + 4)))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.usize = UNP.csize; /* only a specifically crafted or badly corrupted sample should land here */
|
2022-08-09 16:56:54 -07:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (cli_checklimits("autoit", ctx, UNP.usize, 0, 0) != CL_CLEAN) {
|
2023-04-17 11:39:18 -07:00
|
|
|
// Free this inputbuf and set back to NULL.
|
|
|
|
free(UNP.inputbuf);
|
|
|
|
UNP.inputbuf = NULL;
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(UNP.outputbuf = cli_max_malloc(UNP.usize))) {
|
2022-08-09 16:56:54 -07:00
|
|
|
status = CL_EMEM;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: uncompressed size again: %x\n", UNP.usize);
|
|
|
|
|
|
|
|
UNP.cur_output = 0;
|
|
|
|
UNP.cur_input = 8;
|
|
|
|
UNP.bitmap.full = 0;
|
|
|
|
UNP.bits_avail = 0;
|
|
|
|
UNP.error = 0;
|
|
|
|
|
|
|
|
while (!UNP.error && UNP.cur_output < UNP.usize) {
|
|
|
|
if (getbits(&UNP, 1)) {
|
|
|
|
uint32_t bb, bs, addme = 0;
|
|
|
|
bb = getbits(&UNP, 15);
|
|
|
|
|
|
|
|
if ((bs = getbits(&UNP, 2)) == 3) {
|
|
|
|
addme = 3;
|
|
|
|
if ((bs = getbits(&UNP, 3)) == 7) {
|
|
|
|
addme = 10;
|
|
|
|
if ((bs = getbits(&UNP, 5)) == 31) {
|
|
|
|
addme = 41;
|
|
|
|
if ((bs = getbits(&UNP, 8)) == 255) {
|
|
|
|
addme = 296;
|
|
|
|
while ((bs = getbits(&UNP, 8)) == 255) {
|
|
|
|
addme += 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bs += 3 + addme;
|
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
/* If getbits set UNP.error, bail out here, since otherwise
|
|
|
|
* the data we'd write out would be garbage */
|
|
|
|
if (UNP.error) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!CLI_ISCONTAINED(UNP.outputbuf, UNP.usize, &UNP.outputbuf[UNP.cur_output], bs) ||
|
|
|
|
!CLI_ISCONTAINED(UNP.outputbuf, UNP.usize, &UNP.outputbuf[UNP.cur_output - bb], bs)) {
|
|
|
|
UNP.error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (bs--) {
|
|
|
|
UNP.outputbuf[UNP.cur_output] = UNP.outputbuf[UNP.cur_output - bb];
|
|
|
|
UNP.cur_output++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
UNP.outputbuf[UNP.cur_output] = (uint8_t)getbits(&UNP, 8);
|
|
|
|
UNP.cur_output++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(UNP.inputbuf);
|
2022-08-09 16:56:54 -07:00
|
|
|
UNP.inputbuf = NULL;
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
/* Sometimes the autoit exe is in turn packed/lamed with a runtime compressor and similar shit.
|
2020-03-19 21:23:54 -04:00
|
|
|
* However, since the autoit script doesn't compress a second time very well, chances are we're
|
|
|
|
* still able to match the headers and unpack something (see sample 0811129)
|
|
|
|
* I'd rather unpack something (although possibly highly corrupted) than nothing at all
|
|
|
|
*
|
|
|
|
* - Fortuna audaces iuvat -
|
|
|
|
*/
|
2018-12-03 12:40:13 -05:00
|
|
|
if (UNP.error) {
|
|
|
|
cli_dbgmsg("autoit: decompression error after %u bytes - partial file may exist\n", UNP.cur_output);
|
|
|
|
UNP.usize = UNP.cur_output;
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-09 16:56:54 -07:00
|
|
|
/*
|
|
|
|
* File is NOT compressed.
|
|
|
|
*/
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: file is not compressed\n");
|
|
|
|
UNP.outputbuf = UNP.inputbuf;
|
2023-04-17 11:39:18 -07:00
|
|
|
UNP.inputbuf = NULL;
|
|
|
|
|
|
|
|
UNP.usize = UNP.csize;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (UNP.usize < 4) {
|
|
|
|
cli_dbgmsg("autoit: file is too short\n");
|
|
|
|
free(UNP.outputbuf);
|
2023-04-17 11:39:18 -07:00
|
|
|
UNP.outputbuf = NULL;
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
files++;
|
|
|
|
|
|
|
|
/* FIXME: REGRESSION NEEDED! */
|
|
|
|
/* UNP.usize = u2a(UNP.outputbuf, UNP.usize); */
|
|
|
|
|
|
|
|
snprintf(tempfile, 1023, "%s" PATHSEP "autoit.%.3u", tmpd, files);
|
|
|
|
tempfile[1023] = '\0';
|
2022-08-09 16:56:54 -07:00
|
|
|
|
|
|
|
tempfd = open(tempfile, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
|
|
|
|
if (tempfd < 0) {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: Can't create file %s\n", tempfile);
|
2022-08-09 16:56:54 -07:00
|
|
|
status = CL_ECREAT;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
|
|
|
if (cli_writen(tempfd, UNP.outputbuf, UNP.usize) != UNP.usize) {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: cannot write %d bytes\n", UNP.usize);
|
2022-08-09 16:56:54 -07:00
|
|
|
status = CL_EWRITE;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
free(UNP.outputbuf);
|
2022-08-09 16:56:54 -07:00
|
|
|
UNP.outputbuf = NULL;
|
|
|
|
|
|
|
|
if (ctx->engine->keeptmp) {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: file extracted to %s\n", tempfile);
|
2022-08-09 16:56:54 -07:00
|
|
|
} else {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: file successfully extracted\n");
|
2022-08-09 16:56:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lseek(tempfd, 0, SEEK_SET) == -1) {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: call to lseek() has failed\n");
|
2022-08-09 16:56:54 -07:00
|
|
|
status = CL_ESEEK;
|
|
|
|
goto done;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
|
|
|
ret = cli_magic_scan_desc(tempfd, tempfile, ctx, NULL, LAYER_ATTRIBUTES_NONE);
|
|
|
|
if (CL_SUCCESS != ret) {
|
|
|
|
status = ret;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(tempfd);
|
|
|
|
tempfd = -1;
|
|
|
|
if (!ctx->engine->keeptmp) {
|
|
|
|
(void)cli_unlink(tempfile);
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (NULL != UNP.inputbuf) {
|
|
|
|
free(UNP.inputbuf);
|
|
|
|
}
|
|
|
|
if (NULL != UNP.outputbuf) {
|
|
|
|
free(UNP.outputbuf);
|
|
|
|
}
|
|
|
|
if (tempfd >= 0) {
|
|
|
|
close(tempfd);
|
|
|
|
if (!ctx->engine->keeptmp) {
|
|
|
|
(void)cli_unlink(tempfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************
|
2024-01-19 09:08:36 -08:00
|
|
|
LAME related stuff
|
2007-10-30 18:53:25 +00:00
|
|
|
*********************/
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
#define ROFL(a, b) ((a << (b % (sizeof(a) << 3))) | (a >> ((sizeof(a) << 3) - (b % (sizeof(a) << 3)))))
|
2007-10-30 18:53:25 +00:00
|
|
|
|
|
|
|
struct LAME {
|
2018-12-03 12:40:13 -05:00
|
|
|
uint32_t c0;
|
|
|
|
uint32_t c1;
|
|
|
|
uint32_t grp1[17];
|
2007-10-30 18:53:25 +00:00
|
|
|
};
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
static double LAME_fpusht(struct LAME *l)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
double as_double;
|
|
|
|
struct {
|
|
|
|
uint32_t lo;
|
|
|
|
uint32_t hi;
|
|
|
|
} as_uint;
|
|
|
|
} ret;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
uint32_t rolled = ROFL(l->grp1[l->c0], 9) + ROFL(l->grp1[l->c1], 13);
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
l->grp1[l->c0] = rolled;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!l->c0--) l->c0 = 16;
|
|
|
|
if (!l->c1--) l->c1 = 16;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
/* if (l->grp1[l->c0] == l->grp2[0]) { */
|
|
|
|
/* if (!memcmp(l->grp1, (uint32_t *)l + 0x24 - l->c0, 0x44)) */
|
|
|
|
/* return 0.0; */
|
|
|
|
/* } */
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (fpu_words == FPU_ENDIAN_LITTLE) {
|
|
|
|
ret.as_uint.lo = rolled << 0x14;
|
|
|
|
ret.as_uint.hi = 0x3ff00000 | (rolled >> 0xc);
|
|
|
|
} else {
|
|
|
|
ret.as_uint.hi = rolled << 0x14;
|
|
|
|
ret.as_uint.lo = 0x3ff00000 | (rolled >> 0xc);
|
|
|
|
}
|
|
|
|
return ret.as_double - 1.0;
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
static void LAME_srand(struct LAME *l, uint32_t seed)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
for (i = 0; i < 17; i++) {
|
|
|
|
seed *= 0x53A9B4FB; /*1403630843*/
|
|
|
|
seed = 1 - seed;
|
|
|
|
l->grp1[i] = seed;
|
|
|
|
}
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
l->c0 = 0;
|
|
|
|
l->c1 = 10;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
for (i = 0; i < 9; i++)
|
|
|
|
LAME_fpusht(l);
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
static uint8_t LAME_getnext(struct LAME *l)
|
|
|
|
{
|
|
|
|
double x;
|
|
|
|
uint8_t ret;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
LAME_fpusht(l);
|
|
|
|
x = LAME_fpusht(l) * 256.0;
|
|
|
|
if ((int32_t)x < 256)
|
|
|
|
ret = (uint8_t)x;
|
|
|
|
else
|
|
|
|
ret = 0xff;
|
|
|
|
return ret;
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
static void LAME_decrypt(uint8_t *cypher, uint32_t size, uint16_t seed)
|
|
|
|
{
|
|
|
|
struct LAME lame;
|
|
|
|
/* mt_srand_timewrap(struct srand_struc bufDC); */
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
LAME_srand(&lame, (uint32_t)seed);
|
|
|
|
while (size--)
|
|
|
|
*cypher++ ^= LAME_getnext(&lame);
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************
|
2019-05-04 15:54:54 -04:00
|
|
|
autoit3 EA06 handler
|
2007-10-30 18:53:25 +00:00
|
|
|
*********************/
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
static int ea06(cli_ctx *ctx, const uint8_t *base, char *tmpd)
|
|
|
|
{
|
2022-08-09 16:56:54 -07:00
|
|
|
cl_error_t ret;
|
|
|
|
uint8_t b[600], comp, *buf;
|
2018-12-03 12:40:13 -05:00
|
|
|
uint32_t s;
|
2022-08-09 16:56:54 -07:00
|
|
|
int i;
|
2018-12-03 12:40:13 -05:00
|
|
|
unsigned int files = 0;
|
|
|
|
char tempfile[1024];
|
2021-03-31 18:04:13 -07:00
|
|
|
const char prefixes[] = {'\0', '\0', '@', '$', '\0', '.', '"', '\0'};
|
2019-06-16 06:58:53 -04:00
|
|
|
const char *opers[] = {",", "=", ">", "<", "<>", ">=", "<=", "(", ")", "+", "-", "/", "*", "&", "[", "]", "==", "^", "+=", "-=", "/=", "*=", "&=", "?", ":"};
|
2018-12-03 12:40:13 -05:00
|
|
|
struct UNP UNP;
|
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
|
|
|
|
|
|
|
/* Useless due to a bug in CRC calculation - LMAO!!1 */
|
|
|
|
/* if (cli_readn(desc, buf, 24)!=24) */
|
|
|
|
/* return CL_CLEAN; */
|
|
|
|
/* LAME_decrypt(buf, 0x10, 0x99f2); */
|
|
|
|
/* buf+=0x10; */
|
|
|
|
base += 16; /* for now we just skip the garbage */
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
while (CL_SUCCESS == (ret = cli_checklimits("cli_autoit", ctx, 0, 0, 0))) {
|
|
|
|
bool script = false;
|
|
|
|
|
|
|
|
if (!fmap_need_ptr_once(map, base, 8)) {
|
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
/* LAME_decrypt(buf, 4, 0x18ee); waste of time */
|
|
|
|
if (cli_readint32(base) != 0x52ca436b) {
|
2020-02-17 10:14:01 -08:00
|
|
|
cli_dbgmsg("autoit: no FILE magic found, giving up (got 0x%08x)\n", cli_readint32(base));
|
2022-08-09 16:56:54 -07:00
|
|
|
return CL_SUCCESS;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
s = cli_readint32(base + 4) ^ 0xadbc;
|
2022-08-09 16:56:54 -07:00
|
|
|
if ((int32_t)(s * 2) < 0) {
|
|
|
|
return CL_SUCCESS; /* the original code wouldn't seek back here */
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
base += 8;
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (s < sizeof(b) / 2) {
|
2022-08-09 16:56:54 -07:00
|
|
|
if (!fmap_need_ptr_once(map, base, s * 2)) {
|
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
memcpy(b, base, s * 2);
|
|
|
|
LAME_decrypt(b, s * 2, s + 0xb33f);
|
|
|
|
u2a(b, s * 2);
|
|
|
|
cli_dbgmsg("autoit: magic string '%s'\n", b);
|
2022-08-09 16:56:54 -07:00
|
|
|
|
|
|
|
if (s == 19 && !memcmp(">>>AUTOIT SCRIPT<<<", b, 19)) {
|
|
|
|
script = true;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
} else {
|
|
|
|
cli_dbgmsg("autoit: magic string too long to print\n");
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
base += s * 2;
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
if (!fmap_need_ptr_once(map, base, 4)) {
|
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
s = cli_readint32(base) ^ 0xf820;
|
2022-08-09 16:56:54 -07:00
|
|
|
if ((int32_t)(s * 2) < 0) {
|
|
|
|
return CL_SUCCESS; /* the original code wouldn't seek back here */
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
base += 4;
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (cli_debug_flag && s < sizeof(b) / 2) {
|
2022-08-09 16:56:54 -07:00
|
|
|
if (!fmap_need_ptr_once(map, base, s * 2)) {
|
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
memcpy(b, base, s * 2);
|
|
|
|
LAME_decrypt(b, s * 2, s + 0xf479);
|
|
|
|
b[s * 2] = '\0';
|
|
|
|
b[s * 2 + 1] = '\0';
|
|
|
|
u2a(b, s * 2);
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: original filename '%s'\n", b);
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
base += s * 2;
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
if (!fmap_need_ptr_once(map, base, 13)) {
|
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
comp = *base;
|
|
|
|
UNP.csize = cli_readint32(base + 1) ^ 0x87bc;
|
|
|
|
if ((int32_t)UNP.csize < 0) {
|
|
|
|
cli_dbgmsg("autoit: bad file size - giving up\n");
|
2022-08-09 16:56:54 -07:00
|
|
|
return CL_SUCCESS;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!UNP.csize) {
|
|
|
|
cli_dbgmsg("autoit: skipping empty file\n");
|
|
|
|
base += 13 + 16;
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: compressed size: %x\n", UNP.csize);
|
|
|
|
cli_dbgmsg("autoit: advertised uncompressed size %x\n", cli_readint32(base + 5) ^ 0x87bc);
|
|
|
|
cli_dbgmsg("autoit: ref chksum: %x\n", cli_readint32(base + 9) ^ 0xa685);
|
|
|
|
|
|
|
|
base += 13 + 16;
|
|
|
|
|
|
|
|
if (cli_checklimits("autoit", ctx, UNP.csize, 0, 0) != CL_CLEAN) {
|
|
|
|
base += UNP.csize;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comp == 1 && UNP.csize < sizeof(union unaligned_32)) {
|
|
|
|
cli_dbgmsg("autoit: compressed size too small, skipping\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
files++;
|
2020-02-17 10:14:01 -08:00
|
|
|
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(UNP.inputbuf = cli_max_malloc(UNP.csize))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
return CL_EMEM;
|
2022-08-09 16:56:54 -07:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!fmap_need_ptr_once(map, base, UNP.csize)) {
|
|
|
|
cli_dbgmsg("autoit: failed to read compressed stream. broken/truncated file?\n");
|
|
|
|
free(UNP.inputbuf);
|
2022-08-09 16:56:54 -07:00
|
|
|
return CL_SUCCESS;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
memcpy(UNP.inputbuf, base, UNP.csize);
|
|
|
|
base += UNP.csize;
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
LAME_decrypt(UNP.inputbuf, UNP.csize, 0x2477 /* + m4sum (broken by design) */);
|
|
|
|
|
|
|
|
if (comp == 1) {
|
|
|
|
cli_dbgmsg("autoit: file is compressed\n");
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (cli_readint32(UNP.inputbuf) != 0x36304145) {
|
|
|
|
cli_dbgmsg("autoit: bad magic or unsupported version\n");
|
|
|
|
free(UNP.inputbuf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
if (!(UNP.usize = be32_to_host(*(uint32_t *)(UNP.inputbuf + 4)))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.usize = UNP.csize; /* only a specifically crafted or badly corrupted sample should land here */
|
2022-08-09 16:56:54 -07:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (cli_checklimits("autoit", ctx, UNP.usize, 0, 0) != CL_CLEAN) {
|
|
|
|
free(UNP.inputbuf);
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(UNP.outputbuf = cli_max_malloc(UNP.usize))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
free(UNP.inputbuf);
|
|
|
|
return CL_EMEM;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: uncompressed size again: %x\n", UNP.usize);
|
|
|
|
|
|
|
|
UNP.cur_output = 0;
|
|
|
|
UNP.cur_input = 8;
|
|
|
|
UNP.bitmap.full = 0;
|
|
|
|
UNP.bits_avail = 0;
|
|
|
|
UNP.error = 0;
|
|
|
|
|
|
|
|
while (!UNP.error && UNP.cur_output < UNP.usize) {
|
|
|
|
if (!getbits(&UNP, 1)) {
|
|
|
|
uint32_t bb, bs, addme = 0;
|
|
|
|
bb = getbits(&UNP, 15);
|
|
|
|
|
|
|
|
if ((bs = getbits(&UNP, 2)) == 3) {
|
|
|
|
addme = 3;
|
|
|
|
if ((bs = getbits(&UNP, 3)) == 7) {
|
|
|
|
addme = 10;
|
|
|
|
if ((bs = getbits(&UNP, 5)) == 31) {
|
|
|
|
addme = 41;
|
|
|
|
if ((bs = getbits(&UNP, 8)) == 255) {
|
|
|
|
addme = 296;
|
|
|
|
while ((bs = getbits(&UNP, 8)) == 255) {
|
|
|
|
addme += 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bs += 3 + addme;
|
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
/* If getbits set UNP.error, bail out here, since otherwise
|
|
|
|
* the data we'd write out would be garbage */
|
|
|
|
if (UNP.error) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-16 00:13:55 +01:00
|
|
|
// cli_dbgmsg("cur_output: %u, bs: %u, bb: %u\n", UNP.cur_output, bs, bb);
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!CLI_ISCONTAINED(UNP.outputbuf, UNP.usize, &UNP.outputbuf[UNP.cur_output], bs) ||
|
|
|
|
!CLI_ISCONTAINED(UNP.outputbuf, UNP.usize, &UNP.outputbuf[UNP.cur_output - bb], bs)) {
|
|
|
|
UNP.error = 1;
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
while (bs--) {
|
|
|
|
UNP.outputbuf[UNP.cur_output] = UNP.outputbuf[UNP.cur_output - bb];
|
|
|
|
UNP.cur_output++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
UNP.outputbuf[UNP.cur_output] = (uint8_t)getbits(&UNP, 8);
|
|
|
|
UNP.cur_output++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(UNP.inputbuf);
|
|
|
|
if (UNP.error) {
|
|
|
|
cli_dbgmsg("autoit: decompression error after %u bytes - partial file may exist\n", UNP.cur_output);
|
|
|
|
UNP.usize = UNP.cur_output;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cli_dbgmsg("autoit: file is not compressed\n");
|
|
|
|
UNP.outputbuf = UNP.inputbuf;
|
|
|
|
UNP.usize = UNP.csize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UNP.usize < 4) {
|
|
|
|
cli_dbgmsg("autoit: file is too short\n");
|
|
|
|
free(UNP.outputbuf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (script) {
|
2020-02-17 10:14:01 -08:00
|
|
|
/* From here on, we'll reuse csize to be the size of the
|
|
|
|
* output buffer */
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.csize = UNP.usize;
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(buf = cli_max_malloc(UNP.csize))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
free(UNP.outputbuf);
|
|
|
|
return CL_EMEM;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.cur_output = 0;
|
|
|
|
UNP.cur_input = 4;
|
|
|
|
UNP.bits_avail = cli_readint32((char *)UNP.outputbuf);
|
|
|
|
UNP.error = 0;
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: script has got %u lines\n", UNP.bits_avail);
|
|
|
|
|
|
|
|
while (!UNP.error && UNP.bits_avail && UNP.cur_input < UNP.usize) {
|
|
|
|
uint8_t op;
|
|
|
|
|
|
|
|
switch ((op = UNP.outputbuf[UNP.cur_input++])) {
|
2020-02-17 10:14:01 -08:00
|
|
|
case 0: /* keyword ID */ {
|
|
|
|
uint32_t keyword_id;
|
|
|
|
uint32_t keyword_len;
|
|
|
|
if (UNP.cur_input >= UNP.usize - 4) {
|
|
|
|
UNP.error = 1;
|
|
|
|
cli_dbgmsg("autoit: too few bytes present - expected enough for a keyword ID\n");
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2020-03-21 14:15:28 -04:00
|
|
|
keyword_id = cli_readint32((char *)&UNP.outputbuf[UNP.cur_input]);
|
2020-02-17 10:14:01 -08:00
|
|
|
if (keyword_id >= (sizeof(autoit_keywords) / sizeof(autoit_keywords[0]))) {
|
|
|
|
UNP.error = 1;
|
|
|
|
cli_dbgmsg("autoit: unknown AutoIT keyword ID: 0x%x\n", keyword_id);
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
UNP.cur_input += 4;
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
keyword_len = strlen(autoit_keywords[keyword_id]);
|
|
|
|
if (UNP.cur_output + keyword_len + 2 >= UNP.csize) {
|
|
|
|
uint8_t *newout;
|
|
|
|
UNP.csize += 512;
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(newout = cli_max_realloc(buf, UNP.csize))) {
|
2020-02-17 10:14:01 -08:00
|
|
|
UNP.error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = newout;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
if (cli_debug_flag) {
|
|
|
|
if (0 == memcmp(autoit_keywords[keyword_id], "UNKNOWN", MIN(strlen("UNKNOWN"), keyword_len))) {
|
|
|
|
cli_dbgmsg("autoit: encountered use of unknown keyword ID: %s\n", autoit_keywords[keyword_id]);
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
snprintf((char *)&buf[UNP.cur_output], keyword_len + 2, "%s ", autoit_keywords[keyword_id]);
|
|
|
|
UNP.cur_output += keyword_len + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: /* function ID */ {
|
|
|
|
uint32_t function_id;
|
|
|
|
uint32_t function_len;
|
|
|
|
if (UNP.cur_input >= UNP.usize - 4) {
|
|
|
|
UNP.error = 1;
|
|
|
|
cli_dbgmsg("autoit: too few bytes present - expected enough for a function ID\n");
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2020-03-21 14:15:28 -04:00
|
|
|
function_id = cli_readint32((char *)&UNP.outputbuf[UNP.cur_input]);
|
2020-02-17 10:14:01 -08:00
|
|
|
if (function_id >= (sizeof(autoit_functions) / sizeof(autoit_functions[0]))) {
|
|
|
|
UNP.error = 1;
|
|
|
|
cli_dbgmsg("autoit: unknown AutoIT function ID: 0x%x\n", function_id);
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
UNP.cur_input += 4;
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
function_len = strlen(autoit_functions[function_id]);
|
|
|
|
if (UNP.cur_output + function_len + 2 >= UNP.csize) {
|
|
|
|
uint8_t *newout;
|
|
|
|
UNP.csize += 512;
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(newout = cli_max_realloc(buf, UNP.csize))) {
|
2020-02-17 10:14:01 -08:00
|
|
|
UNP.error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = newout;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
if (cli_debug_flag) {
|
|
|
|
if (0 == memcmp(autoit_functions[function_id], "UNKNOWN", MIN(strlen("UNKNOWN"), function_len))) {
|
|
|
|
cli_dbgmsg("autoit: encountered use of unknown function ID: %s\n", autoit_functions[function_id]);
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2020-02-17 10:14:01 -08:00
|
|
|
snprintf((char *)&buf[UNP.cur_output], function_len + 2, "%s ", autoit_functions[function_id]);
|
|
|
|
UNP.cur_output += function_len + 1;
|
|
|
|
break;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
case 5: /* <INT> */
|
|
|
|
if (UNP.cur_input >= UNP.usize - 4) {
|
|
|
|
UNP.error = 1;
|
|
|
|
cli_dbgmsg("autoit: not enough space for an int\n");
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (UNP.cur_output + 12 >= UNP.csize) {
|
|
|
|
uint8_t *newout;
|
|
|
|
UNP.csize += 512;
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(newout = cli_max_realloc(buf, UNP.csize))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = newout;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
snprintf((char *)&buf[UNP.cur_output], 12, "0x%08x ", cli_readint32((char *)&UNP.outputbuf[UNP.cur_input]));
|
|
|
|
UNP.cur_output += 11;
|
|
|
|
UNP.cur_input += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x10: /* <INT64> */
|
|
|
|
{
|
|
|
|
uint64_t val;
|
|
|
|
if (UNP.usize < 8 || UNP.cur_input >= UNP.usize - 8) {
|
|
|
|
UNP.error = 1;
|
|
|
|
cli_dbgmsg("autoit: not enough space for an int64\n");
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (UNP.cur_output + 20 >= UNP.csize) {
|
|
|
|
uint8_t *newout;
|
|
|
|
UNP.csize += 512;
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(newout = cli_max_realloc(buf, UNP.csize))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = newout;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
val = (uint64_t)cli_readint32((char *)&UNP.outputbuf[UNP.cur_input + 4]);
|
|
|
|
val <<= 32;
|
|
|
|
val += (uint64_t)cli_readint32((char *)&UNP.outputbuf[UNP.cur_input]);
|
|
|
|
snprintf((char *)&buf[UNP.cur_output], 20, "0x%016lx ", (unsigned long int)val);
|
|
|
|
UNP.cur_output += 19;
|
|
|
|
UNP.cur_input += 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x20: /* <DOUBLE> */
|
|
|
|
if (UNP.usize < 8 || UNP.cur_input >= UNP.usize - 8) {
|
|
|
|
UNP.error = 1;
|
|
|
|
cli_dbgmsg("autoit: not enough space for a double\n");
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (UNP.cur_output + 40 >= UNP.csize) {
|
|
|
|
uint8_t *newout;
|
|
|
|
UNP.csize += 512;
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(newout = cli_max_realloc(buf, UNP.csize))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = newout;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
|
|
|
if (fpu_words == FPU_ENDIAN_LITTLE) {
|
2018-12-03 12:40:13 -05:00
|
|
|
snprintf((char *)&buf[UNP.cur_output], 39, "%g ", *(double *)&UNP.outputbuf[UNP.cur_input]);
|
2022-08-09 16:56:54 -07:00
|
|
|
} else
|
2018-12-03 12:40:13 -05:00
|
|
|
do {
|
|
|
|
double x;
|
|
|
|
uint8_t *j = (uint8_t *)&x;
|
|
|
|
unsigned int i;
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
for (i = 0; i < 8; i++) {
|
2018-12-03 12:40:13 -05:00
|
|
|
j[7 - i] = UNP.outputbuf[UNP.cur_input + i];
|
2022-08-09 16:56:54 -07:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
snprintf((char *)&buf[UNP.cur_output], 39, "%g ", x); /* FIXME: check */
|
|
|
|
} while (0);
|
|
|
|
buf[UNP.cur_output + 38] = ' ';
|
|
|
|
buf[UNP.cur_output + 39] = '\0';
|
|
|
|
UNP.cur_output += strlen((char *)&buf[UNP.cur_output]);
|
|
|
|
UNP.cur_input += 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x30: /* COSTRUCT */
|
|
|
|
case 0x31: /* COMMAND */
|
|
|
|
case 0x32: /* MACRO */
|
|
|
|
case 0x33: /* VAR */
|
|
|
|
case 0x34: /* FUNC */
|
|
|
|
case 0x35: /* OBJECT */
|
|
|
|
case 0x36: /* STRING */
|
|
|
|
case 0x37: /* DIRECTIVE */
|
|
|
|
{
|
|
|
|
uint32_t chars, dchars, i;
|
|
|
|
|
|
|
|
if (UNP.cur_input >= UNP.usize - 4) {
|
|
|
|
UNP.error = 1;
|
|
|
|
cli_dbgmsg("autoit: not enough space for size\n");
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
chars = cli_readint32((char *)&UNP.outputbuf[UNP.cur_input]);
|
|
|
|
dchars = chars * 2;
|
|
|
|
UNP.cur_input += 4;
|
|
|
|
|
|
|
|
if (UNP.usize < dchars || UNP.cur_input >= UNP.usize - dchars) {
|
|
|
|
UNP.error = 1;
|
|
|
|
cli_dbgmsg("autoit: size too big - needed %d, total %d, avail %d\n", dchars, UNP.usize, UNP.usize - UNP.cur_input);
|
|
|
|
break;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (UNP.cur_output + chars + 3 >= UNP.csize) {
|
|
|
|
uint8_t *newout;
|
|
|
|
UNP.csize += chars + 512;
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(newout = cli_max_realloc(buf, UNP.csize))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = newout;
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
if (prefixes[op - 0x30]) {
|
2018-12-03 12:40:13 -05:00
|
|
|
buf[UNP.cur_output++] = prefixes[op - 0x30];
|
2022-08-09 16:56:54 -07:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
if (chars) {
|
|
|
|
for (i = 0; i < dchars; i += 2) {
|
|
|
|
UNP.outputbuf[UNP.cur_input + i] ^= (uint8_t)chars;
|
|
|
|
UNP.outputbuf[UNP.cur_input + i + 1] ^= (uint8_t)(chars >> 8);
|
|
|
|
}
|
|
|
|
u2a(&UNP.outputbuf[UNP.cur_input], dchars);
|
|
|
|
memcpy(&buf[UNP.cur_output], &UNP.outputbuf[UNP.cur_input], chars);
|
|
|
|
UNP.cur_output += chars;
|
|
|
|
UNP.cur_input += dchars;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
|
|
|
if (op == 0x36) {
|
2021-03-31 18:04:13 -07:00
|
|
|
// TODO: Mask possible double quotes inside the string: >Say:"Hi "< ==> >"Say:""Hi"" "<
|
2018-12-03 12:40:13 -05:00
|
|
|
buf[UNP.cur_output++] = '"';
|
2022-08-09 16:56:54 -07:00
|
|
|
}
|
|
|
|
if (op != 0x34) {
|
2018-12-03 12:40:13 -05:00
|
|
|
buf[UNP.cur_output++] = ' ';
|
2022-08-09 16:56:54 -07:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case 0x40: /* , */
|
|
|
|
case 0x41: /* = */
|
|
|
|
case 0x42: /* > */
|
|
|
|
case 0x43: /* < */
|
|
|
|
case 0x44: /* <> */
|
|
|
|
case 0x45: /* >= */
|
|
|
|
case 0x46: /* <= */
|
|
|
|
case 0x47: /* ( */
|
|
|
|
case 0x48: /* ) */
|
|
|
|
case 0x49: /* + */
|
|
|
|
case 0x4a: /* - */
|
|
|
|
case 0x4b: /* / */
|
|
|
|
case 0x4c: /* * */
|
|
|
|
case 0x4d: /* & */
|
|
|
|
case 0x4e: /* [ */
|
|
|
|
case 0x4f: /* ] */
|
|
|
|
case 0x50: /* == */
|
|
|
|
case 0x51: /* ^ */
|
|
|
|
case 0x52: /* += */
|
|
|
|
case 0x53: /* -= */
|
|
|
|
case 0x54: /* /= */
|
|
|
|
case 0x55: /* *= */
|
|
|
|
case 0x56: /* &= */
|
2019-06-16 06:58:53 -04:00
|
|
|
case 0x57: /* ? */
|
|
|
|
case 0x58: /* : */
|
2018-12-03 12:40:13 -05:00
|
|
|
if (UNP.cur_output + 4 >= UNP.csize) {
|
|
|
|
uint8_t *newout;
|
|
|
|
UNP.csize += 512;
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(newout = cli_max_realloc(buf, UNP.csize))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = newout;
|
|
|
|
}
|
2021-03-31 18:04:13 -07:00
|
|
|
|
2022-02-16 00:13:55 +01:00
|
|
|
// TODO: Fix Autoit plus bug
|
|
|
|
// if (op == 0x49) /* + */ and next op ==0x05 /*int32*/ and that int32 is negative...
|
|
|
|
// skip next line (and don't add "+")
|
|
|
|
// Background: "$a= (-4)" gets incorrect compiled. Decompiled it will be get "$A= (+ -4)"
|
|
|
|
// That doesn't effects the interpreter however when recompiling decompiled output that will result in a syntax error
|
2021-03-31 18:04:13 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.cur_output += snprintf((char *)&buf[UNP.cur_output], 4, "%s ", opers[op - 0x40]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x7f:
|
|
|
|
UNP.bits_avail--;
|
|
|
|
if (UNP.cur_output + 1 >= UNP.csize) {
|
|
|
|
uint8_t *newout;
|
|
|
|
UNP.csize += 512;
|
2022-05-09 14:28:34 -07:00
|
|
|
if (!(newout = cli_max_realloc(buf, UNP.csize))) {
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf = newout;
|
|
|
|
}
|
|
|
|
buf[UNP.cur_output++] = '\n';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-06-16 06:58:53 -04:00
|
|
|
cli_dbgmsg("autoit: found unknown op (0x%x)\n", op);
|
2018-12-03 12:40:13 -05:00
|
|
|
UNP.error = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
if (UNP.error) {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: decompilation aborted - partial script may exist\n");
|
2022-08-09 16:56:54 -07:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
|
|
|
|
free(UNP.outputbuf);
|
|
|
|
} else {
|
|
|
|
buf = UNP.outputbuf;
|
|
|
|
UNP.cur_output = UNP.usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(tempfile, 1023, "%s" PATHSEP "autoit.%.3u", tmpd, files);
|
|
|
|
tempfile[1023] = '\0';
|
2020-03-30 20:42:44 -04:00
|
|
|
if ((i = open(tempfile, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR)) < 0) {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: Can't create file %s\n", tempfile);
|
|
|
|
free(buf);
|
|
|
|
return CL_ECREAT;
|
|
|
|
}
|
2019-05-04 15:54:54 -04:00
|
|
|
if (cli_writen(i, buf, UNP.cur_output) != UNP.cur_output) {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: cannot write %d bytes\n", UNP.usize);
|
|
|
|
close(i);
|
|
|
|
free(buf);
|
|
|
|
return CL_EWRITE;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
free(buf);
|
2022-08-09 16:56:54 -07:00
|
|
|
|
|
|
|
if (ctx->engine->keeptmp) {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: %s extracted to %s\n", (script) ? "script" : "file", tempfile);
|
2022-08-09 16:56:54 -07:00
|
|
|
} else {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_dbgmsg("autoit: %s successfully extracted\n", (script) ? "script" : "file");
|
2022-08-09 16:56:54 -07:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (lseek(i, 0, SEEK_SET) == -1) {
|
|
|
|
cli_dbgmsg("autoit: call to lseek() has failed\n");
|
|
|
|
close(i);
|
|
|
|
return CL_ESEEK;
|
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
|
|
|
ret = cli_magic_scan_desc(i, tempfile, ctx, NULL, LAYER_ATTRIBUTES_NONE);
|
|
|
|
if (CL_SUCCESS != ret) {
|
|
|
|
close(i);
|
|
|
|
if (!ctx->engine->keeptmp) {
|
|
|
|
if (cli_unlink(tempfile)) {
|
|
|
|
return CL_EUNLINK;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
return CL_VIRUS;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
|
2013-02-28 19:32:29 -05:00
|
|
|
close(i);
|
2022-08-09 16:56:54 -07:00
|
|
|
|
|
|
|
if (!ctx->engine->keeptmp) {
|
|
|
|
if (cli_unlink(tempfile)) {
|
|
|
|
return CL_EUNLINK;
|
|
|
|
}
|
|
|
|
}
|
2013-02-28 19:32:29 -05:00
|
|
|
}
|
2022-08-09 16:56:54 -07:00
|
|
|
return ret;
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************
|
2019-05-04 15:54:54 -04:00
|
|
|
autoit3 wrapper
|
2007-10-30 18:53:25 +00:00
|
|
|
*********************/
|
|
|
|
|
2022-08-09 16:56:54 -07:00
|
|
|
cl_error_t cli_scanautoit(cli_ctx *ctx, off_t offset)
|
2018-12-03 12:40:13 -05:00
|
|
|
{
|
2022-08-09 16:56:54 -07:00
|
|
|
cl_error_t status = CL_SUCCESS;
|
2018-12-03 12:40:13 -05:00
|
|
|
const uint8_t *version;
|
|
|
|
char *tmpd;
|
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 scanautoit()\n");
|
|
|
|
|
|
|
|
if (!(version = fmap_need_off_once(map, offset, sizeof(*version))))
|
|
|
|
return CL_EREAD;
|
2007-10-30 18:53:25 +00:00
|
|
|
|
Improve tmp sub-directory names
At present many parsers create tmp subdirectories to store extracted
files. For parsers like the vba parser, this is required as the
directory is later scanned. For other parsers, these subdirectories are
probably not helpful now that we provide recursive sub-dirs when
--leave-temps is enabled. It's not quite as simple as removing the extra
subdirectories, however. Certain parsers, like autoit, don't create very
unique filenames and would result in file name collisions when
--leave-temps is not enabled.
The best thing to do would be to make sure each parser uses unique
filenames and doesn't rely on cli_magic_scan_dir() to scan extracted
content before removing the extra subdirectory. In the meantime, this
commit gives the extra subdirectories meaningful names to improve
readability.
This commit also:
- Provides the 'bmp' prefix for extracted PE icons.
- Removes empty tmp subdirs when extracting rtf files, to eliminate
clutter.
- The PDF parser sometimes creates tmp files when decompressing streams
before it knows if there is actually any content to decompress. This
resulted in a large number of empty files. While it would be best to
avoid creating empty files in the first place, that's not quite as
as it sounds. This commit does the next best thing and deletes the
tmp files if nothing was actually extracted, even if --leave-temps is
enabled.
- Removes the "scantemp" prefix for unnamed fmaps scanned with
cli_magic_scan(). The 5-character hashes given to tmp files with
prefixes resulted in occasional file name collisions when extracting
certain file types with thousands of embedded files.
- The VBA and TAR parsers mistakenly used NAME_MAX instead of PATH_MAX,
resulting in truncated file paths and failed extraction when
--leave-temps is enabled and a lot of recursion is in play. This commit
switches them from NAME_MAX to PATH_MAX.
2020-03-27 16:06:22 -04:00
|
|
|
if (!(tmpd = cli_gentemp_with_prefix(ctx->sub_tmpdir, "autoit-tmp")))
|
2018-12-03 12:40:13 -05:00
|
|
|
return CL_ETMPDIR;
|
|
|
|
if (mkdir(tmpd, 0700)) {
|
|
|
|
cli_dbgmsg("autoit: Can't create temporary directory %s\n", tmpd);
|
|
|
|
free(tmpd);
|
|
|
|
return CL_ETMPDIR;
|
|
|
|
}
|
|
|
|
if (ctx->engine->keeptmp)
|
|
|
|
cli_dbgmsg("autoit: Extracting files to %s\n", tmpd);
|
|
|
|
|
|
|
|
switch (*version) {
|
|
|
|
case 0x35:
|
2022-08-09 16:56:54 -07:00
|
|
|
status = ea05(ctx, version + 1, tmpd);
|
2018-12-03 12:40:13 -05:00
|
|
|
break;
|
|
|
|
case 0x36:
|
|
|
|
if (fpu_words == FPU_ENDIAN_INITME)
|
|
|
|
fpu_words = get_fpu_endian();
|
|
|
|
if (fpu_words == FPU_ENDIAN_UNKNOWN) {
|
|
|
|
cli_dbgmsg("autoit: EA06 support not available"
|
|
|
|
"(cannot extract ea06 doubles, unknown floating double representation).\n");
|
2022-08-09 16:56:54 -07:00
|
|
|
status = CL_CLEAN;
|
2018-12-03 12:40:13 -05:00
|
|
|
} else
|
2022-08-09 16:56:54 -07:00
|
|
|
status = ea06(ctx, version + 1, tmpd);
|
2018-12-03 12:40:13 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* NOT REACHED */
|
|
|
|
cli_dbgmsg("autoit: unknown method\n");
|
2022-08-09 16:56:54 -07:00
|
|
|
status = CL_CLEAN;
|
2018-12-03 12:40:13 -05:00
|
|
|
}
|
2007-10-30 18:53:25 +00:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (!ctx->engine->keeptmp)
|
|
|
|
cli_rmdirs(tmpd);
|
2010-07-29 02:41:15 +02:00
|
|
|
|
2007-10-30 18:53:25 +00:00
|
|
|
free(tmpd);
|
2022-08-09 16:56:54 -07:00
|
|
|
return status;
|
2007-10-30 18:53:25 +00:00
|
|
|
}
|