android app开发者如何防止二次打包攻击?

来源:深圳沃龙软件开发公司     2020-10-10

1、新建SignCheck类

/**

 * @创建者 深圳沃龙信息技术有限公司

 * @创建时间 2020-10-10 下午 7:05

 * @描述 新建,用于 校验签名是否正确,防止二次打包

 */

public class SignCheck {

    private Context context;

    private String cer = null;

    private String realCer = null;

    private static final String TAG = "SignCheck";


    public SignCheck(Context context) {

        this.context = context;

        this.cer = getCertificateSHA1Fingerprint();

    }


    public SignCheck(Context context, String realCer) {

        this.context = context;

        this.realCer = realCer;

        this.cer = getCertificateSHA1Fingerprint();

    }


    public String getRealCer() {

        return realCer;

    }


    /**

     * 设置正确的签名

     *

     * @param realCer

     */

    public void setRealCer(String realCer) {

        this.realCer = realCer;

    }


    /**

     * 获取应用的签名

     *

     * @return

     */

    public String getCertificateSHA1Fingerprint() {

        //获取包管理器

        PackageManager pm = context.getPackageManager();


        //获取当前要获取 SHA1 值的包名,也可以用其他的包名,但需要注意,

        //在用其他包名的前提是,此方法传递的参数 Context 应该是对应包的上下文。

        String packageName = context.getPackageName();


        //返回包括在包中的签名信息

        int flags = PackageManager.GET_SIGNATURES;


        PackageInfo packageInfo = null;


        try {

            //获得包的所有内容信息类

            packageInfo = pm.getPackageInfo(packageName, flags);

        } catch (PackageManager.NameNotFoundException e) {

            e.printStackTrace();

        }


        //签名信息

        Signature[] signatures = packageInfo.signatures;

        byte[] cert = signatures[0].toByteArray();


        //将签名转换为字节数组流

        InputStream input = new ByteArrayInputStream(cert);


        //证书工厂类,这个类实现了出厂合格证算法的功能

        CertificateFactory cf = null;


        try {

            cf = CertificateFactory.getInstance("X509");

        } catch (Exception e) {

            e.printStackTrace();

        }


        //X509 证书,X.509 是一种非常通用的证书格式

        X509Certificate c = null;


        try {

            c = (X509Certificate) cf.generateCertificate(input);

        } catch (Exception e) {

            e.printStackTrace();

        }


        String hexString = null;


        try {

            //加密算法的类,这里的参数可以使 MD4,MD5 等加密算法

            MessageDigest md = MessageDigest.getInstance("SHA1");


            //获得公钥

            byte[] publicKey = md.digest(c.getEncoded());


            //字节到十六进制的格式转换

            hexString = byte2HexFormatted(publicKey);


        } catch (NoSuchAlgorithmException e1) {

            e1.printStackTrace();

        } catch (CertificateEncodingException e) {

            e.printStackTrace();

        }

        return hexString;

    }


    //这里是将获取到得编码进行16 进制转换

    private String byte2HexFormatted(byte[] arr) {


        StringBuilder str = new StringBuilder(arr.length * 2);


        for (int i = 0; i <arr.length; i++) {

            String h = Integer.toHexString(arr[i]);

            int l =h.length();

            if (l == 1)

                h = "0" + h;

            if (l > 2)

                h = h.substring(l - 2, l);

            str.append(h.toUpperCase());

            if (i < (arr.length - 1))

                str.append(':');

        }

        return str.toString();

    }


    /**

     * 检测签名是否正确

     * @return true 签名正常 false 签名不正常

     */

    public boolean check() {


        if (this.realCer != null) {

            cer = cer.trim();

            realCer = realCer.trim();

            if (this.cer.equals(this.realCer)) {

                return true;

            }

        }else {

            Log.e(TAG, "未给定真实的签名 SHA-1 值");

        }

        return false;

    }

}

