-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdatabase.cpp
264 lines (240 loc) · 7.92 KB
/
database.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* ______________________.___
* \_ _____/\_ _____/| |
* | __)_ | __) | |
* | \ | \ | |
* /_______ / \___ / |___|
* \/ \/
* _________ .__ ____ __. .__ _____
* / _____/_ _ _|__| ______ _____ | |/ _| ____ |__|/ ____\____
* \_____ \\ \/ \/ / |/ ___// ___/ | < / \| \ __\/ __ \
* / \\ /| |\___ \ \___ \ | | \| | \ || | \ ___/
* /_______ / \/\_/ |__/____ >____ > |____|__ \___| /__||__| \___ >
* \/ \/ \/ \/ \/ \/
*
* EFI Swiss Knife
* An IDA plugin to improve (U)EFI reversing
*
* Copyright (C) 2016, 2017 Pedro Vilaça (fG!) - reverser@put.as - https://reverse.put.as
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* database.c
*
*/
#include "database.h"
#include <ida.hpp>
#include <idp.hpp>
#include <loader.hpp>
#include <bytes.hpp>
#include <kernwin.hpp>
#include <search.hpp>
#include <allins.hpp>
#include <segment.hpp>
#include <limits.h>
#include <auto.hpp>
#include <name.hpp>
#include <frame.hpp>
#include <struct.hpp>
#include <funcs.hpp>
#include <sqlite3.h>
#include <libgen.h>
#include "config.h"
#include "logging.h"
sqlite3 *g_db_connection;
static int set_db_options(void);
static int init_db(void);
int
open_db(void)
{
/* if db already exists just open it and set options */
if ( access(DB_FILE, F_OK) != -1 )
{
if (sqlite3_open(DB_FILE, &g_db_connection) != SQLITE_OK)
{
ERROR_MSG("Unable to open database! (line %d)", __LINE__);
sqlite3_close(g_db_connection);
g_db_connection = NULL;
return 1;
}
set_db_options();
}
/* else we need to create and init it */
else
{
if (sqlite3_open(DB_FILE, &g_db_connection) == SQLITE_OK)
{
set_db_options();
init_db();
}
else
{
ERROR_MSG("Unable to create database! (line %d)", __LINE__);
sqlite3_close(g_db_connection);
g_db_connection = NULL;
return 1;
}
}
return 0;
}
int
close_db(void)
{
if (g_db_connection == NULL)
{
ERROR_MSG("Database handle is invalid.");
return 1;
}
sqlite3_close(g_db_connection);
g_db_connection = NULL;
return 0;
}
/* we probably want options more safe than this to not risk any fuzzing data? */
static int
set_db_options(void)
{
if (g_db_connection == NULL)
{
ERROR_MSG("Database handle is invalid.");
return 1;
}
char *err_msg = NULL;
if (sqlite3_exec(g_db_connection, "PRAGMA journal_mode = MEMORY", NULL, NULL, &err_msg) != SQLITE_OK)
{
ERROR_MSG("Unable to set pragma: %s", err_msg);
sqlite3_free(err_msg);
return 1;
}
sqlite3_free(err_msg);
if (sqlite3_exec(g_db_connection, "PRAGMA synchronous = ON", NULL, NULL, &err_msg) != SQLITE_OK)
{
ERROR_MSG("Unable to set pragma: %s", err_msg);
sqlite3_free(err_msg);
return 1;
}
sqlite3_free(err_msg);
return 0;
}
static int
init_db(void)
{
if (g_db_connection == NULL)
{
ERROR_MSG("Database handle is invalid.");
return 1;
}
char main_table_sql[] = "CREATE TABLE main ( \
file_guid TEXT NOT NULL, \
path TEXT NOT NULL, \
type INTEGER NOT NULL, \
error INTEGER NOT NULL)";
char *error_msg = NULL;
if (sqlite3_exec(g_db_connection, main_table_sql, NULL, NULL, &error_msg) != SQLITE_OK)
{
ERROR_MSG("Unable to create main table: %s.", error_msg);
return 1;
}
char boot_service_stats_table_sql[] = "CREATE TABLE boot_service_stats ( \
file_guid TEXT NOT NULL, \
raisetpl INTEGER NOT NULL, \
restoretpl INTEGER NOT NULL, \
allocatepages INTEGER NOT NULL, \
freepages INTEGER NOT NULL, \
getmemorymap INTEGER NOT NULL, \
allocatepool INTEGER NOT NULL, \
freepool INTEGER NOT NULL, \
createevent INTEGER NOT NULL, \
settimer INTEGER NOT NULL, \
waitforevent INTEGER NOT NULL, \
signalevent INTEGER NOT NULL, \
closeevent INTEGER NOT NULL, \
checkevent INTEGER NOT NULL, \
installprotocolinterface INTEGER NOT NULL, \
reinstallprotocolinterface INTEGER NOT NULL, \
uninstallprotocolinterface INTEGER NOT NULL, \
handleprotocol INTEGER NOT NULL, \
reserved INTEGER NOT NULL, \
registerprotocolnotify INTEGER NOT NULL, \
locatehandle INTEGER NOT NULL, \
locatedevicepath INTEGER NOT NULL, \
installconfigurationtable INTEGER NOT NULL, \
loadimage INTEGER NOT NULL, \
startimage INTEGER NOT NULL, \
exit INTEGER NOT NULL, \
unloadimage INTEGER NOT NULL, \
exitbootservices INTEGER NOT NULL, \
getnextmonotoniccount INTEGER NOT NULL, \
stall INTEGER NOT NULL, \
setwatchdogtimer INTEGER NOT NULL, \
connectcontroller INTEGER NOT NULL, \
disconnectcontroller INTEGER NOT NULL, \
openprotocol INTEGER NOT NULL, \
closeprotocol INTEGER NOT NULL, \
openprotocolinformation INTEGER NOT NULL, \
protocolsperhandle INTEGER NOT NULL, \
locatehandlebuffer INTEGER NOT NULL, \
locateprotocol INTEGER NOT NULL, \
installmultipleprotocolinterfaces INTEGER NOT NULL, \
uninstallmultipleprotocolinterfaces INTEGER NOT NULL, \
calculatecrc32 INTEGER NOT NULL, \
copymem INTEGER NOT NULL, \
setmem INTEGER NOT NULL, \
createeventex INTEGER NOT NULL)";
if (sqlite3_exec(g_db_connection, boot_service_stats_table_sql, NULL, NULL, &error_msg) != SQLITE_OK)
{
ERROR_MSG("Unable to create boot services stats table: %s.", error_msg);
return 1;
}
char runtime_service_stats_table_sql[] = "CREATE TABLE runtime_service_stats ( \
file_guid TEXT NOT NULL, \
gettime INTEGER NOT NULL, \
settime INTEGER NOT NULL, \
getwakeuptime INTEGER NOT NULL, \
setwakeuptime INTEGER NOT NULL, \
setvirtualaddressmap INTEGER NOT NULL, \
convertpointer INTEGER NOT NULL, \
getvariable INTEGER NOT NULL, \
getnextvariablename INTEGER NOT NULL, \
setvariable INTEGER NOT NULL, \
getnexthighmonotoniccount INTEGER NOT NULL, \
resetsystem INTEGER NOT NULL, \
updatecapsule INTEGER NOT NULL, \
querycapsulecapabilities INTEGER NOT NULL, \
queryvariableinfo INTEGER NOT NULL)";
if (sqlite3_exec(g_db_connection, runtime_service_stats_table_sql, NULL, NULL, &error_msg) != SQLITE_OK)
{
ERROR_MSG("Unable to create runtime services stats table: %s.", error_msg);
return 1;
}
char protocols_usage_table_sql[] = "CREATE TABLE protocols_usage ( \
file_guid TEXT NOT NULL, \
protocol TEXT NOT NULL , \
description TEXT NOT NULL, \
type INTEGER NOT NULL)";
if (sqlite3_exec(g_db_connection, protocols_usage_table_sql, NULL, NULL, &error_msg) != SQLITE_OK)
{
ERROR_MSG("Unable to create protocols usage table: %s.", error_msg);
return 1;
}
char installed_protocols_table_sql[] = "CREATE TABLE installed_protocols ( \
file_guid TEXT NOT NULL, \
installed TEXT NOT NULL, \
type INTEGER NOT NULL)";
if (sqlite3_exec(g_db_connection, installed_protocols_table_sql, NULL, NULL, &error_msg) != SQLITE_OK)
{
ERROR_MSG("Unable to create installed protocols table: %s.", error_msg);
return 1;
}
return 0;
}