Source

RNA / packet.cpp

Full commit
  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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#include "packet.h"

extern CSocket *csocket;
extern int sock;


extern void login_result(unsigned char result);
extern void Debug(char *format, ...);

int count = 0;
u8 session_id[16] = {0};
u32 user_id = 0x0;
u8 security = 0x0;

char *recv_buffer = NULL;

char last_sent_trophy[16] = {0};
char last_sent_trophy_path[1024] = {0};

//Blowfish
u8 blowfish_key[8] = {0};
u8 byte0 = 0;
u8 byte1 = 0;
u8 byte2 = 0;
u8 byte3 = 0;
u8 byte4 = 0;
u8 byte5 = 0;
u8 byte6 = 0;
u8 byte7 = 0;

u32 ReadDWord(char *packet);
u16 ReadWord(char *packet);
u8 ReadByte(char *packet);
void ReadArray(char *packet, u8* out, int len);

void add_session_id(char *packet);

void process_A301(char *packet);
void process_A201(char *packet);
void process_A101(char *packet);
void build_1202( void );
void build_1203( void );
void process_6000(char *packet);
void build_6000_answer( void );

u8 chartohex(char ch){
	if(ch >= '0' && ch <= '9')
		return (u8)(ch - '0');

	if(ch >= 'a' && ch <= 'f')
		return (u8)(ch - 'a') + 0xA;
	
	if(ch >= 'A' && ch <= 'F')
		return (u8)(ch - 'A') + 0xA;

	return -1;
}

char hextochar(u8 ch){
	char buff[2];
	sprintf(buff, "%.1X", ch);

	return buff[0];
}

int set_crc_byte(char *packet){
	int len = strlen(packet);

	char *crc_packet = new char[len];
	memcpy(crc_packet, packet, len);

	crc_packet[10] = '0';
	crc_packet[11] = '0';

	for(int i = 0; i < len; i++)
		crc_packet[i] = toupper(crc_packet[i]);
	
	u32 crc = crc32buf(crc_packet, len);
	char crc_str[256] = {0};
	sprintf(crc_str, "%X", crc);

	char *crc_md5 = MDString(crc_str);
	
	u8 crc32 = 0;
	for(int i = 0; i < 16*2; i+=2)
		crc32 ^= ((chartohex(crc_md5[i]) << 4) + chartohex(crc_md5[i+1]) );	
	
	packet[10] = hextochar(crc32>>4);
	packet[11] = hextochar(crc32 & 0xF);

	return 1;
}

int set_security_byte(char *packet){
	u32 result = (0x3F*(~security+0x34));
	result = result ^ ( result >> 4 );
	result = result & 0xFF;
	security = result;

	packet[8] = hextochar((security>>4) & 0xF);
	packet[9] = hextochar(security & 0xF);

	return 1;
}

char* send_login(char *username, char *password){
	char path[1024] = {0};
	char packet[1024] = {0};
								
	signed char ulen = strlen(username);

	sprintf(packet, "0111%.2X000000%.2X", ulen + 16, ulen);

	for(int i = 0; i < ulen; i++){
		char buff[5];
		sprintf(buff, "%.2X", (u8)(char)(username[i]));
		strcat(packet, buff);
	}

	Debug("[LOGIN 1] %s\n", packet);

	char *md5_pass = MDString(password);
	strcat(packet, md5_pass);
	Debug("[MD5] %s\n", md5_pass);

	u8 crc32 = set_crc_byte(packet);	
	Debug("[LOGIN 2] %s\n", packet);
	
	sprintf(path, "/RNA/parse_packet.php?packet=%s", packet);

	csocket->remoteGET(sock, "localhost", path, "PKG", NULL);	
	Debug("[GET]\n");

	char buff[1024] = {0};
	csocket->remoteRecieve(sock, buff, 1024);	

	char *buffer = csocket->clearBuffer(buff);
	Debug("[CLEAR] %s\n", buffer);
	processPacket(buffer);

	return md5_pass;
}

void processPacket(char *packet){
	count = 0;

	if(!recv_buffer)
		recv_buffer = new char[8096];
	
	u16 opcode = ReadWord(packet);
	u16 size = ReadWord(packet);

	if(((size & 0xFF00) >> 12) == 0xB && opcode != 0x6000){
		cBlowFish *bl_de = new cBlowFish();
		bl_de->Initialize(blowfish_key, 8);

		int len = (size &  0x0FFF);
		
		int s_count = count;
		u8 readArray[1024] = {0};
		u8 decryptArray[1024] = {0};
		ReadArray(packet, decryptArray, len);
		count = s_count;

		bl_de->Decode(decryptArray, decryptArray, len);

		int loopl = bl_de->GetOutputLength(len);

		for(int i = 0; i < loopl; i++){
			char buff[4] = {0};
			sprintf(buff, "%c", (char)(decryptArray[i]));
			packet[8+i] = buff[0];
		}
	}

	switch(opcode){
		case 0x6000:
			process_6000(packet);
		break;
		case 0xA101:
			process_A101(packet);
		break;
		case 0xA201:
			process_A201(packet);
		break;
		case 0xA301:
			process_A301(packet);
		break;
		case 0xCCCC:
			login_result(2);
		break;
	}
}

