aboutsummaryrefslogtreecommitdiff
path: root/subhook_x86.c
blob: 991a290005dbb5939b0dd32220300754fe30f1d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Copyright (c) 2012-2013 Zeex
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#if defined _MSC_VER
	#if defined SUBHOOK_X86
		typedef __int32 intptr_t;
	#elif defined SUBHOOK_X86_64
		typedef __int64 intptr_t;
	#endif
#else
	#include <stdint.h>
#endif

#include "subhook.h"
#include "subhook_private.h"

#define JMP_OPCODE 0xE9

static const unsigned char jmp_opcode = JMP_OPCODE;
static const unsigned char jmp_instr[] = { JMP_OPCODE, 0x0, 0x0, 0x0, 0x0 };

struct subhook_x86 {
	struct subhook _;
	unsigned char code[sizeof(jmp_instr)];
};

SUBHOOK_EXPORT subhook_t SUBHOOK_API subhook_new() {
	struct subhook_x86 *hook;

	if ((hook = calloc(1, sizeof(*hook))) == NULL)
		return NULL;

	return (subhook_t)hook;
}

SUBHOOK_EXPORT void SUBHOOK_API subhook_free(subhook_t hook) {
	free(hook);
}

SUBHOOK_EXPORT int SUBHOOK_API subhook_install(subhook_t hook) {
	void *src;
	void *dst;
	intptr_t offset;

	if (subhook_is_installed(hook))
		return -EINVAL;

	src = subhook_get_src(hook);
	dst = subhook_get_dst(hook);

	subhook_unprotect(src, sizeof(jmp_instr));
	memcpy(((struct subhook_x86 *)hook)->code, src, sizeof(jmp_instr));
	memcpy(src, &jmp_instr, sizeof(jmp_instr));

	offset = (intptr_t)dst - ((intptr_t)src + sizeof(jmp_instr));
	memcpy((void *)((intptr_t)src + sizeof(jmp_opcode)), &offset,
	       sizeof(jmp_instr) - sizeof(jmp_opcode));

	hook->installed = 1;
	return 0;
}

SUBHOOK_EXPORT int SUBHOOK_API subhook_remove(subhook_t hook) {
	if (!subhook_is_installed(hook))
		return -EINVAL;

	memcpy(subhook_get_src(hook), ((struct subhook_x86 *)hook)->code,
	       sizeof(jmp_instr));

	hook->installed = 0;
	return 0;
}

SUBHOOK_EXPORT void *SUBHOOK_API subhook_read_dst(void *src) {
	unsigned char opcode;
	intptr_t src_addr = (intptr_t)src;
	intptr_t dst_addr;
	intptr_t *target;

	memcpy(&opcode, src, sizeof(opcode));
	if (opcode != jmp_opcode)
		return NULL;

	memcpy(&target, (void *)(src_addr + sizeof(jmp_opcode)), sizeof(*target));
	dst_addr = *target + src_addr + sizeof(jmp_instr);

	return (void *)dst_addr;
}