Advertisement
BaSs_HaXoR

CCAPI.cpp (Primetime00's alt.)

Dec 3rd, 2014
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.13 KB | None | 0 0
  1. //SOURCE: http://www.ps3hax.net/showthread.php?t=73600
  2. #include "CCAPI.h"
  3. #include <iostream>
  4. #if defined(_WIN32) || defined(WIN32)
  5. #include <intrin.h>
  6. #define CLOSESOCKET(x) closesocket(x)
  7. #else
  8. #include <byteswap.h>
  9. #include <unistd.h>
  10. #include <cstring>
  11. #define CLOSESOCKET(x) close(x)
  12. #endif
  13.  
  14. using namespace std;
  15.  
  16. unsigned char CCAPI::bitConv[] = {0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF};
  17. bool CCAPI::initialized = false;
  18.  
  19. CCAPI::CCAPI(string ip)
  20. {
  21.     startup();
  22.     ipAddress = ip;
  23.     attachedPID = 0;
  24.     destination.sin_family = AF_INET;
  25.     destination.sin_port = htons (CCAPI_PORT);
  26.     sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  27.     if (sock < 0)
  28.     {
  29.         cout << "\nSocket Creation FAILED!" << endl;
  30.     }
  31.     dataBuffer = new char[CCAPI_DATA_BUFFER];
  32.     connected = attached = false;
  33. }
  34.  
  35.  
  36. int CCAPI::connect(void)
  37. {
  38.     struct timeval tv;
  39.     tv.tv_sec = 3;       /* Timeout in seconds */
  40.     //setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,(char *)&tv,sizeof(struct timeval));
  41.     //setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,(char *)&tv,sizeof(struct timeval));
  42.     destination.sin_addr.s_addr = inet_addr(ipAddress.c_str());
  43.     if (::connect(sock,(struct sockaddr *)&destination,sizeof(destination))!=0)
  44.     {
  45.         cout << "\nSocket Connection FAILED!" << endl;
  46.         if (sock)
  47.         {
  48.             CLOSESOCKET(sock);
  49.             return -1;
  50.         }
  51.     }
  52.     connected = true;
  53.     return 0;
  54. }
  55.  
  56.  
  57. int CCAPI::disconnect(void)
  58. {
  59.     connected = false;
  60.     attached = false;
  61.     if (sock)
  62.     {
  63.         CLOSESOCKET(sock);
  64.         return 0;
  65.     }
  66.     return -1;
  67. }
  68.  
  69. int CCAPI::receiveData(void)
  70. {
  71.     const unsigned int bufferSize = 4096;
  72.     unsigned int numOfBytes = 0;
  73.     unsigned int i, size, command;
  74.     //Set the socket timeout
  75.     struct timeval tv;
  76.     //tv.tv_sec = 15;       /* Timeout in seconds */
  77.     //setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO,(char *)&tv,sizeof(struct timeval));
  78.     //setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,(char *)&tv,sizeof(struct timeval));
  79.  
  80.     numOfBytes = recv(sock, dataBuffer, bufferSize, 0);
  81.     if (numOfBytes == -1) //we've disconnected
  82.         return -1;
  83.     for (i=0; i<numOfBytes; i++) {
  84.         dataBuffer[i] = _conv8(dataBuffer[i]);
  85.     }
  86.     while (numOfBytes < 16)
  87.     {
  88.         int _numOfBytes = recv(sock, &dataBuffer[numOfBytes], bufferSize, 0);
  89.         if (_numOfBytes == -1) return -1;//now we really disconnected!
  90.         for (i=numOfBytes; i<numOfBytes+_numOfBytes; i++) { dataBuffer[i] = _conv8(dataBuffer[i]); }
  91.         numOfBytes += _numOfBytes;
  92.     }
  93.     size = getDataSize(dataBuffer);
  94.     command = getDataCommand(dataBuffer);
  95.     while (numOfBytes < size)
  96.     {
  97.         int _numOfBytes = recv(sock, &dataBuffer[numOfBytes], bufferSize, 0);
  98.         if (_numOfBytes == -1) return -1;//now we really disconnected!
  99.         for (i=numOfBytes; i<numOfBytes+_numOfBytes; i++) { dataBuffer[i] = _conv8(dataBuffer[i]); }
  100.         numOfBytes += _numOfBytes;
  101.     }
  102.     switch (command)
  103.     {
  104.     case CCAPI_COMMAND_GETPROCESSLIST: parseProcessIDs(dataBuffer); break;
  105.     case CCAPI_COMMAND_GETPROCESSNAME: parseProcessName(dataBuffer); break;
  106.     case CCAPI_COMMAND_READPROCESSMEMORY: parseProcessMemory(dataBuffer); break;
  107.     default: break;
  108.     }
  109.     return 0;
  110. }
  111.  
  112. int CCAPI::parseProcessIDs(char *data)
  113. {
  114.     unsigned int *intData = (unsigned int*)data;
  115.     unsigned int numberOfProcs = BSWAP32( intData[4] );
  116.     unsigned int startLocation = 5;
  117.     processIDs.clear();
  118.     if (numberOfProcs > 20)
  119.         return -1;
  120.     for (unsigned int i=0, startLocation=5; i<numberOfProcs; i++, startLocation++)
  121.         processIDs.push_back( BSWAP32( intData[startLocation] ));
  122.     return processIDs.size();
  123. }
  124.  
  125. int CCAPI::parseProcessName(char *data)
  126. {
  127.     unsigned int startLocation = 16;
  128.     processName = "";
  129.     for (int i=0, startLocation=16; i<5000; i++, startLocation++) //we will look for a string with a max length of 5000 characters
  130.     {
  131.         if (data[startLocation] != 0)
  132.             processName.push_back( data[startLocation] );
  133.         else
  134.             break;
  135.     }
  136.     return 0;
  137. }
  138.  
  139. int CCAPI::parseProcessMemory(char *data)
  140. {
  141.     unsigned int startLocation = 16;
  142.     return 0;
  143. }
  144.  
  145. int CCAPI::requestProcessIDList(void)
  146. {
  147.     if (!sock)
  148.         return -1;
  149.     char header[30];
  150.     unsigned size=0;
  151.     constructHeader(header, size, CCAPI_SIZE_GETPROCESSLIST, CCAPI_COMMAND_GETPROCESSLIST);
  152.     if (send(sock, header, size, 0) != size)
  153.         return -1;
  154.     return 0;
  155. }
  156.  
  157.  
  158. int CCAPI::attach(void)
  159. {
  160.     attached = false;
  161.     if (!sock)
  162.         return -1;
  163.     attachedPID = 0;
  164.     requestProcessIDList();
  165.     if (receiveData() == -1)
  166.         return -1;
  167.     for (unsigned int i=0; i<processIDs.size(); i++)
  168.     {
  169.         requestProcessName(processIDs.at(i));
  170.         if (receiveData() == -1)
  171.             return -1;
  172.         if (processName.find("flash") == string::npos)
  173.         {
  174.             attachedPID = processIDs.at(i);
  175.             attached = true;
  176.             break;
  177.         }
  178.     }
  179.     return attachedPID;
  180. }
  181.  
  182. int CCAPI::readMemory(unsigned int offset, unsigned int length)
  183. {
  184.     if (!sock)
  185.         return -1;
  186.     if (attachedPID == 0)
  187.         return -2;
  188.     requestReadMemory(attachedPID, offset, length);
  189.     if (receiveData() == -1)
  190.         return -1;
  191.     return 0;
  192. }
  193.  
  194. int CCAPI::validateMemory(unsigned int offset)
  195. {
  196.     if (!sock)
  197.         return -1;
  198.     if (attachedPID == 0)
  199.         return -2;
  200.     requestReadMemory(attachedPID, offset, 1);
  201.     if (receiveData() == -1)
  202.         return -1;
  203.     return getDataValidity(dataBuffer) == 0;
  204. }
  205.  
  206. int CCAPI::writeMemory(unsigned int offset, unsigned int length, char *data)
  207. {
  208.     if (!sock)
  209.         return -1;
  210.     if (attachedPID == 0)
  211.         return -2;
  212.     //convert the memory here?
  213.     requestWriteMemory(attachedPID, offset, length, data);
  214.     if (receiveData() == -1)
  215.         return -1;
  216.     return 0;
  217. }
  218.  
  219. int CCAPI::requestProcessName(unsigned int processID)
  220. {
  221.     if (!sock)
  222.         return -1;
  223.     char header[64];
  224.     unsigned size=0;
  225.     constructHeader(header, size, CCAPI_SIZE_GETPROCESSNAME, CCAPI_COMMAND_GETPROCESSNAME);
  226.     processID = _conv32(processID);
  227.     memcpy(&header[size], &processID, PS3_INT_SIZE); size+=PS3_INT_SIZE;
  228.     if (send(sock, header, size, 0) != size)
  229.         return -1;
  230.     return 0;
  231. }
  232.  
  233. int CCAPI::requestReadMemory(unsigned int processID, unsigned int offset, unsigned int length)
  234. {
  235.     if (!sock)
  236.         return -1;
  237.     char header[64];
  238.     unsigned size=0;
  239.     constructHeader(header, size, CCAPI_SIZE_READPROCESSMEMORY, CCAPI_COMMAND_READPROCESSMEMORY);
  240.     unsigned int pack[] = { _conv32(processID), 0, _conv32(offset), _conv32(length) };
  241.     memcpy(&header[size], pack, PS3_INT_SIZE*4); size+=PS3_INT_SIZE*4;
  242.     if (send(sock, header, size, 0) != size)
  243.         return -1;
  244.     return 0;
  245. }
  246.  
  247. int CCAPI::requestWriteMemory(unsigned int processID, unsigned int offset, unsigned int length, char *data)
  248. {
  249.     if (!sock)
  250.         return -1;
  251.     char header[64];
  252.     unsigned size=0;
  253.     constructHeader(header, size, CCAPI_SIZE_WRITEPROCESSMEMORY+length, CCAPI_COMMAND_WRITEPROCESSMEMORY);
  254.     unsigned int pack[] = { _conv32(processID), 0, _conv32(offset), _conv32(length) };
  255.     memcpy(&header[size], pack, PS3_INT_SIZE*4); size+=PS3_INT_SIZE*4;
  256.     for (int i=0; i<length; i++)
  257.         data[i] = bitConv[(unsigned char)data[i]];
  258.     memcpy(&header[size], data, length); size+=length;
  259.     if (send(sock, header, size, 0) != size)
  260.         return -1;
  261.     return 0;
  262. }
  263.  
  264. void CCAPI::constructHeader(char *buffer, unsigned int &buffersize, unsigned int size, unsigned int command, unsigned int third, unsigned int fourth)
  265. {
  266.     size = _conv32(size);//_conv32(size);
  267.     command = _conv32(command);
  268.     third = _conv32(third);
  269.     //third = _conv32(third);
  270.     fourth = _conv32(fourth);
  271.     unsigned int i=0;
  272.     memcpy(&buffer[i], &size, sizeof(unsigned int)); i+=sizeof(unsigned int);
  273.     memcpy(&buffer[i], &command, sizeof(unsigned int)); i+=sizeof(unsigned int);
  274.     memcpy(&buffer[i], &third, sizeof(unsigned int)); i+=sizeof(unsigned int);
  275.     memcpy(&buffer[i], &fourth, sizeof(unsigned int)); i+=sizeof(unsigned int);
  276.     buffersize = i;
  277. }
  278.  
  279. bool CCAPI::insertData(unsigned int pos, char *data, unsigned int size)
  280. {
  281.     if (pos+size >= CCAPI_DATA_BUFFER-16)
  282.         return false;
  283.     char *dp = &dataBuffer[16];
  284.     memcpy(&dp[pos], data, size);
  285.     return true;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement