-
Notifications
You must be signed in to change notification settings - Fork 13
/
RemoteCheater.m
367 lines (311 loc) · 11.8 KB
/
RemoteCheater.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* The Cheat - The legendary universal game trainer for Mac OS X.
* http://www.brokenzipper.com/trac/wiki/TheCheat
*
* Copyright (c) 2003-2011, Charles McGarvey et al.
*
* Distributable under the terms and conditions of the 2-clause BSD
* license; see the file COPYING for the legal text of the license.
*/
#import "RemoteCheater.h"
@interface RemoteCheater ( PrivateAPI )
- (void)_handlePacket;
@end
@implementation RemoteCheater
- (void)dealloc
{
[self disconnect];
[super dealloc];
}
- (BOOL)connectToHostWithData:(NSData *)data
{
if ( _socket ) {
[self disconnect];
}
_socket = [[MySocket alloc] initWithDelegate:self];
if ( ![_socket connectToAddressWithData:data] ) {
return NO;
}
// start reading from the socket
[_socket readDataToLength:sizeof(TCPacketHeader) tag:0];
return YES;
}
- (NSString *)hostAddress
{
return [_socket remoteHost];
}
// #############################################################################
#pragma mark Cheater Override
// #############################################################################
- (void)connect
{
NSData *params = [NSArchiver archivedDataWithRootObject:[NSString stringWithFormat:@"%@ %@", ChazAppName(), ChazAppVersion()]];
TCPacketHeader header = { TC_NIFTY, [params length], "connect" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)disconnect
{
[_socket setDelegate:nil];
[_socket release];
_socket = nil;
//[_delegate cheaterDidDisconnect:self];
}
- (void)authenticateWithPassword:(NSString *)password
{
NSData *params = [NSArchiver archivedDataWithRootObject:password];
TCPacketHeader header = { TC_NIFTY, [params length], "authenticate" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)getProcessList
{
TCPacketHeader header = { TC_NIFTY, 0, "getproclist" };
ChazLog( @"SENT getproclist" );
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)setTarget:(Process *)target
{
NSData *params = [NSArchiver archivedDataWithRootObject:target];
TCPacketHeader header = { TC_NIFTY, [params length], "settarget" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)pauseTarget
{
TCPacketHeader header = { TC_NIFTY, 0, "pausetarget" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)resumeTarget
{
TCPacketHeader header = { TC_NIFTY, 0, "resumetarget" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)limitReturnedResults:(unsigned)limit
{
NSData *params = [NSArchiver archivedDataWithRootObject:[NSNumber numberWithUnsignedInt:limit]];
TCPacketHeader header = { TC_NIFTY, [params length], "limitresults" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)searchForVariable:(Variable *)data comparison:(TCSearchOperator)op
{
NSData *params = [NSArchiver archivedDataWithRootObject:[NSArray arrayWithObjects:data, [NSNumber numberWithUnsignedChar:op], nil]];
TCPacketHeader header = { TC_NIFTY, [params length], "search" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)searchLastValuesComparison:(TCSearchOperator)op
{
NSData *params = [NSArchiver archivedDataWithRootObject:[NSNumber numberWithUnsignedChar:op]];
TCPacketHeader header = { TC_NIFTY, [params length], "lastsearch" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)cancelSearch
{
TCPacketHeader header = { TC_NIFTY, 0, "cancelsearch" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)clearSearch
{
TCPacketHeader header = { TC_NIFTY, 0, "clearsearch" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)getMemoryDump
{
TCPacketHeader header = { TC_NIFTY, 0, "dump" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)cancelMemoryDump
{
TCPacketHeader header = { TC_NIFTY, 0, "canceldump" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)makeVariableChanges:(NSArray *)variables repeat:(BOOL)doRepeat interval:(NSTimeInterval)repeatInterval
{
NSData *params = [NSArchiver archivedDataWithRootObject:[NSArray arrayWithObjects:variables,
[NSNumber numberWithBool:doRepeat], [NSNumber numberWithDouble:repeatInterval], nil]];
TCPacketHeader header = { TC_NIFTY, [params length], "changevars" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)stopChangingVariables
{
TCPacketHeader header = { TC_NIFTY, 0, "stopchange" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)undo
{
TCPacketHeader header = { TC_NIFTY, 0, "undo" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)redo
{
TCPacketHeader header = { TC_NIFTY, 0, "redo" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
- (void)watchVariablesAtIndex:(unsigned)index count:(unsigned)count interval:(NSTimeInterval)checkInterval
{
NSData *params = [NSArchiver archivedDataWithRootObject:[NSArray arrayWithObjects:[NSNumber numberWithUnsignedInt:index],
[NSNumber numberWithUnsignedInt:count], [NSNumber numberWithDouble:checkInterval], nil]];
TCPacketHeader header = { TC_NIFTY, [params length], "watchvars" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
[_socket writeData:params tag:0];
}
- (void)stopWatchingVariables
{
TCPacketHeader header = { TC_NIFTY, 0, "stopwatching" };
[_socket writeBytes:&header length:sizeof(header) tag:0];
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark MySocketDelegate
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
- (void)socket:(MySocket *)mySocket didReadData:(NSData *)theData tag:(int)tag
{
if ( tag == 0 && [theData length] == sizeof(TCPacketHeader) ) {
// got a packet header
memcpy( &_header, [theData bytes], sizeof(TCPacketHeader) );
if ( _header.size > 0 ) {
// request the rest of the packet
[mySocket readDataToLength:_header.size tag:1];
}
else {
[self _handlePacket];
// start reading the next packet
[mySocket readDataToLength:sizeof(TCPacketHeader) tag:0];
}
}
else if ( tag == 1 && [theData length] == _header.size ) {
// got packet parameter data
_parameters = [theData retain];
[self _handlePacket];
// start reading the next packet
[mySocket readDataToLength:sizeof(TCPacketHeader) tag:0];
}
else {
ChazLog( @"RemoteCheater - read expected data, disconnecting..." );
[self disconnect];
}
}
- (void)socketDidDisconnect:(MySocket *)mySocket
{
[_socket release];
_socket = nil;
[_delegate cheaterDidDisconnect:self];
}
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#pragma mark PrivateAPI
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
- (void)_handlePacket
{
if ( strncmp( "connected", _header.name, sizeof(_header.name) ) == 0 ) {
ChazLog( @"RemoteCheater - connected" );
[_delegate cheaterDidConnect:self];
}
else if ( strncmp( "requireauth", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterRequiresAuthentication:self];
}
else if ( strncmp( "rejectedauth", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterRejectedPassword:self];
}
else if ( strncmp( "authenticated", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterAcceptedPassword:self];
}
else if ( strncmp( "proclist", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheater:self didFindProcesses:[NSUnarchiver unarchiveObjectWithData:_parameters]];
}
else if ( strncmp( "addprocess", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheater:self didAddProcess:[NSUnarchiver unarchiveObjectWithData:_parameters]];
}
else if ( strncmp( "removeprocess", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheater:self didRemoveProcess:[NSUnarchiver unarchiveObjectWithData:_parameters]];
}
else if ( strncmp( "didsettarget", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheater:self didSetTarget:[NSUnarchiver unarchiveObjectWithData:_parameters]];
}
else if ( strncmp( "didpausetarget", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterPausedTarget:self];
}
else if ( strncmp( "didresumetarget", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterResumedTarget:self];
}
else if ( strncmp( "vars", _header.name, sizeof(_header.name) ) == 0 ) {
struct varInfo {
unsigned count;
unsigned varCount;
unsigned varSize;
} *varInfo;
void *bytes;
TCArray variables;
varInfo = (struct varInfo *)[_parameters bytes];
bytes = (void *)[_parameters bytes] + sizeof(*varInfo);
variables = TCMakeArrayWithBytes( varInfo->varCount, varInfo->varSize, bytes );
[_delegate cheater:self didFindVariables:variables actualAmount:varInfo->count];
}
else if ( strncmp( "values", _header.name, sizeof(_header.name) ) == 0 ) {
struct varInfo {
unsigned varCount;
unsigned varSize;
} *varInfo;
void *bytes;
TCArray values;
varInfo = (struct varInfo *)[_parameters bytes];
bytes = (void *)[_parameters bytes] + sizeof(*varInfo);
values = TCMakeArrayWithBytes( varInfo->varCount, varInfo->varSize, bytes );
[_delegate cheater:self didFindValues:values];
}
else if ( strncmp( "didcancelsearch", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterDidCancelSearch:self];
}
else if ( strncmp( "didclearsearch", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterDidClearSearch:self];
}
else if ( strncmp( "memdump", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheater:self didDumpMemory:_parameters];
}
else if ( strncmp( "didcanceldump", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterDidCancelMemoryDump:self];
}
else if ( strncmp( "changedvars", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheater:self didChangeVariables:[[NSUnarchiver unarchiveObjectWithData:_parameters] unsignedIntValue]];
}
else if ( strncmp( "didstopchanging", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterDidStopChangingVariables:self];
}
else if ( strncmp( "progress", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheater:self didReportProgress:[[NSUnarchiver unarchiveObjectWithData:_parameters] intValue]];
}
else if ( strncmp( "revertedto", _header.name, sizeof(_header.name) ) == 0 ) {
struct varInfo {
unsigned count;
unsigned varCount;
unsigned varSize;
} *varInfo;
void *bytes;
TCArray variables;
varInfo = (struct varInfo *)[_parameters bytes];
bytes = (void *)[_parameters bytes] + sizeof(*varInfo);
variables = TCMakeArrayWithBytes( varInfo->varCount, varInfo->varSize, bytes );
[_delegate cheater:self didRevertToVariables:variables actualAmount:varInfo->count];
}
else if ( strncmp( "didundo", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterDidUndo:self];
}
else if ( strncmp( "didredo", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheaterDidRedo:self];
}
else if ( strncmp( "varchanged", _header.name, sizeof(_header.name) ) == 0 ) {
NSArray *params = [NSUnarchiver unarchiveObjectWithData:_parameters];
[_delegate cheater:self variableAtIndex:[[params objectAtIndex:0] unsignedIntValue] didChangeTo:[params objectAtIndex:1]];
}
else if ( strncmp( "failed", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheater:self didFailLastRequest:[NSUnarchiver unarchiveObjectWithData:_parameters]];
}
else if ( strncmp( "echo", _header.name, sizeof(_header.name) ) == 0 ) {
[_delegate cheater:self echo:[NSUnarchiver unarchiveObjectWithData:_parameters]];
}
[_parameters release];
_parameters = nil;
}
@end