void process_A101(char *packet){
	login_result(0);
}

void process_A301(char *packet){
	u32 num_friends = ReadDWord(packet);
		printf("NUM: %X\n", num_friends);
	for(u32 i = 0; i < num_friends; i++){
		printf("ID: %X\n", ReadDWord(packet));
		u8 nick_len = ReadByte(packet); 
		char *nick = new char[nick_len];
		
		for(u8 n = 0; n < nick_len; n++)
			nick[n] = (char)ReadByte(packet);
		nick[nick_len] = '\0';

		printf("NICK: %s\n", nick);
	}
}

void process_A201(char *packet){
	u8 flag = ReadByte(packet);

	if(flag == 0x0)
		build_1202();
	else
		build_1203();
}

void build_1202(){
	char packet[1024] = {0};
	char packet_raw[1024] = {0};
	sprintf(packet, "021216B00000");
	add_session_id(packet);

	char tropusr_path[1024] = {0};
	sprintf(tropusr_path, "%s\\TROPUSR.DAT", last_sent_trophy_path);

	tropusr_ret *ret = parse_tropusr(tropusr_path);
	
	strcat(packet_raw, last_sent_trophy);
	packet_raw[16] = ret->max_trophies;	

	int size = 17;

	while(ret->trophies_list){
		packet_raw[size] = ret->trophies_list->trophie->trophie_id;
		
		packet_raw[size+1] = ret->trophies_list->trophie->unlocked;
		
		packet_raw[size+2] = ret->trophies_list->trophie->platinum_unlocked;

		ret->trophies_list = ret->trophies_list->next;

		size += 3;
	}

	cBlowFish *temp = new cBlowFish();
	temp->Initialize(blowfish_key, 8);
	size = temp->Encode((u8*)packet_raw, (u8*)packet_raw, size);
	
	for(int i = 0; i < size; i++){
		char buff[4] = {0};
		sprintf(buff, "%.2X", (u8)(packet_raw[i]));
		strcat(packet, buff);
	}

	set_security_byte(packet);
	set_crc_byte(packet);

	char path[1024] = {0};
	sprintf(path, "/RNA/parse_packet.php?packet=%s", packet);
	csocket->remoteGET(sock, "localhost", path, "PKG", NULL);

	printf("[C->S] %s\n", packet);
	
	csocket->remoteRecieve(sock, recv_buffer, 8096);
	

	//LET'S SEND TROPCONF! =)
	char tropconf_path[1024] = {0};
	sprintf(tropconf_path, "%s\\TROPCONF.SFM", last_sent_trophy_path);

	FILE *fp = fopen(tropconf_path, "rb");
	fseek(fp, 0, SEEK_END);
	int fsize = ftell(fp);
	rewind(fp);

	char *buff = (char*)malloc(fsize + 5 - 0x1A1);
	memset(buff, 0, fsize+5 - 0x1A1);
	strcpy(buff, "info=");

	fseek(fp, 0x1A1, SEEK_CUR);
	fread((buff+5), 1, fsize - 0x1A1, fp);
	
	char packet2[1024] = {0};
	strcpy(packet2, "041200000000");
	add_session_id(packet2);
	set_security_byte(packet2);
	set_crc_byte(packet2);

	sprintf(path, "/RNA/parse_packet.php?len=%d&packet=%s", fsize - 0x1A1, packet2);
	csocket->remotePOST(sock, "localhost", path, buff, "PKG", NULL);
	
	csocket->remoteRecieve(sock, recv_buffer, 8096);

	free(buff);
}
void build_1203(){
	char packet[1024] = {0};
	char packet_raw[1024] = {0};
	sprintf(packet, "031216B00000");
	add_session_id(packet);

	char tropusr_path[1024] = {0};
	sprintf(tropusr_path, "%s\\TROPUSR.DAT", last_sent_trophy_path);

	tropusr_ret *ret = parse_tropusr(tropusr_path);
	
	strcat(packet_raw, last_sent_trophy);
	packet_raw[16] = ret->user_trophies;	

	if(ret->user_trophies == 0)
		return;

	int size = 17;	
	while(ret->trophies_list){
		if(ret->trophies_list->trophie->unlocked == 0x1){
			packet_raw[size] = ret->trophies_list->trophie->trophie_id;
		
			packet_raw[size+1] = ret->trophies_list->trophie->unlocked;
		
			packet_raw[size+2] = ret->trophies_list->trophie->platinum_unlocked;
					
			size += 3;
		}
		ret->trophies_list = ret->trophies_list->next;
	}
	
	cBlowFish *temp = new cBlowFish();
	temp->Initialize(blowfish_key, 8);
	size = temp->Encode((u8*)packet_raw, (u8*)packet_raw, size);
	
	for(int i = 0; i < size; i++){
		char buff[4] = {0};
		sprintf(buff, "%.2X", (u8)(packet_raw[i]));
		strcat(packet, buff);
	}

	set_security_byte(packet);
	set_crc_byte(packet);

	char path[1024] = {0};
	sprintf(path, "/RNA/parse_packet.php?packet=%s", packet);
	csocket->remoteGET(sock, "localhost", path, "PKG", NULL);

	printf("[C->S] %s\n", packet);
	
	csocket->remoteRecieve(sock, recv_buffer, 8096);
}

