1*839529caSEd Maste /*-
2*839529caSEd Maste * Copyright (c) 2015 Kai Wang
3*839529caSEd Maste * All rights reserved.
4*839529caSEd Maste *
5*839529caSEd Maste * Redistribution and use in source and binary forms, with or without
6*839529caSEd Maste * modification, are permitted provided that the following conditions
7*839529caSEd Maste * are met:
8*839529caSEd Maste * 1. Redistributions of source code must retain the above copyright
9*839529caSEd Maste * notice, this list of conditions and the following disclaimer.
10*839529caSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
11*839529caSEd Maste * notice, this list of conditions and the following disclaimer in the
12*839529caSEd Maste * documentation and/or other materials provided with the distribution.
13*839529caSEd Maste *
14*839529caSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*839529caSEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*839529caSEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*839529caSEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*839529caSEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*839529caSEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*839529caSEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*839529caSEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*839529caSEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*839529caSEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*839529caSEd Maste * SUCH DAMAGE.
25*839529caSEd Maste */
26*839529caSEd Maste
27*839529caSEd Maste #include <assert.h>
28*839529caSEd Maste #include <errno.h>
29*839529caSEd Maste #include <stdlib.h>
30*839529caSEd Maste #include <string.h>
31*839529caSEd Maste
32*839529caSEd Maste #include "_libpe.h"
33*839529caSEd Maste
34*839529caSEd Maste ELFTC_VCSID("$Id: libpe_rich.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
35*839529caSEd Maste
36*839529caSEd Maste static char *
memfind(char * s,const char * find,size_t slen,size_t flen)37*839529caSEd Maste memfind(char *s, const char *find, size_t slen, size_t flen)
38*839529caSEd Maste {
39*839529caSEd Maste int i;
40*839529caSEd Maste
41*839529caSEd Maste if (slen == 0 || flen == 0 || flen > slen)
42*839529caSEd Maste return (NULL);
43*839529caSEd Maste
44*839529caSEd Maste for (i = 0; (size_t) i <= slen - flen; i++) {
45*839529caSEd Maste if (s[i] != find[0])
46*839529caSEd Maste continue;
47*839529caSEd Maste if (flen == 1)
48*839529caSEd Maste return (&s[i]);
49*839529caSEd Maste if (memcmp(&s[i + 1], &find[1], flen - 1) == 0)
50*839529caSEd Maste return (&s[i]);
51*839529caSEd Maste }
52*839529caSEd Maste
53*839529caSEd Maste return (NULL);
54*839529caSEd Maste }
55*839529caSEd Maste
56*839529caSEd Maste int
libpe_parse_rich_header(PE * pe)57*839529caSEd Maste libpe_parse_rich_header(PE *pe)
58*839529caSEd Maste {
59*839529caSEd Maste PE_RichHdr *rh;
60*839529caSEd Maste char *p, *r, *s;
61*839529caSEd Maste uint32_t x;
62*839529caSEd Maste int found, i;
63*839529caSEd Maste
64*839529caSEd Maste assert(pe->pe_stub != NULL && pe->pe_stub_ex > 0);
65*839529caSEd Maste
66*839529caSEd Maste /* Search for the "Rich" keyword to locate the Rich header. */
67*839529caSEd Maste s = pe->pe_stub + sizeof(PE_DosHdr);
68*839529caSEd Maste r = memfind(s, PE_RICH_TEXT, pe->pe_stub_ex, 4);
69*839529caSEd Maste if (r == NULL || r + 8 > s + pe->pe_stub_ex) {
70*839529caSEd Maste errno = ENOENT;
71*839529caSEd Maste return (-1);
72*839529caSEd Maste }
73*839529caSEd Maste
74*839529caSEd Maste if ((rh = calloc(1, sizeof(*rh))) == NULL) {
75*839529caSEd Maste errno = ENOMEM;
76*839529caSEd Maste return (-1);
77*839529caSEd Maste }
78*839529caSEd Maste
79*839529caSEd Maste rh->rh_xor = le32dec(r + 4); /* Retrieve the "XOR mask" */
80*839529caSEd Maste
81*839529caSEd Maste /*
82*839529caSEd Maste * Search for the hidden keyword "DanS" by XOR the dwords before
83*839529caSEd Maste * the "Rich" keyword with the XOR mask.
84*839529caSEd Maste */
85*839529caSEd Maste found = 0;
86*839529caSEd Maste for (p = r - 4; p >= s; p -= 4) {
87*839529caSEd Maste x = le32dec(p) ^ rh->rh_xor;
88*839529caSEd Maste if (x == PE_RICH_HIDDEN) {
89*839529caSEd Maste found = 1;
90*839529caSEd Maste break;
91*839529caSEd Maste }
92*839529caSEd Maste }
93*839529caSEd Maste if (!found) {
94*839529caSEd Maste free(rh);
95*839529caSEd Maste errno = ENOENT;
96*839529caSEd Maste return (-1);
97*839529caSEd Maste }
98*839529caSEd Maste
99*839529caSEd Maste /*
100*839529caSEd Maste * Found the "DanS" keyword, which is the start of the Rich header.
101*839529caSEd Maste * The next step is to skip the first 16 bytes (DanS, XOR mask,
102*839529caSEd Maste * XOR mask, XOR mask) and read the (compid,cnt) tuples.
103*839529caSEd Maste */
104*839529caSEd Maste pe->pe_rh_start = p;
105*839529caSEd Maste p += 16;
106*839529caSEd Maste rh->rh_total = (r - p) / 8;
107*839529caSEd Maste if ((rh->rh_compid = malloc(rh->rh_total * sizeof(*rh->rh_compid))) ==
108*839529caSEd Maste NULL) {
109*839529caSEd Maste free(rh);
110*839529caSEd Maste errno = ENOMEM;
111*839529caSEd Maste return (-1);
112*839529caSEd Maste }
113*839529caSEd Maste if ((rh->rh_cnt = malloc(rh->rh_total * sizeof(*rh->rh_cnt))) ==
114*839529caSEd Maste NULL) {
115*839529caSEd Maste free(rh->rh_compid);
116*839529caSEd Maste free(rh);
117*839529caSEd Maste errno = ENOMEM;
118*839529caSEd Maste return (-1);
119*839529caSEd Maste }
120*839529caSEd Maste for (i = 0; (uint32_t) i < rh->rh_total; i++, p += 8) {
121*839529caSEd Maste rh->rh_compid[i] = le32dec(p) ^ rh->rh_xor;
122*839529caSEd Maste rh->rh_cnt[i] = le32dec(p + 4) ^ rh->rh_xor;
123*839529caSEd Maste }
124*839529caSEd Maste
125*839529caSEd Maste pe->pe_rh = rh;
126*839529caSEd Maste
127*839529caSEd Maste return (0);
128*839529caSEd Maste }
129