|
| 1 | +# -*- coding => binary -*- |
| 2 | + |
| 3 | +require 'msf/core/module/platform' |
| 4 | +require 'rex/constants' |
| 5 | +require 'rex/text' |
| 6 | + |
| 7 | +# |
| 8 | +# This module provides methods for calculating, extracting, and parsing |
| 9 | +# unique ID values used by payloads. |
| 10 | +# |
| 11 | +module Msf::Payload::UUID |
| 12 | + |
| 13 | + Architectures = { |
| 14 | + 0 => nil, |
| 15 | + 1 => ARCH_X86, |
| 16 | + 2 => ARCH_X86_64, |
| 17 | + 3 => ARCH_MIPS, |
| 18 | + 4 => ARCH_MIPSLE, |
| 19 | + 5 => ARCH_MIPSBE, |
| 20 | + 6 => ARCH_PPC, |
| 21 | + 7 => ARCH_PPC64, |
| 22 | + 8 => ARCH_CBEA, |
| 23 | + 9 => ARCH_CBEA64, |
| 24 | + 10 => ARCH_SPARC, |
| 25 | + 11 => ARCH_ARMLE, |
| 26 | + 12 => ARCH_ARMBE, |
| 27 | + 13 => ARCH_CMD, |
| 28 | + 14 => ARCH_PHP, |
| 29 | + 15 => ARCH_TTY, |
| 30 | + 16 => ARCH_JAVA, |
| 31 | + 17 => ARCH_RUBY, |
| 32 | + 18 => ARCH_DALVIK, |
| 33 | + 19 => ARCH_PYTHON, |
| 34 | + 20 => ARCH_NODEJS, |
| 35 | + 21 => ARCH_FIREFOX |
| 36 | + } |
| 37 | + |
| 38 | + Platforms = { |
| 39 | + 0 => nil, |
| 40 | + 1 => 'windows', |
| 41 | + 2 => 'netware', |
| 42 | + 3 => 'android', |
| 43 | + 4 => 'java', |
| 44 | + 5 => 'ruby', |
| 45 | + 6 => 'linux', |
| 46 | + 7 => 'cisco', |
| 47 | + 8 => 'solaris', |
| 48 | + 9 => 'osx', |
| 49 | + 10 => 'bsd', |
| 50 | + 11 => 'openbsd', |
| 51 | + 12 => 'bsdi', |
| 52 | + 13 => 'netbsd', |
| 53 | + 14 => 'freebsd', |
| 54 | + 15 => 'aix', |
| 55 | + 16 => 'hpux', |
| 56 | + 17 => 'irix', |
| 57 | + 18 => 'unix', |
| 58 | + 19 => 'php', |
| 59 | + 20 => 'js', |
| 60 | + 21 => 'python', |
| 61 | + 22 => 'nodejs', |
| 62 | + 23 => 'firefox' |
| 63 | + } |
| 64 | + |
| 65 | + # |
| 66 | + # Generate a raw 12-byte payload UUID given a seed, platform, and architecture |
| 67 | + # |
| 68 | + # @options opts [String] :seed A optional string to use for generated the unique payload ID, deterministic |
| 69 | + # @options opts [String] :arch The hardware architecture for this payload |
| 70 | + # @options opts [String] :platform The operating system platform for this payload |
| 71 | + # |
| 72 | + def self.payload_uuid_generate_raw(opts={}) |
| 73 | + plat_id = opts[:platform] ? find_platform_id(opts[:platform]) : 0 |
| 74 | + arch_id = opts[:arch] ? find_architecture_id(opts[:arch]) : 0 |
| 75 | + seed = opts[:seed] || Rex::Text.rand_text(16) |
| 76 | + |
| 77 | + plat_xor = rand(255) |
| 78 | + arch_xor = rand(255) |
| 79 | + |
| 80 | + # Combine the last 64-bits of the SHA1 of seed with the arch/platform |
| 81 | + # Use XOR to obscure the platform and architecture IDs |
| 82 | + Rex::Text.sha1_raw(seed)[12,8] + |
| 83 | + [ |
| 84 | + plat_xor, arch_xor, |
| 85 | + plat_xor ^ plat_id, |
| 86 | + arch_xor ^ arch_id |
| 87 | + ].pack('C*') |
| 88 | + end |
| 89 | + |
| 90 | + # |
| 91 | + # Parse a raw 12-byte payload UUID and return the payload ID, platform, and architecture |
| 92 | + # |
| 93 | + # @param raw [String] The raw 12-byte payload UUID to parse |
| 94 | + # @return [Array] The Payload ID, platform, and architecture |
| 95 | + # |
| 96 | + def self.payload_uuid_parse_raw(raw) |
| 97 | + puid, plat_xor, arch_xor, plat_id, arch_id = raw.unpack('A8C4') |
| 98 | + plat = find_platform_name(plat_xor ^ plat_id) |
| 99 | + arch = find_architecture_name(arch_xor ^ arch_id) |
| 100 | + [puid, plat, arch] |
| 101 | + end |
| 102 | + |
| 103 | + # Alias for the class method |
| 104 | + def payload_uuid_generate_raw(opts) |
| 105 | + self.class.payload_uuid_generate_raw(opts) |
| 106 | + end |
| 107 | + |
| 108 | + # Alias for the class method |
| 109 | + def parse_payload_uuid_raw(raw) |
| 110 | + self.class.payload_uuid_parse_raw(raw) |
| 111 | + end |
| 112 | + |
| 113 | + def self.find_platform_id(platform) |
| 114 | + # Handle a PlatformList input by grabbing the first entry |
| 115 | + if platform.respond_to? :platforms |
| 116 | + platform = platform.platforms.first.realname.downcase |
| 117 | + end |
| 118 | + |
| 119 | + # Map a platform abbreviation to the real name |
| 120 | + name = Msf::Platform::Abbrev[platform] |
| 121 | + |
| 122 | + Platforms.keys.select{ |k| |
| 123 | + Platforms[k] == name |
| 124 | + }.first || Platforms[0] |
| 125 | + end |
| 126 | + |
| 127 | + def self.find_architecture_id(name) |
| 128 | + Architectures.keys.select{ |k| |
| 129 | + Architectures[k] == name |
| 130 | + }.first || Architectures[0] |
| 131 | + end |
| 132 | + |
| 133 | + def self.find_platform_name(num) |
| 134 | + Platforms[num] |
| 135 | + end |
| 136 | + |
| 137 | + def self.find_architecture_name(num) |
| 138 | + Architectures[num] |
| 139 | + end |
| 140 | + |
| 141 | +end |
0 commit comments