void process_6000(char *packet){
	u8 flag = ReadByte(packet);

	switch(flag){
		case 0x50:{
			security = ReadByte(packet);
			u8 result = ReadByte(packet); //0x5 -> Login succes

			if(result != 0x5)
				login_result(0);

			ReadByte(packet); //0x1 -> User permisses
			user_id = ReadDWord(packet); //save uid
			ReadArray(packet, session_id, 16); //save sid

			build_6000_answer();
		}break;
		case 0x55:{
			byte0 = ReadByte(packet);
			byte1 = ReadByte(packet);
			byte2 = ReadByte(packet);
			byte3 = ReadByte(packet);
			
			blowfish_key[0] = byte0;
			blowfish_key[1] = byte1;
			blowfish_key[2] = byte2;
			blowfish_key[3] = byte3;
			blowfish_key[4] = byte7;
			blowfish_key[5] = byte6;
			blowfish_key[6] = byte5;
			blowfish_key[7] = byte4;
			
			u8 magic[9] = {0};		
			for(int i = 0; i < 8; i++)
				magic[i] = ReadByte(packet);
			
			cBlowFish *blowfish = new cBlowFish();
			blowfish->Initialize(blowfish_key, 8);
			blowfish->Decode(magic, magic, 8);

			if(strcmp("00133700", (char*)magic) == 0){
				login_result(1);
				//LOGUED IN AND HANDSHAKE = 1
			}else
				login_result(0);
		}break;
	}
}

void add_session_id(char *packet){
	for(int i = 0; i < 16; i++){
		char buff[3];
		sprintf(buff, "%.2X", session_id[i]);
		strcat(packet, buff);
	}
}

void listFriends(){
	printf("\nRequesting Friends List\n");
	
	char packet[1024] = "011300000000";
	add_session_id(packet);
	set_security_byte(packet);
	set_crc_byte(packet);

	char path[1024] = {0};
	sprintf(path, "/RNA/parse_packet.php?packet=%s", packet);
	csocket->remoteGET(sock, "localhost", path, "PKG", NULL);
	
	printf("[C->S] %s\n", packet);

	csocket->remoteRecieve(sock, recv_buffer, 8096);
	bool end = false;
	bool old = false;
	while(!end){
		if(*recv_buffer == '\n'){
			if(!old)
				old = true;
			else
				end = true;
		}else if(*recv_buffer == '\r'){}
		else
			old = false;	
		
		recv_buffer++;
	}
	
	printf("[S->C] %s\n", recv_buffer);
	processPacket(recv_buffer);
}

void addFriend(){
	char packet[1024] = {0};

	strcpy(packet, "021300000000");
	add_session_id(packet);
	set_security_byte(packet);

	char buff[100];
	sprintf(buff, "04%.2X%.2X%.2X%.2X", 't', 'e', 's', 't');
	strcat(packet, buff);

	set_crc_byte(packet);

	char path[2048] = {0};
	sprintf(path, "/RNA/parse_packet.php?packet=%s", packet);
	csocket->remoteGET(sock, "localhost", path, "PKG", NULL);
	csocket->remoteRecieve(sock, recv_buffer, 8096);	
	bool end = false;
	bool old = false;
	while(!end){
		if(*recv_buffer == '\n'){
			if(!old)
				old = true;
			else
				end = true;
		}else if(*recv_buffer == '\r'){}
		else
			old = false;	
		
		recv_buffer++;
	}

	printf("[S->C] %s\n", recv_buffer);
}

