forked from MaidsShadowClub/triton_krackme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routines.cpp
151 lines (122 loc) · 4.13 KB
/
routines.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
//! \file
/*
** This program is under the terms of the Apache License 2.0.
** Jonathan Salwan
*/
#include <cstdarg>
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <regex>
#include <triton/archEnums.hpp>
#include <triton/architecture.hpp>
#include <triton/context.hpp>
#include <triton/cpuSize.hpp>
#include <triton/memoryAccess.hpp>
#include <vector>
#include <cstdio>
#include "routines.hpp"
#include "ttexplore.hpp"
#include "utils.hpp"
extern bool DEBUG;
/*
* This file aims to provide an example about using routines when emulating a
* target. For example, we provide a very simple printf routine that just prints
* the string format pointed by rdi. As example, this printf routine is used in
* the harness5.
*
* The idea behind routines is that you can simulate whatever the program calls
* and update the triton context according to your goals.
*/
namespace triton {
namespace routines {
triton::callbacks::cb_state_e __libc_start_main(triton::Context *ctx) {
debug_printf("[i] Execute %s\n", __FUNCTION__);
auto main_loc = getArg(0);
std::vector<std::string> argv = {"./programm", "1", "2", "3", "4"};
uint64 argc = argv.size();
for (int i = 0; i < argc; i++) {
uint8 *arg = reinterpret_cast<uint8 *>(argv[i].data());
auto ptr = allocate(arg, argv[i].size() + 1); // +1 for c-string
setStack(i + 3, ptr);
}
// set argc, argv & env
setArg(0, argc);
setArg(1, getStack(3));
setArg(2, 0);
setGpr("ip", main_loc);
return triton::callbacks::CONTINUE;
}
// TODO: add format string parser
triton::callbacks::cb_state_e printf(triton::Context *ctx) {
debug_printf("[i] Execute %s\n", __FUNCTION__);
auto format = getArg(0);
uint64 len = lenString(format);
// va_list ap;
// va_start(ap, static_cast<uint64>(gctx.getConcreteMemoryValue(format)));
// printf(readAsciiString(format).data(), ap);
// va_end(ap);
std::printf("%s\n", readUtf8String(format, len).data());
return triton::callbacks::PLT_CONTINUE;
}
triton::callbacks::cb_state_e puts(triton::Context *ctx) {
debug_printf("[i] Execute %s\n", __FUNCTION__);
auto format = getArg(0);
uint64 len = lenString(format);
std::printf("%s\n", readUtf8String(format, len).data());
return triton::callbacks::PLT_CONTINUE;
}
triton::callbacks::cb_state_e fflush(triton::Context *ctx) {
debug_printf("[i] Execute %s\n", __FUNCTION__);
return triton::callbacks::PLT_CONTINUE;
}
triton::callbacks::cb_state_e getlogin(triton::Context *ctx) {
debug_printf("[i] Execute %s\n", __FUNCTION__);
unsigned char user[] = "Hacker1337";
setGpr("ret", allocate(user, sizeof(user) + 1)); // +1 for c-string
return triton::callbacks::PLT_CONTINUE;
}
triton::callbacks::cb_state_e usleep(triton::Context *ctx) {
debug_printf("[i] Execute %s\n", __FUNCTION__);
auto timeout = getArg(0);
debug_printf("Waiting %zd ms\n", timeout);
return triton::callbacks::PLT_CONTINUE;
callbacks::PLT_CONTINUE;
}
triton::callbacks::cb_state_e sleep(triton::Context *ctx) {
debug_printf("[i] Execute %s\n", __FUNCTION__);
auto timeout = getArg(0);
debug_printf("Waiting %zd s\n", timeout);
return triton::callbacks::PLT_CONTINUE;
}
triton::callbacks::cb_state_e putchar(triton::Context *ctx) {
debug_printf("[i] Execute %s\n", __FUNCTION__);
auto c = static_cast<uint8>(getArg(0));
std::printf("%c", c);
debug_puts("\n");
setGpr("ret", c);
return triton::callbacks::PLT_CONTINUE;
}
triton::callbacks::cb_state_e exit(triton::Context *ctx) {
debug_printf("[i] Execute %s\n", __FUNCTION__);
auto code = getArg(0);
triton_printf("Exit: %zd\n", code);
// std::exit(code);
return triton::callbacks::BREAK;
}
triton::callbacks::cb_state_e fgets(triton::Context *ctx) {
debug_printf("[i] Execute %s\n", __FUNCTION__);
auto buf = getArg(0);
auto len = getArg(1);
if (ctx->isMemorySymbolized(buf)) {
return triton::callbacks::PLT_CONTINUE;
}
triton_printf("[!] fgets: add ctx->symbolizeMemory(%#010zx, %#zx)", buf, len);
return triton::callbacks::PLT_CONTINUE;
}
triton::callbacks::cb_state_e stub(triton::Context *ctx) {
return triton::callbacks::CONTINUE;
}
}; // namespace routines
}; // namespace triton