Browse code

upx/lzma

git-svn: trunk@4036

aCaB authored on 2008/07/31 11:17:25
Showing 6 changed files
... ...
@@ -1,3 +1,7 @@
1
+Thu Jul 31 04:01:02 CEST 2008 (acab)
2
+------------------------------------
3
+  * libclamav/upx: add preliminar support for upx/lzma (disabled)
4
+
1 5
 Wed Jul 30 20:09:03 EEST 2008 (edwin)
2 6
 -------------------------------------
3 7
   * clamd/others.c: avoid calling non-async-signal-safe functions between fork()
... ...
@@ -128,3 +128,33 @@ int cli_LzmaDecode(CLI_LZMA **Lp, struct stream_state* state) {
128 128
 
129 129
   return res;
130 130
 }
131
+
132
+int cli_LzmaInitUPX(CLI_LZMA **Lp, uint32_t dictsz) {
133
+  CLI_LZMA *L = *Lp;
134
+
135
+  if(!L) {
136
+    *Lp = L = cli_calloc(sizeof(*L), 1);
137
+    if(!L) {
138
+      return LZMA_RESULT_DATA_ERROR;
139
+    }
140
+  }
141
+
142
+  L->state.Properties.pb = 2; /* FIXME: these  */
143
+  L->state.Properties.lp = 0; /* values may    */
144
+  L->state.Properties.lc = 3; /* not be static */
145
+
146
+  L->state.Properties.DictionarySize = dictsz;
147
+
148
+  if (!(L->state.Probs = (CProb *)cli_malloc(LzmaGetNumProbs(&L->state.Properties) * sizeof(CProb))))
149
+    return LZMA_RESULT_DATA_ERROR;
150
+
151
+  if (!(L->state.Dictionary = (unsigned char *)cli_malloc(L->state.Properties.DictionarySize))) {
152
+    free(L->state.Probs);
153
+    return LZMA_RESULT_DATA_ERROR;
154
+  }
155
+
156
+  L->initted = 1;
157
+
158
+  LzmaDecoderInit(&L->state);
159
+  return LZMA_RESULT_OK;
160
+}
... ...
@@ -37,6 +37,7 @@ struct stream_state {
37 37
 int cli_LzmaInit(CLI_LZMA **, uint64_t);
38 38
 void cli_LzmaShutdown(CLI_LZMA **);
39 39
 int cli_LzmaDecode(CLI_LZMA **, struct stream_state*);
40
+int cli_LzmaInitUPX(CLI_LZMA **, uint32_t);
40 41
 
41 42
 #define LZMA_STREAM_END 2
42 43
 #define LZMA_RESULT_OK 0
... ...
@@ -75,6 +75,8 @@
75 75
 #define UPX_NRV2B "\x11\xdb\x11\xc9\x01\xdb\x75\x07\x8b\x1e\x83\xee\xfc\x11\xdb\x11\xc9\x11\xc9\x75\x20\x41\x01\xdb"
76 76
 #define UPX_NRV2D "\x83\xf0\xff\x74\x78\xd1\xf8\x89\xc5\xeb\x0b\x01\xdb\x75\x07\x8b\x1e\x83\xee\xfc\x11\xdb\x11\xc9"
77 77
 #define UPX_NRV2E "\xeb\x52\x31\xc9\x83\xe8\x03\x72\x11\xc1\xe0\x08\x8a\x06\x46\x83\xf0\xff\x74\x75\xd1\xf8\x89\xc5"
78
+#define UPX_LZMA1 "\x56\x83\xc3\x04\x53\x50\xc7\x03\x03\x00\x02\x00\x90\x90\x90\x55\x57\x56\x53\x83"
79
+#define UPX_LZMA2 "\x56\x83\xc3\x04\x53\x50\xc7\x03\x03\x00\x02\x00\x90\x90\x90\x90\x90\x55\x57\x56"
78 80
 
79 81
 #define EC32(x) le32_to_host(x) /* Convert little endian to host */
80 82
 #define EC16(x) le16_to_host(x)
... ...
@@ -1737,6 +1739,12 @@ int cli_scanpe(int desc, cli_ctx *ctx)
1737 1737
 	    }
1738 1738
 	}
1739 1739
 
1740
+	if(0 && cli_memstr(UPX_LZMA2, 20, epbuff + 0x2f, 20)) {
1741
+	  uint32_t ndsize=cli_readint32(epbuff+0x21);
1742
+	  if(ndsize<=dsize)
1743
+	    upx_success = upx_inflatelzma(src, ssize, dest, &ndsize, exe_sections[i].rva, exe_sections[i + 1].rva, vep) >=0;
1744
+	}
1745
+
1740 1746
 	if(!upx_success) {
1741 1747
 	    cli_dbgmsg("UPX: All decompressors failed\n");
1742 1748
 	    free(src);
... ...
@@ -56,6 +56,7 @@
56 56
 #include "others.h"
57 57
 #include "upx.h"
58 58
 #include "str.h"
59
+#include "lzma_iface.h"
59 60
 
60 61
 #define PEALIGN(o,a) (((a))?(((o)/(a))*(a)):(o))
61 62
 #define PESALIGN(o,a) (((a))?(((o)/(a)+((o)%(a)!=0))*(a)):(o))
... ...
@@ -520,3 +521,24 @@ int upx_inflate2e(char *src, uint32_t ssize, char *dst, uint32_t *dsize, uint32_
520 520
 
521 521
   return pefromupx (src, ssize, dst, dsize, ep, upx0, upx1, magic, dcur);
522 522
 }
523
+
524
+int upx_inflatelzma(char *src, uint32_t ssize, char *dst, uint32_t *dsize, uint32_t upx0, uint32_t upx1, uint32_t ep) {
525
+  CLI_LZMA *lz = NULL;
526
+  struct stream_state s;
527
+  uint32_t magic[]={0xb16,0xb1e,0};
528
+
529
+  cli_LzmaInitUPX(&lz, *dsize);
530
+  s.avail_in = ssize;
531
+  s.avail_out = *dsize;
532
+  s.next_in = src+2;
533
+  s.next_out = dst;
534
+
535
+  if(cli_LzmaDecode(&lz, &s)==LZMA_RESULT_DATA_ERROR) {
536
+/*     __asm__ __volatile__("int3"); */
537
+    cli_LzmaShutdown(&lz);
538
+    return -1;
539
+  }
540
+  cli_LzmaShutdown(&lz);
541
+
542
+  return pefromupx (src, ssize, dst, dsize, ep, upx0, upx1, magic, *dsize);
543
+}
... ...
@@ -26,5 +26,6 @@
26 26
 int upx_inflate2b(char *, uint32_t, char *, uint32_t *, uint32_t, uint32_t, uint32_t);
27 27
 int upx_inflate2d(char *, uint32_t, char *, uint32_t *, uint32_t, uint32_t, uint32_t);
28 28
 int upx_inflate2e(char *, uint32_t, char *, uint32_t *, uint32_t, uint32_t, uint32_t);
29
+int upx_inflatelzma(char *, uint32_t, char *, uint32_t *, uint32_t, uint32_t, uint32_t);
29 30
 
30 31
 #endif