Class: Rex::Post::Meterpreter::Extensions::Stdapi::Sys::Thread

Inherits:
Thread
  • Object
show all
Includes:
ObjectAliasesContainer
Defined in:
lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb

Overview

This class implements the Rex::Post::Thread interface which wrappers a logical thread for a given process.

Instance Attribute Summary collapse

Attributes included from ObjectAliasesContainer

#aliases

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ObjectAliasesContainer

#dump_alias_tree, #initialize_aliases, #method_missing

Constructor Details

#initialize(process, handle, tid) ⇒ Thread

Initialize the thread instance.



33
34
35
36
37
38
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 33

def initialize(process, handle, tid)
  self.process = process
  self.handle  = handle
  self.tid     = tid
  ObjectSpace.define_finalizer( self, self.class.finalize(self.process.client, self.handle) )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rex::Post::Meterpreter::ObjectAliasesContainer

Instance Attribute Details

#handleObject

:nodoc:



174
175
176
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 174

def handle
  @handle
end

#processObject

:nodoc:



174
175
176
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 174

def process
  @process
end

#tidObject

:nodoc:



174
175
176
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 174

def tid
  @tid
end

Class Method Details

.close(client, handle) ⇒ Object

Closes the thread handle.



161
162
163
164
165
166
167
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 161

def self.close(client, handle)
  request = Packet.create_request('stdapi_sys_process_thread_close')
  request.add_tlv(TLV_TYPE_THREAD_HANDLE, handle)
  client.send_request(request, nil)
  handle = nil
  return true
end

.finalize(client, handle) ⇒ Object



40
41
42
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 40

def self.finalize(client,handle)
  proc { self.close(client,handle) }
end

Instance Method Details

#closeObject

Instance method



170
171
172
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 170

def close
  self.class.close(self.process.client, self.handle)
end

#pretty_regsObject

Formats the registers in a pretty way.



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 139

def pretty_regs
  regs = query_regs

  buf  = sprintf("eax=%.8x ebx=%.8x ecx=%.8x edx=%.8x esi=%.8x edi=%.8x\n",
                 regs['eax'], regs['ebx'], regs['ecx'], regs['edx'], regs['esi'], regs['edi'])
  buf += sprintf("eip=%.8x esp=%.8x ebp=%.8x\n",
                 regs['eip'], regs['esp'], regs['ebp'])
  buf += sprintf("cs=%.4x ss=%.4x ds=%.4x es=%.4x fs=%.4x gs=%.4x\n",
                 regs['cs'], regs['ss'], regs['ds'], regs['es'], regs['fs'], regs['gs'])

  return buf
end

#query_regsObject

Queries the register state of the thread.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 99

def query_regs
  request = Packet.create_request('stdapi_sys_process_thread_query_regs')
  regs    = {}

  request.add_tlv(TLV_TYPE_THREAD_HANDLE, handle)

  response = process.client.send_request(request)

  response.each(TLV_TYPE_REGISTER) { |reg|
    regs[reg.get_tlv_value(TLV_TYPE_REGISTER_NAME)] = reg.get_tlv_value(TLV_TYPE_REGISTER_VALUE_32)
  }

  return regs
end

#resumeObject

Resumes the thread’s execution.



66
67
68
69
70
71
72
73
74
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 66

def resume
  request = Packet.create_request('stdapi_sys_process_thread_resume')

  request.add_tlv(TLV_TYPE_THREAD_HANDLE, handle)

  process.client.send_request(request)

  return true
end

#set_regs(regs_hash) ⇒ Object

Sets the register state of the thread. The registers are supplied in the form of a hash.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 118

def set_regs(regs_hash)
  request = Packet.create_request('stdapi_sys_process_thread_set_regs')

  request.add_tlv(TLV_TYPE_THREAD_HANDLE, handle)

  # Add all of the register that we're setting
  regs_hash.each_key { |name|
    t = request.add_tlv(TLV_TYPE_REGISTER)

    t.add_tlv(TLV_TYPE_REGISTER_NAME, name)
    t.add_tlv(TLV_TYPE_REGISTER_VALUE_32, regs_hash[name])
  }

  process.client.send_request(request)

  return true
end

#suspendObject

Suspends the thread’s execution.



53
54
55
56
57
58
59
60
61
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 53

def suspend
  request = Packet.create_request('stdapi_sys_process_thread_suspend')

  request.add_tlv(TLV_TYPE_THREAD_HANDLE, handle)

  process.client.send_request(request)

  return true
end

#terminate(code) ⇒ Object

Terminates the thread’s execution.



79
80
81
82
83
84
85
86
87
88
# File 'lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb', line 79

def terminate(code)
  request = Packet.create_request('stdapi_sys_process_thread_terminate')

  request.add_tlv(TLV_TYPE_THREAD_HANDLE, handle)
  request.add_tlv(TLV_TYPE_EXIT_CODE, code)

  process.client.send_request(request)

  return true
end