2、在application程序入口,红色部分替换成自己的签名SHA1进行判断(不知道如何获取SHA1信息的,请参考 http://www.wooolong.com/blog/24.html)


 SignCheck signCheck = new SignCheck(this,"40:A0:D4:01:26:79:64:89:B3:E9:0A:CB:EB:21:D7:6D:29:EC:F3:36");

        if(signCheck.check()) {

            //TODO 签名正常

           // ToastUtil.show("签名正常");

        }else {

            //TODO 签名不正确

            ToastUtil.show("该程序存在风险,请前往官方渠道下载正版 app!");

            //延迟两秒跳转

            new Handler().postDelayed(new Runnable() {

                @Override

                public void run() {

                    android.os.Process.killProcess(android.os.Process.myPid());//结束本程序

                    System.exit(0);

                }

            }, 3000);

        }



本内容皆为 深圳沃龙软件开发公司 原创
如需转载,请注明文章出处和来源网址:http://www.wooolong.com/blog/25.html

全部评论

Jamieawame | 2024-06-16

《a href=HHtps://mebel-24.blogspot.com/》HHtps://mebel-24.blogspot.com/《/a》 - всем рекомендую эту компанию. Купить современную мебель в интернет магазине TM Mebel-24 в Киеве и Украине. Производство качественной нестандартной мебели для дома и офиса. Большой каталог готовой мебели на 15000 товаров. Наш опыт более 19 лет в мебельной промышленности!

Reply

barberkusa | 2024-06-16

HHtps://abacus-mirror.store/ - abacus market 《a href="HHtps://abacus-mirror.store/"》abacus darkweb shop《/a》 HHtps://abacus-mirror.store/

Reply

barberkusa | 2024-06-16

HHtps://abacus-mirror.store/ - abacus darkweb shop 《a href="HHtps://abacus-mirror.store/"》abacus link《/a》 HHtps://abacus-mirror.store/

Reply

barberkusa | 2024-06-16

HHtps://abacus-mirror.store/ - abacus darknet market 《a href="HHtps://abacus-mirror.store/"》abacus darknet market《/a》 HHtps://abacus-mirror.store/

Reply

barberkusa | 2024-06-17

HHtps://abacus-mirror.store/ - abacus darknet market 《a href="HHtps://abacus-mirror.store/"》abacus darknet shop《/a》 HHtps://abacus-mirror.store/

Reply

barberkusa | 2024-06-17

HHtps://abacus-mirror.store/ - abacus darknet shop 《a href="HHtps://abacus-mirror.store/"》abacus《/a》 HHtps://abacus-mirror.store/

Reply

Renate | 2024-06-17

Greetings! Very helpful advice in this particular post! It is the little changes that produce the greatest changes. Thanks a lot for sharing!

Reply

RichardReT | 2024-06-17

“Врумврум.рф” - сайт чтобы поиска и объемы продажи каров, сверху тот или иной юзеры смогут найти свежеиспеченные (а) также бывшие в употреблении автомашины через различных продавцов. Сайт тоже выдает рапорт о стоимостях, данных равным образом откликах что касается различных марках равно моделях автомобилей.

Reply

Bernd | 2024-06-17

Valuable info. Fortunate me I discovered your site by accident, and I'm surprised why this coincidence did not came about in advance! I bookmarked it.

Reply

sakoioygan | 2024-06-17

HHtps://abacus-official.com/ - abacus mirror link 《a href="HHtps://abacus-official.com/"》abacus mirror link《/a》 HHtps://abacus-official.com/

Reply

sakoioygan | 2024-06-17

HHtps://abacus-official.com/ - abacus market 《a href="HHtps://abacus-official.com/"》abacus deepweb market《/a》 HHtps://abacus-official.com/

Reply

sakoioygan | 2024-06-17

HHtps://abacus-official.com/ - abacus darknet market 《a href="HHtps://abacus-official.com/"》abacus darknet market《/a》 HHtps://abacus-official.com/

Reply

sakoioygan | 2024-06-17

HHtps://abacus-official.com/ - abacus deepweb market 《a href="HHtps://abacus-official.com/"》abacus darknet market《/a》 HHtps://abacus-official.com/

Reply

Celina | 2024-06-17

I was pretty pleased to find this web site. I want to to thank you for your time for this wonderful read!! I definitely savored every part of it and i also have you bookmarked to see new information on your blog.

Reply

Rudolph | 2024-06-17

What's up to every , since I am in fact keen of reading this weblog's post to be updated regularly. It contains pleasant information.

Reply

Elane | 2024-06-17

It's really a cool and helpful piece of info. I am happy that you just shared this helpful info with us. Please stay us up to date like this. Thanks for sharing.

Reply

Shanon | 2024-06-17

I'm curious to find out what blog platform you are utilizing? I'm experiencing some small security issues with my latest site and I would like to find something more safe. Do you have any suggestions?

Reply

igorroyrh | 2024-06-17

HHtps://abacus-shop.site/ - abacus market 《a href="HHtps://abacus-shop.site/"》abacus marketplace《/a》 HHtps://abacus-shop.site/

Reply

igorroyrh | 2024-06-17

HHtps://abacus-shop.site/ - abacus marketplace 《a href="HHtps://abacus-shop.site/"》abacus《/a》 HHtps://abacus-shop.site/

Reply

igorroyrh | 2024-06-17

HHtps://abacus-shop.site/ - abacus mirror link 《a href="HHtps://abacus-shop.site/"》abacus link《/a》 HHtps://abacus-shop.site/

Reply

igorroyrh | 2024-06-18

HHtps://abacus-shop.site/ - abacus link 《a href="HHtps://abacus-shop.site/"》abacus darknet market《/a》 HHtps://abacus-shop.site/

Reply

igorroyrh | 2024-06-18

HHtps://abacus-shop.site/ - abacus deepweb market 《a href="HHtps://abacus-shop.site/"》abacus deepweb market《/a》 HHtps://abacus-shop.site/

Reply

RobertTycl | 2024-06-18

HHtps://my-viagra-shop.ru/vliyanie-kofe-na-potenciyu.html

Reply

paveljytma | 2024-06-18

HHtps://official-abacus.com/ - abacus darknet shop 《a href="HHtps://official-abacus.com/"》abacus darknet market《/a》 HHtps://official-abacus.com/

Reply

paveljytma | 2024-06-18

HHtps://official-abacus.com/ - abacus mirror link 《a href="HHtps://official-abacus.com/"》abacus mirror link《/a》 HHtps://official-abacus.com/

Reply

paveljytma | 2024-06-18

HHtps://official-abacus.com/ - abacus link 《a href="HHtps://official-abacus.com/"》abacus deepweb market《/a》 HHtps://official-abacus.com/

Reply

paveljytma | 2024-06-18

HHtps://official-abacus.com/ - abacus marketplace 《a href="HHtps://official-abacus.com/"》abacus darkweb shop《/a》 HHtps://official-abacus.com/

Reply

paveljytma | 2024-06-18

HHtps://official-abacus.com/ - abacus darkweb shop 《a href="HHtps://official-abacus.com/"》abacus darknet shop《/a》 HHtps://official-abacus.com/

Reply

Kelsey | 2024-06-18

Greetings from Los angeles! I'm bored at work so I decided to browse your site on my iphone during lunch break. I enjoy the information you provide here and can't wait to take a look when I get home. I'm amazed at how fast your blog loaded on my phone .. I'm not even using WIFI, just 3G .. Anyhow, fantastic site!

Reply

optikujba | 2024-06-18

HHtps://darknet-abacus.com/ - abacus link 《a href="HHtps://darknet-abacus.com/"》abacus《/a》 HHtps://darknet-abacus.com/

Reply

optikujba | 2024-06-18

HHtps://darknet-abacus.com/ - abacus mirror link 《a href="HHtps://darknet-abacus.com/"》abacus darkweb shop《/a》 HHtps://darknet-abacus.com/

Reply

optikujba | 2024-06-18

HHtps://darknet-abacus.com/ - abacus deepweb market 《a href="HHtps://darknet-abacus.com/"》abacus darknet market《/a》 HHtps://darknet-abacus.com/

Reply

optikujba | 2024-06-19

HHtps://darknet-abacus.com/ - abacus darknet shop 《a href="HHtps://darknet-abacus.com/"》abacus deepweb market《/a》 HHtps://darknet-abacus.com/

Reply

Marla | 2024-06-19

You can shoot them an e-mail at anytime you have got a query or want a hand with something.

Reply

Chase | 2024-06-19

Very good post. I am facing some of these issues as well..

Reply

machetyjka | 2024-06-19

HHtps://www.yoga-books.ru/ - книги по кундалини йоге 《a href="HHtps://www.yoga-books.ru/"》kundalini yoga books《/a》 HHtps://www.yoga-books.ru/

Reply

machetyjka | 2024-06-19

HHtps://www.yoga-books.ru/ - книги по кундалини йоге 《a href="HHtps://www.yoga-books.ru/"》kundalini yoga books《/a》 HHtps://www.yoga-books.ru/

Reply

machetyjka | 2024-06-19

HHtps://www.yoga-books.ru/ - kundalini yoga books 《a href="HHtps://www.yoga-books.ru/"》kundalini yoga books《/a》 HHtps://www.yoga-books.ru/

Reply

machetyjka | 2024-06-19

HHtps://www.yoga-books.ru/ - kundalini yoga books 《a href="HHtps://www.yoga-books.ru/"》kundalini yoga books《/a》 HHtps://www.yoga-books.ru/

Reply

machetyjka | 2024-06-19

HHtps://www.yoga-books.ru/ - книги по кундалини йоге 《a href="HHtps://www.yoga-books.ru/"》kundalini yoga books《/a》 HHtps://www.yoga-books.ru/

Reply

Lucienne | 2024-06-19

This post is priceless. How can I find out more?

Reply

Dorthea | 2024-06-19

When some one searches for his required thing, thus he/she wants to be available that in detail, so that thing is maintained over here.

Reply

Eulalia | 2024-06-19

KONVERTIEREN YOUTUBE VIDEOS MIT ovc Online-Videokonverter online Video- Online-Videokonverter HHtps://de.onlinevideoconverter.pro/youtube-converter-mp3 OVC - ONLINE VIDEOCLIP DOWNLOADER Wir bieten eine Bereich von Methoden an transform und herunterladen im Internet Videos für den Offline- Barrierefreiheit. Sie können speichern Videos sowie sie sich auf Ihrem mobilen Gerät oder Computer leicht, auch während unterwegs! Nur kopieren oder GIF Links von YouTube, Facebook,

Reply

gretybvaf | 2024-06-19

HHtps://saffelychange.com/ - Обмен бтк на сбер netex24 《a href="HHtps://saffelychange.com/"》 нетекс24《/a》 HHtps://saffelychange.com/

Reply

gretybvaf | 2024-06-19

HHtps://saffelychange.com/ - Обменник netex24 《a href="HHtps://saffelychange.com/"》 safelychange нетекс24《/a》 HHtps://saffelychange.com/

Reply

gretybvaf | 2024-06-19

HHtps://saffelychange.com/ - netex24 《a href="HHtps://saffelychange.com/"》 netex24《/a》 HHtps://saffelychange.com/

Reply

gretybvaf | 2024-06-20

HHtps://saffelychange.com/ - safelychange netex24 《a href="HHtps://saffelychange.com/"》 обменять криптовалюту《/a》 HHtps://saffelychange.com/

Reply

gretybvaf | 2024-06-20

HHtps://saffelychange.com/ - купить usdt 《a href="HHtps://saffelychange.com/"》 netex24 обменник《/a》 HHtps://saffelychange.com/

Reply

Lula | 2024-06-20

I blog often and I seriously thank you for your information. This article has truly peaked my interest. I will bookmark your website and keep checking for new details about once a week. I subscribed to your Feed as well.

Reply

发表评论