This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
iSpy.logwriter.xm
238 lines (203 loc) · 7.55 KB
/
iSpy.logwriter.xm
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
/*
iSpy - iOS hooking framework.
Async logging framework.
Logs are written to <app>/Documents/.ispy/logs/<facility>.log
*/
#include <stack>
#include <fcntl.h>
#include <stdio.h>
#include <dlfcn.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <objc/objc.h>
#include <ifaddrs.h>
#include <time.h>
#include <arpa/inet.h>
#include <mach-o/dyld.h>
#include <netinet/in.h>
#include <dispatch/dispatch.h>
#import <Security/Security.h>
#import <Security/SecCertificate.h>
#include <CFNetwork/CFNetwork.h>
#include <CFNetwork/CFProxySupport.h>
#include <CoreFoundation/CFString.h>
#include <CoreFoundation/CFStream.h>
#import <Foundation/NSJSONSerialization.h>
#include "iSpy.common.h"
#include "hooks_C_system_calls.h"
static const unsigned int DEBUG = 0;
static const unsigned int INFO = 1;
static const unsigned int WARNING = 2;
static const unsigned int ERROR = 3;
static const unsigned int WTF = 4;
static const char* LEVELS[] = {"[DEBUG]", "[INFO]", "[WARNING]", "[ERROR]", "[WTF]"};
/*
>>> These come from iSpy.common.h
static const unsigned int LOG_STRACE = 0;
static const unsigned int LOG_MSGSEND = 1;
static const unsigned int LOG_GENERAL = 2;
static const unsigned int LOG_HTTP = 3;
static const unsigned int LOG_TCPIP = 4;
*/
static const char* FACILITY_FILES[] = {"strace.log", "msgsend.log", "general.log", "http.log", "tcpip.log", "global.log", "report.log"};
static const int LOG_UMASK = 0644;
static int logFiles[MAX_LOG + 1];
static BOOL logIsInitialized = NO;
static unsigned int minLogLevel = DEBUG;
static dispatch_queue_t logQueue;
static const char *LOG_QUEUE = "com.bishopfox.iSpy.logger";
// If we use any hooked calls within the log writer, we must use the original (unhooked) versions.
// Simply declare them extern and copy from hook_C_system_calls.xm
// MAKE ABSOLUTELY SURE that orig_funcname = funcname in hook_C_system_calls.xm
// See the following functions in hook_C_system_calls.xm for examples.
extern int (*orig_gettimeofday)(struct timeval *tp, void *tzp);
extern size_t (*orig_write)(int fd, const void *cbuf, user_size_t nbyte);
/*
* This function is dispatched to GCD for execution
*/
static void ispy_log_write(unsigned int facility, unsigned int level, char *msg) {
if (MAX_LOG < facility) {
facility = LOG_GENERAL;
}
char *line, *p;
unsigned int lineLength;
struct timeval tv;
orig_gettimeofday(&tv, NULL); // we use the original syscall so that we don't get a race when hooking this syscall
time_t ticks = tv.tv_sec;
/* Don't forget the extra chars for formatting! */
lineLength = strlen(LEVELS[level]) + strlen(msg) + strlen(ctime(&ticks)) + 5;
line = (char *) malloc(lineLength);
/* The closing ] is added to the ctime string below */
snprintf(line, lineLength, "%s [%s %s\n", LEVELS[level], ctime(&ticks), msg);
// this is so dumb that we need to do this. Stupid ctime() puts a newline at the end of its string :-(
p = line;
while(*p && *p != '\n')
p++;
if(*p == '\n')
*p = ']';
/* Make sure to use the un-hooked write() */
if(facility == LOG_MSGSEND) {
flock(logFiles[LOG_MSGSEND], LOCK_EX);
orig_write(logFiles[facility], line, lineLength - 1);
flock(logFiles[LOG_MSGSEND], LOCK_UN);
} else {
orig_write(logFiles[facility], line, lineLength - 1);
orig_write(logFiles[LOG_GLOBAL], line, lineLength - 1);
}
free(line);
// ewww. I made this function static because of this call to free().
// Nobody should be allowed to call ispy_log_write() except functions below.
free(msg);
}
/*
* We can use Objc here because we havn't hooked everything yet
*/
EXPORT void ispy_init_logwriter(NSString *documents) {
if (!logIsInitialized) {
NSError *error = nil;
/* Check to see if our ispy directory exists, and create it if not */
NSString *iSpyDirectory = [documents stringByAppendingPathComponent:@"/.ispy/"];
if (![[NSFileManager defaultManager] fileExistsAtPath:iSpyDirectory]) {
[[NSFileManager defaultManager] createDirectoryAtPath:iSpyDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error];
}
if (error != nil) {
NSLog(@"[iSpy][ERROR] %@", error);
}
/* Check to see if the <App>/Documents/.ispy/logs/ directory exists, and create it if not */
NSString *logsDirectory = [iSpyDirectory stringByAppendingPathComponent:@"/logs/"];
if (![[NSFileManager defaultManager] fileExistsAtPath:logsDirectory]) {
[[NSFileManager defaultManager] createDirectoryAtPath:logsDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error];
}
if (error != nil) {
NSLog(@"[iSpy][ERROR] %@", error);
}
/* Next we create each log file, and store the FD in the static fileLogs array */
for(unsigned int index = 0; index <= MAX_LOG; ++index) {
NSString *fileName = [NSString stringWithFormat:@"%s", FACILITY_FILES[index]];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", logsDirectory, fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
logFiles[index] = open([filePath UTF8String], O_WRONLY | O_APPEND);
} else {
logFiles[index] = open([filePath UTF8String], O_WRONLY | O_CREAT, LOG_UMASK);
}
}
/* Initialize GCD serial queue */
logQueue = dispatch_queue_create(LOG_QUEUE, NULL);
logIsInitialized = YES;
}
}
/* Non-blocking logging calls */
EXPORT void ispy_log_debug(unsigned int facility, const char *msg, ...) {
if (minLogLevel <= DEBUG) {
char *msgBuffer;
va_list args;
va_start(args, msg);
vasprintf(&msgBuffer, msg, args);
va_end(args);
dispatch_async(logQueue, ^{
ispy_log_write(facility, DEBUG, msgBuffer);
});
}
}
EXPORT void ispy_log_info(unsigned int facility, const char *msg, ...) {
if (minLogLevel <= INFO) {
char *msgBuffer;
va_list args;
va_start(args, msg);
vasprintf(&msgBuffer, msg, args);
va_end(args);
dispatch_async(logQueue, ^{
ispy_log_write(facility, INFO, msgBuffer);
});
}
}
EXPORT void ispy_log_warning(unsigned int facility, const char *msg, ...) {
if (minLogLevel <= WARNING) {
char *msgBuffer;
va_list args;
va_start(args, msg);
vasprintf(&msgBuffer, msg, args);
va_end(args);
dispatch_async(logQueue, ^{
ispy_log_write(facility, WARNING, msgBuffer);
});
}
}
EXPORT void ispy_log_error(unsigned int facility, const char *msg, ...) {
if (minLogLevel <= ERROR) {
char *msgBuffer;
va_list args;
va_start(args, msg);
vasprintf(&msgBuffer, msg, args);
va_end(args);
dispatch_async(logQueue, ^{
ispy_log_write(facility, ERROR, msgBuffer);
});
}
}
/* We always log "what a terrible failure" */
EXPORT void ispy_log_wtf(unsigned int facility, const char *msg, ...) {
char *msgBuffer;
va_list args;
va_start(args, msg);
vasprintf(&msgBuffer, msg, args);
va_end(args);
dispatch_async(logQueue, ^{
ispy_log_write(facility, WTF, msgBuffer);
});
}