Rizin
unix-like reverse engineering framework and cli tools
bin_xtr_pemixed.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2018-2019 JohnPeng47 <johnpeng47@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_util.h>
6 #include <rz_lib.h>
7 #include <rz_bin.h>
8 #include "pe/pemixed.h"
9 
10 static RzList *oneshotall(RzBin *bin, const ut8 *buf, ut64 size);
11 static RzBinXtrData *oneshot(RzBin *bin, const ut8 *buf, ut64 size, int subbin_type);
12 
13 static void free_xtr(void *xtr_obj) {
14  rz_bin_pemixed_free((struct rz_bin_pemixed_obj_t *)xtr_obj);
15 }
16 
17 static void destroy(RzBin *bin) {
18  free_xtr(bin->cur->xtr_obj);
19 }
20 
21 static bool check_buffer(RzBuffer *b) {
22  return false;
23 #if 0
24  if (!bytes) {
25  return false;
26  }
27  if (sz <= 0x3d) { //less than size of MS-DOS header which is 64bytes
28  return false;
29  }
30  ut32 idx = (bytes[0x3c] | (bytes[0x3d]<<8));
31  if (sz > idx + 0x18 + 2) {
32  /* Here PE signature for usual PE files
33  * and PL signature for Phar Lap TNT DOS extender 32bit executables
34  */
35  if (!memcmp (bytes, "MZ", 2)) {
36  if (!memcmp (bytes+idx, "PE", 2) &&
37  !memcmp (bytes+idx+0x18, "\x0b\x01", 2)) {
38  return true;
39  }
40  // TODO: Add one more indicator, to prevent false positives
41  if (!memcmp (bytes+idx, "PL", 2)) {
42  return true;
43  }
44  }
45  }
46  return false;
47 #endif
48 }
49 
50 // TODO RzBufferify
51 static RzList *oneshotall(RzBin *bin, const ut8 *buf, ut64 size) {
52  // extract dos componenent first
54 
55  if (!data) {
56  return NULL;
57  }
58  // XXX - how do we validate a valid narch?
60  rz_list_append(res, data);
61 
62  if ((data = oneshot(bin, buf, size, SUB_BIN_NATIVE))) {
63  rz_list_append(res, data);
64  }
65 
66  if ((data = oneshot(bin, buf, size, SUB_BIN_NET))) {
67  rz_list_append(res, data);
68  }
69 
70  return res;
71 }
72 
73 // implement this later
74 static void fill_metadata_info_from_hdr(RzBinXtrMetadata *meta, void *foo) { // struct Pe_32_rz_bin_pemixed_obj_t* pe_bin){
75  meta->arch = NULL;
76  meta->bits = 0;
77  meta->machine = NULL;
78  meta->type = NULL;
79  meta->libname = NULL;
80  meta->xtr_type = "net";
81  // strcpy (meta->xtr_type, "net");
82 }
83 
84 // XXX: ut8* should be RzBuffer *
85 static RzBinXtrData *oneshot(RzBin *bin, const ut8 *buf, ut64 size, int sub_bin_type) {
86  rz_return_val_if_fail(bin && bin->cur && buf, false);
87 
88  if (!bin->cur->xtr_obj) {
89  bin->cur->xtr_obj = rz_bin_pemixed_from_bytes_new(buf, size);
90  }
91 
92  struct rz_bin_pemixed_obj_t *fb = bin->cur->xtr_obj;
93  // this function is prolly not nessescary
94  struct PE_(rz_bin_pe_obj_t) *pe = rz_bin_pemixed_extract(fb, sub_bin_type);
95  if (!pe) {
96  return NULL;
97  }
99  if (!metadata) {
100  return NULL;
101  }
102  fill_metadata_info_from_hdr(metadata, pe);
103  return rz_bin_xtrdata_new(pe->b, 0, pe->size, 3, metadata);
104 }
105 
107  .name = "xtr.pemixed",
108  .desc = "Extract sub-binaries in PE files",
109  .load = NULL, // not yet implemented
110  .extract = NULL, // not yet implemented
111  .extractall = NULL, // not yet implemented
112  .destroy = &destroy,
113  .extract_from_bytes = &oneshot,
114  .extractall_from_bytes = &oneshotall,
115  .free_xtr = &free_xtr,
116  .check_buffer = &check_buffer,
117 };
118 
119 #ifndef RZ_PLUGIN_INCORE
122  .data = &rz_bin_xtr_plugin_pemixed,
123  .version = RZ_VERSION
124 };
125 #endif
static ut8 bytes[32]
Definition: asm_arc.c:23
RZ_API void rz_bin_xtrdata_free(void *data_)
Definition: bin.c:61
RZ_API RzBinXtrData * rz_bin_xtrdata_new(RzBuffer *buf, ut64 offset, ut64 size, ut32 file_count, RzBinXtrMetadata *metadata)
Definition: bin.c:47
static void fill_metadata_info_from_hdr(RzBinXtrMetadata *meta, void *foo)
static RzList * oneshotall(RzBin *bin, const ut8 *buf, ut64 size)
static void free_xtr(void *xtr_obj)
static void destroy(RzBin *bin)
static RzBinXtrData * oneshot(RzBin *bin, const ut8 *buf, ut64 size, int subbin_type)
RzBinXtrPlugin rz_bin_xtr_plugin_xtr_pemixed
RZ_API RzLibStruct rizin_plugin
static bool check_buffer(RzBuffer *b)
#define RZ_API
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
static RzMain foo[]
Definition: main.c:11
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
int idx
Definition: setup.py:197
struct rz_bin_pemixed_obj_t * rz_bin_pemixed_from_bytes_new(const ut8 *buf, ut64 size)
Definition: pemixed.c:159
RzBinPEObj * rz_bin_pemixed_extract(struct rz_bin_pemixed_obj_t *bin, int sub_bin)
Definition: pemixed.c:115
void * rz_bin_pemixed_free(struct rz_bin_pemixed_obj_t *bin)
Definition: pemixed.c:137
#define SUB_BIN_NATIVE
Definition: pemixed.h:8
#define SUB_BIN_DOS
Definition: pemixed.h:7
#define SUB_BIN_NET
Definition: pemixed.h:9
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
@ RZ_LIB_TYPE_BIN_XTR
Definition: rz_lib.h:76
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_VERSION
Definition: rz_version.h:8
#define b(i)
Definition: sha256.c:42
Definition: malloc.c:26
const char * xtr_type
Definition: rz_bin.h:374
ut64(WINAPI *w32_GetEnabledXStateFeatures)()