void syncTrophies(){
	printf("Syncing Trophies\n");
	char packet[1024] = {0};
								//5 = ulen
	sprintf(packet, "011216000000");
	add_session_id(packet);

	DIR *dir, *sub_dir1, *subdir2;
    struct dirent *ent, *ent1, *ent2;

    /* open directory stream */
	dir = opendir ("I:\\home");
	if (dir != NULL) {
		while ((ent = readdir (dir)) != NULL) {
			if(strcmp(ent->d_name, ".") == 0)
				continue;
			else if(strcmp(ent->d_name, "..") == 0)
				continue;

			char path[1024] = {0};
			sprintf(path, "I:\\home\\%s\\trophy", ent->d_name);

			sub_dir1 = opendir (path);
			if (sub_dir1 != NULL) {
				while ((ent1 = readdir (sub_dir1)) != NULL) {
					if(strcmp(ent1->d_name, ".") == 0)
						continue;
					else if(strcmp(ent1->d_name, "..") == 0)
						continue;
					else if(strcmp(ent1->d_name, "_TROPSYS_") == 0)
						continue;

					sprintf(last_sent_trophy_path, "%s\\%s", path, ent1->d_name);
					strcpy(last_sent_trophy, ent1->d_name);

					char send_packet[1024] = {0};
					strcpy(send_packet, packet);
					
					for(int i = 0; i< 16; i++){
						char buff[3] = {0};
						if(i < strlen(ent1->d_name))
							sprintf(buff, "%.2X", (u8)(ent1->d_name[i]));
						else
							strcpy(buff, "00");
						strcat(send_packet, buff);
					}
					
					set_security_byte(send_packet);
					set_crc_byte(send_packet);

					char server_path[1024] = {0};
					sprintf(server_path, "/RNA/parse_packet.php?packet=%s", send_packet);
					csocket->remoteGET(sock, "localhost", server_path, "PKG", NULL);

					printf("[C->S] %s\n", send_packet);

					csocket->remoteRecieve(sock, recv_buffer, 8096);
					bool end = false;
					bool old = false;
					while(!end){
						if(*recv_buffer == '\n'){
							if(!old)
								old = true;
							else
								end = true;
						}else if(*recv_buffer == '\r'){}
						else
							old = false;	
		
						recv_buffer++;
					}
	
					printf("[S->C] %s\n", recv_buffer);
					processPacket(recv_buffer);
				}
			}
			
			closedir (sub_dir1);
		}
      
		closedir (dir);
	}
}

void build_6000_answer(){
	char packet[256] = {0};
	char path[256*2] = {0};
	sprintf(packet, "006020000000");
	add_session_id(packet);

	srand((unsigned)time(0)); 
    byte4 = rand() & 0xFF; 
	byte5 = rand() & 0xFF; 
	byte6 = rand() & 0xFF; 
	byte7 = rand() & 0xFF;

	char buff[9];
	sprintf(buff, "%.2X%.2X%.2X%.2X", byte4, byte5, byte6, byte7);
	strcat(packet, buff);

	set_security_byte(packet);
	set_crc_byte(packet);

	sprintf(path, "/RNA/parse_packet.php?packet=%s", packet);
	csocket->remoteGET(sock, "localhost", path, "PKG", NULL);

	printf("[C->S] %s\n", packet);

	csocket->remoteRecieve(sock, recv_buffer, 8096);
	bool end = false;
	bool old = false;
	while(!end){
		if(*recv_buffer == '\n'){
			if(!old)
				old = true;
			else
				end = true;
		}else if(*recv_buffer == '\r'){}
		else
			old = false;	
		
		recv_buffer++;
	}
	
	printf("[S->C] %s\n", recv_buffer);
	processPacket(recv_buffer);
}

u8 ReadByte(char *packet){
	u8 ret = (chartohex(packet[count]) << 4) + chartohex(packet[count + 1]);

	count += 1*2;
	return ret;
}

u16 ReadWord(char *packet){
	u16 ret = ((chartohex(packet[count]) << 4) + chartohex(packet[count + 1])) + (((chartohex(packet[count + 2]) << 4) + chartohex(packet[count + 3])) << 8);

	count += 2*2;
	return ret;
}

u32 ReadDWord(char *packet){
	u32 ret = ((chartohex(packet[count]) << 4) + chartohex(packet[count + 1])) + (((chartohex(packet[count + 2]) << 4) + chartohex(packet[count + 3])) << 8) + (((chartohex(packet[count + 4]) << 4) + chartohex(packet[count + 5])) << 16) + (((chartohex(packet[count + 6]) << 4) + chartohex(packet[count + 7])) << 24);

	count += 4*2;
	return ret;
}

void ReadArray(char *packet, u8* out, int len){
	for(int i = 0; i < len; i++)
		out[i] = ReadByte(packet);
}