Giter Site home page Giter Site logo

tanghuaizhe / safetynethelper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from scottyab/safetynethelper

0.0 1.0 0.0 1.38 MB

SafetyNet Helper wraps the Google Play Services SafetyNet.API and verifies Safety Net API response with the Android Device Verification API.

Java 100.00%

safetynethelper's Introduction

SafetyNet attest() Helper

SafetyNet Helper wraps the Google Play Services SafetyNet.API and verifies Safety Net API response with the Android Device Verification API. The SafetyNet.API analyses the device your app is running on to test its software/hardware configuration matches that of a device that has passed the Android Compatibility Test Suite (CTS). Note this is a client only validation, it's recommended to include server side validation.

Rooted devices seem to cause ctsProfileMatch=false.

Recommend reading the developers guide to getting started with SafetyNet

Extract from Android SafetyNet API doc

Check if your app is running on a device that matches a device model that has passed Android compatibility testing. This analysis can help you determine if your app will work as expected on the device where it is installed. The service evaluates both software and hardware characteristics of the device, and may use hardware roots of trust, when available.

*Since this library release Google has created an Safety Net Sample

Features

  • Calls Google play services Safety Net test
  • Local verification of request
  • Verifies Safety Net API response with the Android Device Verification API (over SSL pinned connection)

Requires / Dependencies

  • Google Play services (specifically the SafetyNet API 'com.google.android.gms:play-services-safetynet:17.0.0')
  • Requires Internet permission
  • Google API key for the Android Device Verification API

Server Validation!!!

This library was built to get app developers up and going with SafetyNet attest API. With skill and time any device based checks can be bypassed. This is why the validation should be handled by the server. Therefore you should look at implementing more robust and secure validation of the attest response via a server-side component.

  • App requests the nonce / request token from your server
  • Call `SafetyNet.SafetyNetApi.attest(googleApiClient, requestNonce)
  • Pass the JWT response from SafetyNet API to your server for validation (the same validation check which this library completes)
    • Check the nonce/request token matches the expected value
    • verify the SafetyNet response is from Google using the Android Device Verification API
    • verify app package, timestamp, apk and certificate digests

If verification fails then at least your server would know if the device (and app installation) was compromised and take appropriate action e.g revoking OAUTH tokens.

How to use

You'll need to get a API key from the Google developer console to allow you to verify with the Android Device Verification API (in the sample project this is set via a BuildConfig field to keep my api key out of GitHub)

    final SafetyNetHelper safetyNetHelper = new SafetyNetHelper(API_KEY);

    safetyNetHelper.requestTest(context, new SafetyNetHelper.SafetyNetWrapperCallback() {
            @Override
            public void error(int errorCode, String msg) {
                //handle and retry depending on errorCode
                Log.e(TAG, msg);
            }

            @Override
            public void success(boolean ctsProfileMatch, boolean basicIntegrity) {
                if (ctsProfileMatch) {
                    //profile of the device running your app matches the profile of a device that has passed Android compatibility testing.
                else if(basicIntegrity){
                    //then the device running your app likely wasn't tampered with, but the device has not necessarily passed Android compatibility testing.
                } else {
                    //handle fail, maybe warn user device is unsupported or in compromised state? (this is up to you!)
                }
            }
        });

Add as dependency

This library is not released in Maven Central, until then you can add as a library module or use JitPack.io

add remote maven url

    repositories {
        maven {
            url "https://jitpack.io"
        }
    }

then add a library dependency

    dependencies {
        compile 'com.github.scottyab:safetynethelper:0.8'
    }

Sample App

The sample app illustrates the helper library in practice. Test your own devices today. It's available on the playstore.


Licence

Copyright (c) 2021 Scott Alexander-Bown

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

safetynethelper's People

Contributors

aembleton avatar andrewjli avatar aymandf avatar chaoscalm avatar friederbluemle avatar luk1337 avatar mirokuratczyk avatar mpp-doric avatar robsmall avatar scottyab avatar tanghuaizhe 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.