iOS多點串連的使用、協議逆向、安全性
什麼是多點串連?
多點串連簡單說就是將裝置兩兩進行串連,從而組成一個網路,見:
多點串連可以基於如下兩種通道建立:
即:藍芽與WiFi, 且“只具有藍芽的裝置”可以與“只具有WiF的裝置”通訊, 這一切都是透明的,開發人員根本不需要關心:
個人感覺它的能力還是比較強大的。 既然能力這麼強大,它可以用來做什麼呢? MC只是提供了一種資料通道,具體用途還是要看業務、看大家的想象力, 下面列幾個比較常見的用途: 傳檔案聊天室一台裝置作為資料擷取外設(比如:網路攝影機),將即時資料導到另一台裝置上網路資料轉寄...
多點串連 API 的使用 SDK及版本資訊
MultipeerConnectivity.frameworkiOS 7.0OS X 10.10
可以看到基於MC可以做到電腦與手機的通訊。 瞭解了其能力與SDK相關資訊後,下面我們看看工作流程: 使裝置可被發現--->瀏覽裝置,建立串連--->傳輸資料 。 關於使用大家可以看看參考資源與 MCDemo, 這裡只是做一個代碼導讀。
1、初始化 MCPeerID 及 MCSession, MCPeerID 用來唯一的標識裝置, MCSession 是通訊的基礎:
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName{ _peerID = [[MCPeerID alloc] initWithDisplayName:displayName]; _session = [[MCSession alloc] initWithPeer:_peerID]; _session.delegate = self;}
2、廣播裝置,使裝置可以被發現:
-(void)advertiseSelf:(BOOL)shouldAdvertise{ if (shouldAdvertise) { _advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@chat-files discoveryInfo:nil session:_session]; [_advertiser start]; } else{ [_advertiser stop]; _advertiser = nil; }}
3、瀏覽“區域網路”中的裝置,並建立串連:
-(void)setupMCBrowser{ _browser = [[MCBrowserViewController alloc] initWithServiceType:@chat-files session:_session];}
MCBrowserViewController執行個體化後,直接彈出,這個類內部會負責尋找裝置並建立串連。 對於有介面定製化需求的,也可以通過相關介面實作類別似的功能。
4、發送訊息:
-(void)sendMyMessage{ NSData *dataToSend = [_txtMessage.text dataUsingEncoding:NSUTF8StringEncoding]; NSArray *allPeers = _appDelegate.mcManager.session.connectedPeers; NSError *error; [_appDelegate.mcManager.session sendData:dataToSend toPeers:allPeers withMode:MCSessionSendDataReliable error:&error]; if (error) { NSLog(@%@, [error localizedDescription]); } [_tvChat setText:[_tvChat.text stringByAppendingString:[NSString stringWithFormat:@I wrote:%@, _txtMessage.text]]]; [_txtMessage setText:@]; [_txtMessage resignFirstResponder];}
發送訊息時有個選項:MCSessionSendDataReliable,MCSessionSendDataUnreliable 但是不管是可靠還是不可靠,資料都是基於 UDP 進行傳輸的。
5、接收訊息:
-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{ NSDictionary *dict = @{@data: data, @peerID: peerID }; [[NSNotificationCenter defaultCenter] postNotificationName:@MCDidReceiveDataNotification object:nil userInfo:dict];}
訊息的接收是通過 MCSession 的回調方法進行的。 MCSession的回調方法非常重要, 裝置狀態的改變、訊息的接收、資源的接收、流的接收都是通過這個回調進行通知的。
6、發送資源,資源可以是本地的URL,也可以是 Http 連結:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex != [[_appDelegate.mcManager.session connectedPeers] count]) { NSString *filePath = [_documentsDirectory stringByAppendingPathComponent:_selectedFile]; NSString *modifiedName = [NSString stringWithFormat:@%@_%@, _appDelegate.mcManager.peerID.displayName, _selectedFile]; NSURL *resourceURL = [NSURL fileURLWithPath:filePath]; dispatch_async(dispatch_get_main_queue(), ^{ NSProgress *progress = [_appDelegate.mcManager.session sendResourceAtURL:resourceURL withName:modifiedName toPeer:[[_appDelegate.mcManager.session connectedPeers] objectAtIndex:buttonIndex] withCompletionHandler:^(NSError *error) { if (error) { NSLog(@Error: %@, [error localizedDescription]); } else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@MCDemo message:@File was successfully sent. delegate:self cancelButtonTitle:nil otherButtonTitles:@Great!, nil]; [alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO]; [_arrFiles replaceObjectAtIndex:_selectedRow withObject:_selectedFile]; [_tblFiles performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; } }]; //NSLog(@*** %f, progress.fractionCompleted); [progress addObserver:self forKeyPath:@fractionCompleted options:NSKeyValueObservingOptionNew context:nil]; }); }}
可以通過 NSProgress查詢相關狀態。
7、接收資源:
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress{ NSDictionary *dict = @{@resourceName : resourceName, @peerID : peerID, @progress : progress }; [[NSNotificationCenter defaultCenter] postNotificationName:@MCDidStartReceivingResourceNotification object:nil userInfo:dict]; dispatch_async(dispatch_get_main_queue(), ^{ [progress addObserver:self forKeyPath:@fractionCompleted options:NSKeyValueObservingOptionNew context:nil]; });}
協議逆向
協議分析時,我們是基於WiFi進行分析,因為這樣便於抓包。 抓到的資料包如:
可以看到主要是基於如下幾個協議:
Bonjour在法語中是 Hello 的意思,即:主要用來做服務發現。 STUN主要用來做連接埠映射,便於兩台裝置直接建立串連。 剩下的兩個協議未知:一個基於TCP,一個基於UDP。 基於 TCP 的,我們看下TCP Stream:
注意中紅框部分:
這是某種握手機制,首先是交換裝置ID,然後會基於Binary Plist 交換資訊。 首先提取plist,提取plist時要參考 tcp stream 中的起始位元組與結束位元組, 將 plist 提出來後, 會看到一共交換了三個plist:
plist-1:
MCNearbyServiceInviteIDKey:MCEncryptionOption—>1, MCEncryptionNone—>0;
MCNearbyServiceMessageIDKey:序號
MCNearbyServiceRecipientPeerIDKey:接收者的PeerID
MCNearbyServiceSenderPeerIDKey:寄件者的PeerID
plist-2:
MCNearbyServiceAcceptInviteKey:是否接收串連
MCNearbyServiceConnectionDataKey
plist-3:
MCNearbyServiceConnectionDataKey
如上只是說了plist的內容, 但是在 tcp stream 中我們還看到了裝置ID, 裝置ID是如何產生的呢? 通過代碼逆向可以得到一個大概的結論: 裝置ID在 -[MCPeerIDInternal initWithIDString:pid64:displayName:] 中實現, 基本策略是: IDString: 隨機,base36pid64:隨機displayName:外部傳入,如:”Proteas-iPhone5s” 裝置間交換ID時需要進行序列化, 序列化的方法為:-[MCPeerID serializedRepresentation] 總結起來就是:PeerID = 基於pid64產生前 9 byte + displayName 附反編譯結果:
void * -[MCPeerID initWithDisplayName:](void * self, void * _cmd, void * arg2) { STK33 = r5; STK35 = r7; sp = sp - 0x28; r5 = arg2; arg_20 = self; arg_24 = *0x568f0; r6 = [[&arg_20 super] init]; if (r6 != 0x0) { if ((r5 == 0x0) || ([r5 length] == 0x0)) { r0 = [r6 class]; r0 = NSStringFromClass(r0); var_0 = r0; [NSException raise:*_NSInvalidArgumentException format:@Invalid displayName passed to %@]; } else { if ([r5 lengthOfBytesUsingEncoding:0x4] >= 0x40) { r0 = [r6 class]; r0 = NSStringFromClass(r0); var_0 = r0; [NSException raise:*_NSInvalidArgumentException format:@Invalid displayName passed to %@]; } } arg_8 = r6; arg_C = r5; r8 = CFUUIDCreate(*_kCFAllocatorDefault); CFUUIDGetUUIDBytes(&arg_10); r11 = (arg_1C ^ arg_14) << 0x18 | (arg_1C ^ arg_14) & 0xff00 | 0xff00 & (arg_1C ^ arg_14) | arg_1C ^ arg_14; r10 = 0xff00 & (arg_10 ^ arg_18) | ((arg_10 ^ arg_18) & 0xff00) << 0x8 | arg_10 ^ arg_18 | arg_10 ^ arg_18; r5 = _makebase36string(r11, r10); if (*_gVRTraceErrorLogLevel < 0x6) { asm{ strd r4, r5, [sp] }; VRTracePrint_(); } else { if (*(int8_t *)_gVRTraceModuleFilterEnabled != 0x0) { asm{ strd r4, r5, [sp] }; VRTracePrint_(); } } r4 = [NSString stringWithUTF8String:r5]; free(r5); CFRelease(r8); r0 = [MCPeerIDInternal alloc]; var_0 = r10; arg_4 = arg_C; r0 = [r0 initWithIDString:r4 pid64:r11 displayName:STK-1]; r6 = arg_8; r6->_internal = r0; } r0 = r6; Pop(); Pop(); Pop(); return r0;}[[MCPeerIDInternal alloc] initWithIDString:_makebase36string(...) pid64:r11 displayName:STK-1]
前面的 plist 中有 Data Key,我們沒有做過多說明, 接下來我們大概看看 Data Key 的產生:
在初始化一個多點串連的 Session 時,我們可以指定加密方式, 這個加密方式是個枚舉類型: MCEncryptionOptional = 0
MCEncryptionRequired = 1
MCEncryptionNone = 2
從可以看出加密方式會影響Data Key, 但是完全通過抓包來分析 Data Key 是比較耗時的, 而且很可能會有遺漏。 通過代碼逆向,我們找到負責 Data Key 產生的類:
這裡可以作為分析 Data Key 的起點, 有需要的兄弟可以進行深入分析。
上面我們都是在說基於 TCP 的未知協議, 接下來我們看看基於 UDP 的未知協議。 UDP資料流:
具體一個UDP資料包:
可以看出它是在 DTLS 之上做了封裝, 我們只要拋棄到 0xd0 就可以讓 Wireshark 進行識別分析。 這裡需要說下 BH-US 大會上沒有公布具體的工具與方法, 我處理的方法是寫一個 Custom Protocol Dissector:
-- Apple Mutipeer Connectivity Custom DTLS Protocl-- cache globals to local for speed.local format = string.formatlocal tostring = tostringlocal tonumber = tonumberlocal sqrt = math.sqrtlocal pairs = pairs-- wireshark API globalslocal Pref = Preflocal Proto = Protolocal ProtoField = ProtoFieldlocal DissectorTable = DissectorTablelocal Dissector = Dissectorlocal ByteArray = ByteArraylocal PI_MALFORMED = PI_MALFORMEDlocal PI_ERROR = PI_ERROR-- dissectorslocal dtls_dissector = Dissector.get(dtls)apple_mcdtls_proto = Proto(apple_mcDTLS, Apple Multipeer Connectivity DTLS, Apple Multipeer Connectivity DTLS Protocol)function apple_mcdtls_proto.dissector(buffer, pinfo, tree) local mctype = buffer(0, 1):uint() if mctype == 208 then pinfo.cols.protocol = AppleMCDTLS pinfo.cols.info = Apple MC DTLS Payload Data local subtree = tree:add(apple_mcdtls_proto, buffer(), Apple MC DTLS Protocol) subtree:add(buffer(0, 1),Type: .. buffer(0, 1):uint()) local size = buffer:len() subtree:add(buffer(1, size - 1), Data: .. tostring(buffer)) dtls_dissector:call(buffer(1):tvb(), pinfo, tree) endendlocal function unregister_udp_port_range(start_port, end_port)if not start_port or start_port <= 0 or not end_port or end_port <= 0 thenreturnend udp_port_table = DissectorTable.get(udp.port) for port = start_port,end_port do udp_port_table:remove(port, apple_mcdtls_proto) endend local function register_udp_port_range(start_port, end_port)if not start_port or start_port <= 0 or not end_port or end_port <= 0 thenreturnendudp_port_table = DissectorTable.get(udp.port)for port = start_port,end_port doudp_port_table:add(port, apple_mcdtls_proto)endendregister_udp_port_range(16400, 16499)
在 Wireshark 中使用自訂協議進行處理後:
這裡識別出協議後,我們不做繼續分析, 但是評估安全性時,比如在手機上 kill 調 ssl 後, 可以在 DTLS 的 Payload 中看到明文資料。
安全性分析
前文中也提到了,安全性的控制是在初始化 MCSession 時控制的, 預設是使用 MCEncryptionOptional, 但是當有一方是 MCEncryptionNone 時會發生降級,即:通訊不加密。
但是當雙方都是 MCEncryptionOptional,通訊也是不安全的, 可能發生中間人攻擊:
實施中間人攻擊首先要識別出基於 TCP 一些資料包, 如中的淺色部分,資料包都是有特點的, 因此是可以識別的。 但是沒有示範中間人攻擊的原因是, plist檔案中的資料貌似是有關聯關係,簡單的將0改為1, 並不會將 false 改成 true,會造成 plist 無效, 因此實施中間人攻擊時可能需要將整個 plist 都截獲後, 修改,再發送。
其他目前沒有逆向出整個通訊協定,但是如果想將一些外設類比成 MC 裝置,需要進一步逆向出整個協議。MultipeerConnectivity 連結了 IOKit,因此可能間接得暴露出 IOKit 的攻擊面。