Code Monkey home page Code Monkey logo

btlejuice-python-bindings's Introduction

BtleJuice bindings

BtleJuice provides bindings for both Python and NodeJS. These bindings require the BtleJuice Core to be ran in background in order to work properly.

These bindings provide two interface classes for sniffing and hooking, and an application class.

See https://github.com/DigitalSecurity/btlejuice for more details about BtleJuice.

Importing the required classes

The btlejuice module provides all the required application and interface classes, and should be imported as follows:

from btlejuice import BtleJuiceApp, SniffingInterface, HookingInterface

Creating a sniffing interface

The SniffingInterface provides all the required methods to perform sniffing. This class should be instanciated with BtleJuice's server IP and port, as demonstrated below:

class MySniffingInterface(SniffingInterface):
    """
    Specific sniffing class.
    """
    def __init__(self, host, port, target):
        SniffingInterface.__init__(self, host, port, target)

The following methods are called when specific events occur:

  • on_data_read(self, service, characteristic, data): called once a read operation has just been performed
  • on_data_write(self, service, characteristic, data, offset, withoutResponse, error): called when a write operation has just been performed
  • on_subscribe_notification(self, service, characteristic, enabled): called when a characteristic has just been (un)subscribed for notification
  • on_notification_data(self, service, characteristic, data): called when a notification is received from the target device

Creating a hooking interface

The HookingInterface provides all the required methods to perform on-the-fly data manipulation. This class should be instanciated with BtleJuice's server IP and port, as demonstrated below:

class MyHookingInterface(HookingInterface):
    def __init__(self, host, port, target):
        HookingInterface.__init__(self, host, port, target)

The following methods are called when specific events occur:

  • on_before_read(self, service, characteristic, offset): called before each read operation
  • on_after_read(self, service, characteristic, data): called once the read operation has been performed
  • on_before_write(self, service, characteristic, data, offset, withoutResponse): called before each write operation
  • on_before_subscribe(self, service, characteristic, enabled): called before each notification subscription
  • on_before_notification(self, service, characteristic, data): called before each data notification

It is possible to dismiss an operation or to alter its behavior thanks to two exception classes:

  • HookForceResponse: used to dismiss an operation and force the return value
  • HookModify: used to modify the parameters before forwarding to the target device or the central device.

In the following example, we modify the battery service's behavior in order to decrease the battery level each time this level is read:

class MyHookingInterface(HookingInterface):
    def __init__(self, host, port, target):
        HookingInterface.__init__(self, host, port, target)
        self.batt_level = 10

        def on_proxy_setup(self):
            print('[i] Target found, setting up proxy ...')

        def proxy_ready(self):
            print('[i] Proxy ready !')

        def on_before_read(self, service, characteristic, offset):
            if service.lower() == '180f' and characteristic.lower()=='2a19':
                self.batt_level -= 1
                if self.batt_level < 0:
                    self.batt_level = 100
                raise HookForceResponse(chr(self.batt_level))

Communicating with the target device

BtleJuice bindings also provide all the required methods to communicate with the target device:

  • device_read(self, service, characteristic): asks the BtleJuice proxy to read a specific characteristic on the target device
  • device_write(self, service, characteristic, data, offset=0, withoutResponse=False): asks the BtleJuice proxy to write some data to a specific characteristic on the target device
  • device_notify(self, service, characteristic, enabled): asks the BtleJuice proxy to subscribe for notification for a given characteristic

Creating a BtleJuice based App

Once your interface defined, you may use it through a BtleJuiceApp instance, as shown below:

import threading

[...]

try:
    app = BtleJuiceApp(
      MyHookingInterface(args.server, args.port, args.target)
    )
    app.setDaemon(True)
    app.start()
    while threading.active_count() > 0:
        time.sleep(0.1)
except KeyboardInterrupt:
    print('[i] Stopping ...')
    app.cancel()

Both BtleJuice core and proxy should run before this script is launched to work properly.

btlejuice-python-bindings's People

Contributors

virtualabs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.