iOS 應用關於彌補安全優化問題

1.log輸出會被中獎者截獲,暴露信息,影響app得性能前端

在工程裏面的pch文件加入如下代碼git

// 調試狀態github

#define LMLog(...) NSLog(__VA_ARGS__)安全

 

#else服務器

//  發佈狀態微信

#define LMLog(...)app

#endif /* PersonLife_pch */函數

#ifdef DEBUGpost

#define NSLog(...) NSLog(__VA_ARGS__)性能

#define debugMethod() NSLog(@"%s", __func__)

#else

#define NSLog(...)

#define debugMethod()

而後在工程裏面寫product---scheme,編輯成release

調試開發階段編輯成debug模式 進行調試開發

 

2.登陸請求最好用post請求,把用戶信息放在請求體裏面更加安全

若是是H5的登陸頁作登陸的,則須要後臺把前端用到的參數拼在get請求後面,在H5後面MD5加密在拼在get請求的後面的參數,更加安全

3.作代碼混淆

提升代碼的安全性,使代碼變得難讀,推薦使用ZMConfuse,在github上可搜索到

使用方法:在終端 cd + ZMConfus ,把混淆的工程拷貝到當前目錄下,根據需求修改.sh文件

再次打開工程,會報一些錯誤 ,修改pch的路徑就好,在終端拖入終端,點回車便可 執行腳本命令

再次打開工程,就出現混淆的代碼,對類,屬性,方法,函數進行混淆,是代碼徹底失去了可讀性。

(注意文件名和類的命名的規則,需注意如同樣找不到對應的錯誤,會報編譯錯誤,形成混淆錯誤)

4.使用新設備時須要進行驗證受權  ---如微信

不一樣設備重複登陸校驗問題 :第一次登陸帳號綁定設備uuid,用第二部手機時再次登陸同一帳號時,服務器首先比較uuid uuid 不一樣註銷當前掉當前的用戶 彈出alert 用手機驗證碼進行驗證,驗證成功綁定uuid,實現微信號一對多的存儲在服務端後臺中實現帳號登陸  以此類推,實現不一樣設備重複登陸校驗。

5.https的雙重驗證問題  須要後臺提供相關的證書進行認證便可

這裏是系統驗證的方法

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

     //直接驗證服務器是否被認證(serverTrust),這種方式直接忽略證書驗證,信任該connect

     SecTrustRef serverTrust = [[challenge protectionSpace] serverTrust];

     return [[challenge sender] useCredential: [NSURLCredential credentialForTrust: serverTrust]

     forAuthenticationChallenge: challenge];

    

    if ([[[challenge protectionSpace] authenticationMethod] isEqualToString: NSURLAuthenticationMethodServerTrust]) {

        do

        {

            SecTrustRef serverTrust = [[challenge protectionSpace] serverTrust];

            NSCAssert(serverTrust != nil, @"serverTrust is nil");

            if(nil == serverTrust)

                break; /* failed */

            NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"證書名稱" ofType:@"cer"];//自簽名證書

            NSData* caCert = [NSData dataWithContentsOfFile:cerPath];

            

            NSString *cerPath2 = [[NSBundle mainBundle] pathForResource:@"證書名稱" ofType:@"cer"];//SSL證書

            NSData * caCert2 = [NSData dataWithContentsOfFile:cerPath2];

            

            NSCAssert(caCert != nil, @"caCert is nil");

            if(nil == caCert)

                break; /* failed */

            

            NSCAssert(caCert2 != nil, @"caCert2 is nil");

            if (nil == caCert2) {

                break;

            }

            

            SecCertificateRef caRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)caCert);

            NSCAssert(caRef != nil, @"caRef is nil");

            if(nil == caRef)

                break; /* failed */

            

            SecCertificateRef caRef2 = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)caCert2);

            NSCAssert(caRef2 != nil, @"caRef2 is nil");

            if(nil == caRef2)

                break; /* failed */

            

            NSArray *caArray = @[(__bridge id)(caRef),(__bridge id)(caRef2)];

            

            NSCAssert(caArray != nil, @"caArray is nil");

            if(nil == caArray)

                break; /* failed */

            

            OSStatus status = SecTrustSetAnchorCertificates(serverTrust, (__bridge CFArrayRef)caArray);

            NSCAssert(errSecSuccess == status, @"SecTrustSetAnchorCertificates failed");

            if(!(errSecSuccess == status))

                break; /* failed */

            

            SecTrustResultType result = -1;

            status = SecTrustEvaluate(serverTrust, &result);

            if(!(errSecSuccess == status))

                break; /* failed */

            NSLog(@"stutas:%d",(int)status);

            NSLog(@"Result: %d", result);

            

            BOOL allowConnect = (result == kSecTrustResultUnspecified) || (result == kSecTrustResultProceed);

            if (allowConnect) {

                NSLog(@"success");

            }else {

                NSLog(@"error");

            }

            if(! allowConnect)

            {

                break; /* failed */

            }

            

#if 0

            /* Treat kSecTrustResultConfirm and kSecTrustResultRecoverableTrustFailure as success */

            /*   since the user will likely tap-through to see the dancing bunnies */

            if(result == kSecTrustResultDeny || result == kSecTrustResultFatalTrustFailure || result == kSecTrustResultOtherError)

                break; /* failed to trust cert (good in this case) */

#endif

            

            // The only good exit point

            NSLog(@"信任該證書");

            return [[challenge sender] useCredential: [NSURLCredential credentialForTrust: serverTrust]

                          forAuthenticationChallenge: challenge];

            

        }

        while(0);

    }

    

    // Bad dog

    return [[challenge sender] cancelAuthenticationChallenge: challenge];

    

}

 

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {

    

    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

}

 //目前APP檢測遇到這些問題,已解決 但願有所能對你幫助   共勉

相關文章
相關標籤/搜索