Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #define LOGINSERVERPORT 2000
- #define LOGINSERVERIP "64.124.47.60"
- #define CLIENTVERSION1 6
- #define CLIENTVERSION2 8
- #define CLIENTVERSION3 0
- #define CLIENTVERSION4 0
- #define CLIENTNAME "baram\n"
- #define USERNAME "gniu"
- #define PASSWORD "aaa1"
- #define LENGTHOFUSERNAME 4
- #define LENGTHOFPASSWORD 4
- //#define DEBUG 0
- struct packets
- {
- int n;
- char **buffers;
- int *lengths;
- };
- struct packets *splitpacket(unsigned char *buffer, int result)
- {
- struct packets *packet = malloc(sizeof(struct packets));
- packet->buffers = malloc(sizeof(char*) * 256);
- packet->lengths = malloc(sizeof(int) * 256);
- packet->n = 0;
- int i;
- for (i = 0; i < result;)
- {
- packet->buffers[packet->n] = buffer;
- packet->n = packet->n + 1;
- int length = (buffer[2] | (buffer[1] >> 8)) + 3;
- buffer = buffer + length;
- i = i + length;
- }
- return packet;
- }
- void crypt(unsigned char *buffer, unsigned int length, unsigned char increment)
- {
- unsigned char key[9] = {'N', 'e', 'x', 'o', 'n', 'I', 'n', 'c', '.'};
- unsigned int n;
- for (n = 0; n < length; n++)
- {
- buffer[n] ^= key[n%9];
- unsigned char x = n / 9;
- if (x == increment)
- {
- x = 0;
- }
- buffer[n] ^= x;
- buffer[n] ^= increment;
- }
- }
- void identifypacket(unsigned char *buffer, int result)
- {
- /*if (buffer[3] == 0x0d)
- {
- int inc = buffer[4];
- buffer = buffer + 5;
- crypt(buffer, result-5, inc);
- printf("%s\n", buffer+6);
- buffer = buffer - 5;
- }
- else if (buffer[3] == 0x0a) //chat and whisper
- {
- int inc = buffer[4];
- buffer = buffer + 5;
- crypt(buffer, result-5, inc);
- int length = buffer[2];
- buffer = buffer + 3;
- /int x;
- for (x = 0; x < length; x++)
- {
- putchar(buffer[x]);
- }
- putchar(10); //newline
- buffer = buffer - 8;
- }
- else if (buffer[3] == 0x1a) //emote
- {
- //sometimes terminated by two 0x00 instead of
- int inc = buffer[4];
- buffer = buffer + 5;
- crypt(buffer, result - 5, inc);
- printf("%02x%02x%02x%02x emoted %02x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
- buffer = buffer - 5;
- }*/
- unsigned int length = (buffer[2] | (buffer[1] >> 8)) - 2;
- unsigned char id = buffer[3];
- unsigned char inc = buffer[4];
- buffer = buffer + 5;
- crypt(buffer, length, inc);
- if (id == 0x1a) //emote | sometimes terminated by 0x00 0x00 instead of 0x00
- {
- /*
- a - p
- a 0b music
- b 0c tears
- c 0d red face
- d 0e wink heart
- e 0f boring
- f 10 snoring (also used when afk) TODO: how long does it take? and at what intervals?
- g 11 stars
- h 12 mad steam
- i 13 tongue out
- j 14 shoulder shrug
- k 15 spit steam
- l 16 dance music
- m 09 bow
- n 0a fist up happy
- o 17 embarassed
- p 18 bow kiss
- attack (space bar) = 01
- drop (,) = 04
- */
- //printf("%02x%02x%02x%02x emoted %02x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
- return;
- }
- else if (id == 0x0a) //whsiper/sage/wisdom | not null terminated, be careful
- {
- /*int messagelength = buffer[2];
- buffer = buffer + 3;
- int x;
- for (x = 0; x < messagelength; x++)
- {
- putchar(buffer[x]);
- }
- putchar(10);*/
- return;
- }
- else if (id == 0x0d) //talk
- {
- //printf("%s\n", buffer + 6);
- return;
- }
- else if (id == 0x11) //face direction, also something else, change frame animation?
- {
- /*
- 00 up
- 01 right
- 02 down
- 03 left
- */
- //printf("%02x%02x%02x%02x is now facing %02x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
- return;
- }
- else if (id == 0x0c)
- {
- /*
- 00 up
- 01 right
- 02 down
- 03 left
- */
- //printf("%02x%02x%02x%02x moved from %02x%02x, %02x%02x (X,Y) %02x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8]);
- return;
- }
- else if (id == 0x19)
- {
- //printf("attack\n");
- return;
- }
- else if (id == 0x1d)
- {
- //printf("equip\n");
- return;
- }
- else if (id == 0x33)
- {
- //printf("new object on screen\n"0;
- return;
- }
- else if (id == 0x0e)
- {
- //printf("object left building\n");
- return;
- }
- else if (id == 0x08 || id == 0x68 || id == 0x3b)
- {
- return;
- }
- else
- {
- int x;
- for (x = 0; x < length; x++)
- {
- if (buffer[x] == 0x00 && buffer[x+1] == 0x01 && buffer[x+2] == 0x00 && buffer[x+4] == 0x04)
- {
- printf("AH!\n");
- }
- }
- /*char *filename = malloc(64);
- sprintf(filename, "log/%02x.txt", id);
- FILE *fd = fopen(filename, "a");
- int x;
- for (x = 0; x < length; x++)
- {
- fprintf(fd, "%02x ", buffer[x]);
- }
- fprintf(fd, "\n");
- fclose(fd);*/
- printf("%d\t%02x\t%3d\n", time(NULL), id, length);
- }
- }
- int main(void)
- {
- int sockfd = socket(AF_INET, SOCK_STREAM, 0);
- struct sockaddr_in addr;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(LOGINSERVERPORT);
- inet_aton(LOGINSERVERIP, (struct in_addr*)&addr.sin_addr);
- connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
- #ifdef DEBUG
- printf("connected to login server\n");
- #endif
- unsigned char *connectedserver = malloc(22);
- recv(sockfd, connectedserver, 22, 0);
- #ifdef DEBUG
- printf("recved CONNECTED SERVER\n");
- #endif
- free(connectedserver);
- //you have 6 seconds to send BOTH packets or you will be disconnected
- unsigned char *baram = malloc(10);
- baram[0] = 0xaa;
- baram[1] = 0x00;
- baram[2] = 0x07;
- baram[3] = CLIENTNAME[0];
- baram[4] = 0x00;
- baram = baram + 5;
- baram[0] = CLIENTNAME[1];
- baram[1] = CLIENTNAME[2];
- baram[2] = CLIENTNAME[3];
- baram[3] = CLIENTNAME[4];
- baram[4] = CLIENTNAME[5];
- crypt(baram, 5, 0);
- baram = baram - 5;
- send(sockfd, baram, 10, 0);
- #ifdef DEBUG
- printf("sent baram\n");
- #endif
- free(baram);
- /* 0000 aa 00 08 00 02 a8 c5 00 00 01 00 ...........*/
- unsigned char *version = malloc(11);
- version[0] = 0xaa;
- version[1] = 0x00;
- version[2] = 0x08;
- version[3] = 0x00;
- unsigned int clientversion = CLIENTVERSION1 * 100 + CLIENTVERSION2 * 10 + CLIENTVERSION3;
- version[4] = clientversion >> 8;
- version[5] = clientversion & 0xFF;
- version[6] = 0xC5;
- version[7] = 0x00;
- version[8] = CLIENTVERSION4;
- version[9] = 0x01;
- version[10] = 0x00;
- send(sockfd, version, 11, 0);
- #ifdef DEBUG
- printf("sent client version\n");
- #endif
- free(version);
- //TODO: if client version is not 6 8 0 0
- unsigned char *unknown = malloc(20);
- recv(sockfd, unknown, 20, 0);
- #ifdef DEBUG
- printf("recved unknown\n");
- #endif
- /*
- id = 0x00
- unencrypted
- unknown[5] - unknown[9] = unknown static bytes
- unknown[10] = key length
- unknown[11] - unknown[19] = key
- */
- free(unknown);
- unsigned char sendinc = 1;
- unsigned char *skinny = malloc(7);
- skinny[0] = 0xaa;
- skinny[1] = 0x00;
- skinny[2] = 0x04;
- skinny[3] = 0x7b;
- skinny[4] = sendinc;
- skinny = skinny + 5;
- skinny[0] = 0x01;
- skinny[1] = 0x00;
- crypt(skinny, 2, sendinc);
- skinny = skinny - 5;
- send(sockfd, skinny, 7, 0);
- #ifdef DEBUG
- printf("sent skinny packet\n");
- #endif
- free(skinny);
- sendinc = sendinc + 1;
- unsigned char *charicinfo = malloc(512);
- recv(sockfd, charicinfo, 512, 0);
- #ifdef DEBUG
- printf("recved charic info\n");
- #endif
- free(charicinfo);
- unsigned char *login = malloc(LENGTHOFUSERNAME + LENGTHOFPASSWORD + 12);
- login[0] = 0xaa;
- login[1] = 0x00;
- login[2] = LENGTHOFUSERNAME + LENGTHOFPASSWORD + 9;
- login[3] = 0x03;
- login[4] = sendinc;
- login = login + 5;
- register unsigned int n = 1;
- register unsigned int i;
- login[0] = LENGTHOFUSERNAME;
- for (i = 0; i < LENGTHOFUSERNAME; i++, n++)
- {
- login[n] = USERNAME[i];
- }
- login[n] = LENGTHOFPASSWORD;
- n = n + 1;
- for (i = 0; i < LENGTHOFPASSWORD; i++, n++)
- {
- login[n] = PASSWORD[i];
- }
- login[n] = 0x4e;
- login[n+1] = 0xec;
- login[n+2] = 0x57;
- login[n+3] = 0x23;
- login[n+4] = 0x00;
- crypt(login, LENGTHOFUSERNAME + LENGTHOFPASSWORD + 7, sendinc);
- login = login - 5;
- send(sockfd, login, LENGTHOFUSERNAME + LENGTHOFPASSWORD + 12, 0);
- #ifdef DEBUG
- printf("sent login\n");
- #endif
- free(login);
- sendinc = sendinc + 1;
- unsigned char *response = malloc(256);
- int result = recv(sockfd, response, 256, 0);
- #ifdef DEBUG
- printf("recved login response\n");
- #endif
- if (result != 40)
- {
- #ifdef DEBUG
- printf("login response is not the info to the game server, error!\n");
- #endif
- unsigned char inc = response[4];
- response = response + 5;
- crypt(response, result, inc);
- if (result == 115) //Incorrect password. If you can not remember your password you can get a new one from the Nexus patron page.
- {
- printf("%s\n", response + 2);
- }
- else //That name does not exist.
- {
- printf("%s\n", response + 1);
- }
- response = response - 5;
- free(response);
- }
- else
- {
- #ifdef DEBUG
- printf("login response is ok! connect to game server\n");
- #endif
- response = response + 8; //skip the first packet, it is just 00 00 00 unencrypted saying "correct login"
- unsigned char *gsip = malloc(4);
- unsigned int gsport;
- gsip[0] = response[7]; //the ip is sent in reverse
- gsip[1] = response[6];
- gsip[2] = response[5];
- gsip[3] = response[4];
- gsport = (response[9] | (response[8] << 8));
- unsigned char importantbytes[2];
- importantbytes[0] = response[24 + LENGTHOFUSERNAME];
- importantbytes[1] = response[25 + LENGTHOFUSERNAME];
- response = response - 8;
- free(response);
- unsigned char *cresponse = malloc(9);
- cresponse[0] = 0xaa;
- cresponse[1] = 0x00;
- cresponse[2] = 0x06;
- cresponse[3] = 0x1b;
- cresponse[4] = sendinc;
- cresponse = cresponse + 5;
- cresponse[0] = 0x01;
- cresponse[1] = 0x0d;
- cresponse[2] = 0x00;
- cresponse[3] = 0x00;
- crypt(cresponse, 4, sendinc);
- cresponse = cresponse - 5;
- send(sockfd, cresponse, 9, 0);
- #ifdef DEBUG
- printf("sent client response from servers OK to connect to game server\n");
- #endif
- free(cresponse);
- sendinc = 0;
- shutdown(sockfd, SHUT_RDWR);
- close(sockfd);
- #ifdef DEBUG
- printf("closed login server socket\n");
- #endif
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(gsport);
- addr.sin_addr.s_addr = htonl((gsip[0] << 24) | (gsip[1] << 16) | (gsip[2] << 8) | gsip[3]);
- connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
- #ifdef DEBUG
- printf("connected to game server\n");
- #endif
- unsigned char *intro = malloc(22 + LENGTHOFUSERNAME);
- intro[0] = 0xaa;
- intro[1] = 0x00;
- intro[2] = 19 + LENGTHOFUSERNAME;
- intro[3] = 0x10;
- intro[4] = sendinc;
- intro[5] = 0x09;
- intro[6] = 0x4e;
- intro[7] = 0x65;
- intro[8] = 0x78;
- intro[9] = 0x6f;
- intro[10] = 0x6e;
- intro[11] = 0x49;
- intro[12] = 0x6e;
- intro[13] = 0x63;
- intro[14] = 0x2e;
- intro[15] = LENGTHOFUSERNAME;
- n = 16;
- for (i = 0; i < LENGTHOFUSERNAME; i++, n++)
- {
- intro[n] = USERNAME[i];
- }
- intro[n] = 0x00;
- intro[n+1] = 0x00;
- intro[n+2] = importantbytes[0];
- intro[n+3] = importantbytes[1];
- intro[n+4] = 0x01;
- intro[n+5] = 0x00;
- send(sockfd, intro, 22 + LENGTHOFUSERNAME, 0);
- #ifdef DEBUG
- printf("sent intro packet to game server\n");
- #endif
- free(intro);
- sendinc = sendinc + 1;
- }
- time_t a = time(NULL);
- while (1)
- {
- unsigned char *buffer = malloc(65536);
- int result = recv(sockfd, buffer, 65536, 0);
- #ifdef DEBUG
- printf("recved data in while(1) loop\n");
- #endif
- if (result == 0)
- {
- printf("ERROR: result == 0\n");
- free(buffer);
- close(sockfd);
- return -1;
- }
- int length = buffer[2] | (buffer[3] >> 8) + 3;
- if (length != result)
- {
- #ifdef DEBUG
- printf("length of received data is not the same as result\n");
- #endif
- struct packets *packet = splitpacket(buffer, result);
- #ifdef DEBUG
- printf("split packets\n");
- #endif
- register int n;
- for (n = 0; n < packet-> n; n++)
- {
- identifypacket(packet->buffers[n], packet->lengths[n]);
- }
- #ifdef DEBUG
- printf("identifed packet\n");
- #endif
- free(packet->lengths);
- free(packet->buffers);
- free(packet);
- }
- else
- {
- #ifdef DEBUG
- printf("length of packet is equal to result, only 1 packet\n");
- #endif
- identifypacket(buffer, result);
- #ifdef DEBUG
- printf("identified packet\n");
- #endif
- }
- //printf("%d\t%d\n", time(NULL) - a, result);
- free(buffer);
- time_t b = time(NULL);
- if (b >= a + 32)
- {
- unsigned char *keepalive = malloc(8);
- keepalive[0] = 0xaa;
- keepalive[1] = 0x00;
- keepalive[2] = 0x05;
- keepalive[3] = 0x1b;
- keepalive[4] = sendinc;
- keepalive = keepalive + 5;
- keepalive[0] = 0x00;
- keepalive[1] = 0x00;
- keepalive[2] = 0x00;
- crypt(keepalive, 3, sendinc);
- keepalive = keepalive - 5;
- send(sockfd, keepalive, 8, 0);
- free(keepalive);
- sendinc = sendinc + 1;
- a = a + 32;
- }
- }
- shutdown(sockfd, SHUT_RDWR);
- close(sockfd);
- return 0;
- }
- /*
- 6 right
- 4 up
- 0000 aa 00 0b 32 1c 50 d2 34 73 75 55 70 7f 32 ...2.P.4suUp.2
- aa 00 0b
- 32
- 1c
- */
Add Comment
Please, Sign In to add comment