Advertisement
GLADzTeguhID

[+] Linux Kernel 2.6.32 Local Root Exploit ( x86 x64 )

Jan 26th, 2017
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.13 KB | None | 0 0
  1. /*
  2.  
  3.  ______                  _   _                _____ _          _ _   _______                  
  4. |  ____|                | | (_)              / ____| |        | | | |__   __|                  
  5. | |__   __ _ _   _ _ __ | |_ _  __ _ _ __   | (___ | |__   ___| | |    | | ___  __ _ _ __ ___  
  6. |  __| / _` | | | | '_ \| __| |/ _` | '_ \   \___ \| '_ \ / _ \ | |    | |/ _ \/ _` | '_ ` _ \
  7. | |___| (_| | |_| | |_) | |_| | (_| | | | |  ____) | | | |  __/ | |    | |  __/ (_| | | | | | |
  8. |______\__, |\__, | .__/ \__|_|\__,_|_| |_| |_____/|_| |_|\___|_|_|    |_|\___|\__,_|_| |_| |_|
  9.         __/ | __/ | |                                                                          
  10.        |___/ |___/|_|                            
  11.                                              
  12. ~~~ :xD: Have fun. Don't forget To Bookmark This Website :xD:
  13. ~~~ http://www.egyptianshell.com/
  14. ~~~ Dr.FarFar & Mr.MaGMA & Th3 Mast3r
  15.  
  16. */
  17.  
  18. ###################################################################################################
  19.  
  20. #define _GNU_SOURCE
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26.  
  27. #define KSYM_NAME_LEN        127
  28.  
  29. struct sym_entry {
  30. unsigned long long addr;
  31. unsigned int len;
  32. unsigned char *sym;
  33. };
  34.  
  35. static struct sym_entry *table;
  36. static unsigned int table_size, table_cnt;
  37. static unsigned long long _text, _stext, _etext, _sinittext, _einittext, _***tratext, _eextratext;
  38. static int all_symbols = 0;
  39. static char symbol_prefix_char =;
  40.  
  41. int token_profit[0x10000];
  42.  
  43. /* the table that holds the result of the compression */
  44. unsigned char best_table[256][2];
  45. unsigned char best_table_len[256];
  46.  
  47. static void usage(void)
  48. {
  49. fprintf(stderr, “Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n”);
  50. exit(1);
  51. }
  52.  
  53. /*
  54. * This ignores the intensely annoying “mapping symbols” found
  55. * in ARM ELF files: $a, $t and $d.
  56. */
  57. static inline int is_arm_mapping_symbol(const char *str)
  58. {
  59. return str[0] == ‘$’ && strchr(“atd”, str[1])
  60. && (str[2] ==|| str[2] == ‘.’);
  61. }
  62.  
  63. static int read_symbol(FILE *in, struct sym_entry *s)
  64. {
  65. char str[500];
  66. char *sym, stype;
  67. int rc;
  68.  
  69. rc = fscanf(in,%llx %c %499s\n”, &s->addr, &stype, str);
  70. if (rc != 3) {
  71. if (rc != EOF) {
  72. /* skip line */
  73. fgets(str, 500, in);
  74. }
  75. return -1;
  76. }
  77.  
  78. sym = str;
  79. /* skip prefix char */
  80. if (symbol_prefix_char && str[0] == symbol_prefix_char)
  81. sym++;
  82.  
  83. /* Ignore most absolute/undefined (?) symbols. */
  84. if (strcmp(sym, “_text”) == 0)
  85. _text = s->addr;
  86. else if (strcmp(sym, “_stext”) == 0)
  87. _stext = s->addr;
  88. else if (strcmp(sym, “_etext”) == 0)
  89. _etext = s->addr;
  90. else if (strcmp(sym, “_sinittext”) == 0)
  91. _sinittext = s->addr;
  92. else if (strcmp(sym, “_einittext”) == 0)
  93. _einittext = s->addr;
  94. else if (strcmp(sym, “_***tratext”) == 0)
  95. _***tratext = s->addr;
  96. else if (strcmp(sym, “_eextratext”) == 0)
  97. _eextratext = s->addr;
  98. else if (toupper(stype) == ‘A’)
  99. {
  100. /* Keep these useful absolute symbols */
  101. if (strcmp(sym, “__kernel_syscall_via_break”) &&
  102. strcmp(sym, “__kernel_syscall_via_epc”) &&
  103. strcmp(sym, “__kernel_sigtramp”) &&
  104. strcmp(sym, “__gp”))
  105. return -1;
  106.  
  107. }
  108. else if (toupper(stype) == ‘U’ ||
  109. is_arm_mapping_symbol(sym))
  110. return -1;
  111. /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
  112. else if (str[0] == ‘$’)
  113. return -1;
  114.  
  115. /* include the type field in the symbol name, so that it gets
  116. * compressed together */
  117. s->len = strlen(str) + 1;
  118. s->sym = malloc(s->len + 1);
  119. if (!s->sym) {
  120. fprintf(stderr, “kallsyms failure:
  121. “unable to allocate required amount of memory\n”);
  122. exit(EXIT_FAILURE);
  123. }
  124. strcpy((char *)s->sym + 1, str);
  125. s->sym[0] = stype;
  126.  
  127. return 0;
  128. }
  129.  
  130. static int symbol_valid(struct sym_entry *s)
  131. {
  132. /* Symbols which vary between passes.  Passes 1 and 2 must have
  133. * identical symbol lists.  The kallsyms_* symbols below are only added
  134. * after pass 1, they would be included in pass 2 when –all-symbols is
  135. * specified so exclude them to get a stable symbol list.
  136. */
  137. static char *special_symbols[] = {
  138. “kallsyms_addresses”,
  139. “kallsyms_num_syms”,
  140. “kallsyms_names”,
  141. “kallsyms_markers”,
  142. “kallsyms_token_table”,
  143. “kallsyms_token_index”,
  144.  
  145. /* Exclude linker generated symbols which vary between passes */
  146. “_SDA_BASE_”,        /* ppc */
  147. “_SDA2_BASE_”,        /* ppc */
  148. NULL };
  149. int i;
  150. int offset = 1;
  151.  
  152. /* skip prefix char */
  153. if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
  154. offset++;
  155.  
  156. /* if –all-symbols is not specified, then symbols outside the text
  157. * and inittext sections are discarded */
  158. if (!all_symbols) {
  159. if ((s->addr < _stext || s->addr > _etext)
  160. && (s->addr < _sinittext || s->addr > _einittext)
  161. && (s->addr < _***tratext || s->addr > _eextratext))
  162. return 0;
  163. /* Corner case.  Discard any symbols with the same value as
  164. * _etext _einittext or _eextratext; they can move between pass
  165. * 1 and 2 when the kallsyms data are added.  If these symbols
  166. * move then they may get dropped in pass 2, which breaks the
  167. * kallsyms rules.
  168. */
  169. if ((s->addr == _etext && strcmp((char*)s->sym + offset, “_etext”)) ||
  170. (s->addr == _einittext && strcmp((char*)s->sym + offset, “_einittext”)) ||
  171. (s->addr == _eextratext && strcmp((char*)s->sym + offset, “_eextratext”)))
  172. return 0;
  173. }
  174.  
  175. /* Exclude symbols which vary between passes. */
  176. if (strstr((char *)s->sym + offset, “_compiled.”))
  177. return 0;
  178.  
  179. for (i = 0; special_symbols[i]; i++)
  180. if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
  181. return 0;
  182.  
  183. return 1;
  184. }
  185.  
  186. static void read_map(FILE *in)
  187. {
  188. while (!feof(in)) {
  189. if (table_cnt >= table_size) {
  190. table_size += 10000;
  191. table = realloc(table, sizeof(*table) * table_size);
  192. if (!table) {
  193. fprintf(stderr, “out of memory\n”);
  194. exit (1);
  195. }
  196. }
  197. if (read_symbol(in, &table[table_cnt]) == 0)
  198. table_cnt++;
  199. }
  200. }
  201.  
  202. static void output_label(char *label)
  203. {
  204. if (symbol_prefix_char)
  205. printf(“.globl %c%s\n”, symbol_prefix_char, label);
  206. else
  207. printf(“.globl %s\n”, label);
  208. printf(“\tALGN\n”);
  209. if (symbol_prefix_char)
  210. printf(%c%s:\n”, symbol_prefix_char, label);
  211. else
  212. printf(%s:\n”, label);
  213. }
  214.  
  215. /* uncompress a compressed symbol. When this function is called, the best table
  216. * might still be compressed itself, so the function needs to be recursive */
  217. static int expand_symbol(unsigned char *data, int len, char *result)
  218. {
  219. int c, rlen, total=0;
  220.  
  221. while (len) {
  222. c = *data;
  223. /* if the table holds a single char that is the same as the one
  224. * we are looking for, then end the search */
  225. if (best_table[c][0]==c && best_table_len[c]==1) {
  226. *result++ = c;
  227. total++;
  228. } else {
  229. /* if not, recurse and expand */
  230. rlen = expand_symbol(best_table[c], best_table_len[c], result);
  231. total += rlen;
  232. result += rlen;
  233. }
  234. data++;
  235. len–;
  236. }
  237. *result=0;
  238.  
  239. return total;
  240. }
  241.  
  242. static void write_src(void)
  243. {
  244. unsigned int i, k, off;
  245. unsigned int best_idx[256];
  246. unsigned int *markers;
  247. char buf[KSYM_NAME_LEN+1];
  248.  
  249. printf(#include <asm/types.h>\n”);
  250. printf(#if BITS_PER_LONG == 64\n”);
  251. printf(#define PTR .quad\n”);
  252. printf(#define ALGN .align 8\n”);
  253. printf(#else\n”);
  254. printf(#define PTR .long\n”);
  255. printf(#define ALGN .align 4\n”);
  256. printf(#endif\n”);
  257.  
  258. printf(“.data\n”);
  259.  
  260. /* Provide proper symbols relocatability by their ‘_text’
  261. * relativeness.  The symbol names cannot be used to construct
  262. * normal symbol references as the list of symbols contains
  263. * symbols that are declared static and are private to their
  264. * .o files.  This prevents .tmp_kallsyms.o or any other
  265. * object from referencing them.
  266. */
  267. output_label(“kallsyms_addresses”);
  268. for (i = 0; i < table_cnt; i++) {
  269. if (toupper(table[i].sym[0]) != ‘A’) {
  270. printf(“\tPTR\t_text + %#llx\n”,
  271. table[i].addr – _text);
  272. } else {
  273. printf(“\tPTR\t%#llx\n”, table[i].addr);
  274. }
  275. }
  276. printf(“\n”);
  277.  
  278. output_label(“kallsyms_num_syms”);
  279. printf(“\tPTR\t%d\n”, table_cnt);
  280. printf(“\n”);
  281.  
  282. /* table of offset markers, that give the offset in the compressed stream
  283. * every 256 symbols */
  284. markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
  285. if (!markers) {
  286. fprintf(stderr, “kallsyms failure:
  287. “unable to allocate required memory\n”);
  288. exit(EXIT_FAILURE);
  289. }
  290.  
  291. output_label(“kallsyms_names”);
  292. off = 0;
  293. for (i = 0; i < table_cnt; i++) {
  294. if ((i & 0xFF) == 0)
  295. markers[i >> 8] = off;
  296.  
  297. printf(“\t.byte 0x%02x”, table[i].len);
  298. for (k = 0; k < table[i].len; k++)
  299. printf(, 0x%02x”, table[i].sym[k]);
  300. printf(“\n”);
  301.  
  302. off += table[i].len + 1;
  303. }
  304. printf(“\n”);
  305.  
  306. output_label(“kallsyms_markers”);
  307. for (i = 0; i < ((table_cnt + 255) >> 8); i++)
  308. printf(“\tPTR\t%d\n”, markers[i]);
  309. printf(“\n”);
  310.  
  311. free(markers);
  312.  
  313. output_label(“kallsyms_token_table”);
  314. off = 0;
  315. for (i = 0; i < 256; i++) {
  316. best_idx[i] = off;
  317. expand_symbol(best_table[i], best_table_len[i], buf);
  318. printf(“\t.asciz\t\”%s\”\n”, buf);
  319. off += strlen(buf) + 1;
  320. }
  321. printf(“\n”);
  322.  
  323. output_label(“kallsyms_token_index”);
  324. for (i = 0; i < 256; i++)
  325. printf(“\t.short\t%d\n”, best_idx[i]);
  326. printf(“\n”);
  327. }
  328.  
  329. /* table lookup compression functions */
  330.  
  331. /* count all the possible tokens in a symbol */
  332. static void learn_symbol(unsigned char *symbol, int len)
  333. {
  334. int i;
  335.  
  336. for (i = 0; i < len – 1; i++)
  337. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
  338. }
  339.  
  340. /* decrease the count for all the possible tokens in a symbol */
  341. static void forget_symbol(unsigned char *symbol, int len)
  342. {
  343. int i;
  344.  
  345. for (i = 0; i < len – 1; i++)
  346. token_profit[ symbol[i] + (symbol[i + 1] << 8) ];
  347. }
  348.  
  349. /* remove all the invalid symbols from the table and do the initial token count */
  350. static void build_initial_tok_table(void)
  351. {
  352. unsigned int i, pos;
  353.  
  354. pos = 0;
  355. for (i = 0; i < table_cnt; i++) {
  356. if ( symbol_valid(&table[i]) ) {
  357. if (pos != i)
  358. table[pos] = table[i];
  359. learn_symbol(table[pos].sym, table[pos].len);
  360. pos++;
  361. }
  362. }
  363. table_cnt = pos;
  364. }
  365.  
  366. /* replace a given token in all the valid symbols. Use the sampled symbols
  367. * to update the counts */
  368. static void compress_symbols(unsigned char *str, int idx)
  369. {
  370. unsigned int i, len, size;
  371. unsigned char *p1, *p2;
  372.  
  373. for (i = 0; i < table_cnt; i++) {
  374.  
  375. len = table[i].len;
  376. p1 = table[i].sym;
  377.  
  378. /* find the token on the symbol */
  379. p2 = memmem(p1, len, str, 2);
  380. if (!p2) continue;
  381.  
  382. /* decrease the counts for this symbol’s tokens */
  383. forget_symbol(table[i].sym, len);
  384.  
  385. size = len;
  386.  
  387. do {
  388. *p2 = idx;
  389. p2++;
  390. size -= (p2 – p1);
  391. memmove(p2, p2 + 1, size);
  392. p1 = p2;
  393. len–;
  394.  
  395. if (size < 2) break;
  396.  
  397. /* find the token on the symbol */
  398. p2 = memmem(p1, size, str, 2);
  399.  
  400. } while (p2);
  401.  
  402. table[i].len = len;
  403.  
  404. /* increase the counts for this symbol’s new tokens */
  405. learn_symbol(table[i].sym, len);
  406. }
  407. }
  408.  
  409. /* search the token with the maximum profit */
  410. static int find_best_token(void)
  411. {
  412. int i, best, bestprofit;
  413.  
  414. bestprofit=-10000;
  415. best = 0;
  416.  
  417. for (i = 0; i < 0×10000; i++) {
  418. if (token_profit[i] > bestprofit) {
  419. best = i;
  420. bestprofit = token_profit[i];
  421. }
  422. }
  423. return best;
  424. }
  425.  
  426. /* this is the core of the algorithm: calculate the “best” table */
  427. static void optimize_result(void)
  428. {
  429. int i, best;
  430.  
  431. /* using the ” symbol last allows compress_symbols to use standard
  432. * fast string functions */
  433. for (i = 255; i >= 0; i–) {
  434.  
  435. /* if this table slot is empty (it is not used by an actual
  436. * original char code */
  437. if (!best_table_len[i]) {
  438.  
  439. /* find the token with the breates profit value */
  440. best = find_best_token();
  441.  
  442. /* place it in the “best” table */
  443. best_table_len[i] = 2;
  444. best_table[i][0] = best & 0xFF;
  445. best_table[i][1] = (best >> 8) & 0xFF;
  446.  
  447. /* replace this token in all the valid symbols */
  448. compress_symbols(best_table[i], i);
  449. }
  450. }
  451. }
  452.  
  453. /* start by placing the symbols that are actually used on the table */
  454. static void insert_real_symbols_in_table(void)
  455. {
  456. unsigned int i, j, c;
  457.  
  458. memset(best_table, 0, sizeof(best_table));
  459. memset(best_table_len, 0, sizeof(best_table_len));
  460.  
  461. for (i = 0; i < table_cnt; i++) {
  462. for (j = 0; j < table[i].len; j++) {
  463. c = table[i].sym[j];
  464. best_table[c][0]=c;
  465. best_table_len[c]=1;
  466. }
  467. }
  468. }
  469.  
  470. static void optimize_token_table(void)
  471. {
  472. build_initial_tok_table();
  473.  
  474. insert_real_symbols_in_table();
  475.  
  476. /* When valid symbol is not registered, exit to error */
  477. if (!table_cnt) {
  478. fprintf(stderr, “No valid symbol.\n”);
  479. exit(1);
  480. }
  481.  
  482. optimize_result();
  483. }
  484.  
  485. int main(int argc, char **argv)
  486. {
  487. if (argc >= 2) {
  488. int i;
  489. for (i = 1; i < argc; i++) {
  490. if(strcmp(argv[i], “–all-symbols”) == 0)
  491. all_symbols = 1;
  492. else if (strncmp(argv[i], “–symbol-prefix=, 16) == 0) {
  493. char *p = &argv[i][16];
  494. /* skip quote */
  495. if ((*p == ‘”‘ && *(p+2) == ‘”‘) || (*p == ‘\” && *(p+2) == ‘\”))
  496. p++;
  497. symbol_prefix_char = *p;
  498. } else
  499. usage();
  500. }
  501. } else if (argc != 1)
  502. usage();
  503.  
  504. read_map(stdin);
  505. optimize_token_table();
  506. write_src();
  507.  
  508. return 0;
  509. }
  510.  
  511. ###################################################################################################
  512.  
  513.             لمزيد من المعلومات او لمزيد من دروس القرصنه    
  514.        
  515.                     تابعونا علي
  516.  
  517.                 https://www.facebook.com/egyshell
  518.                 http://WwW.EgyptianShell.com/
  519.  
  520.                      ~\ Dr.FarFar & Mr.MaGMA & Th3 Mast3r /~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement