MOBILE

Mobile Application Security : The Apple iPhone - Networking

1/20/2011 3:44:54 PM
There are several available mechanisms for obtaining resources over network connections on the iPhone, depending on whether your needs are for loading content over HTTP/FTP, doing lower level socket manipulation, or networking with other devices over Bluetooth. We’ll look first at the most common mechanism, the URL loading API.

The URL Loading API

The URL Loading API supports HTTP, HTTPS, FTP, and file resource types; these can be extended by subclassing the NSURLProtocol class. The normal way to interface to this API is via NSURLConnection or NSURLDownload, using an NSURL object as the input (see Listing 1).

Listing 1. Using NSURLConnection (Sample Code from Apple’s “URL Loading System Overview”)
  NSURL *myURL = [NSURL URLWithString:@"https://cybervillains.com/"];

NSMutableURLRequest *myRequest = [NSMutableURLRequestrequestWithURL:
myURLcachePolicy:NSURLRequestReload
IgnoringCacheDatatimeoutInterval:60.0];

[[NSURLConnection alloc] initWithRequest:myRequest
delegate:self];

The request object simply gathers all the properties of the request you’re about to make, with NSURLConnection performing the actual network connection. Requests have a number of methods controlling their behavior—one method that should never be used is setAllowsAnyHTTPSCertificate.

I hesitate to even mention it, should it make some foolhardy developer aware of it. However, for the benefit of penetration testers and QA engineers who have to look specifically for terrible ideas, I’ll specifically call out: Don’t use this method. The correct solution is to update the certificate store.

By default, HTTP and HTTPS request results are cached on the device. For increased privacy, you may consider changing this behavior using a delegate of NSURLConnection implementing connection:willCacheResponse (see Listing 2).

Listing 2. Using NSURLConnection
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse
{

NSCachedURLResponse *newCachedResponse=cachedResponse;
if ([[[[cachedResponse response] URL] scheme] isEqual:@"https"])
{
newCachedResponse=nil;
}
return newCachedResponse;
}

One surprise about the NSURL family is that all cookies stored are accessible by any application that uses the URL loading system (http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Concepts/URLOverview.html#//apple_ref/doc/uid/20001834-157091). This underscores the need to set reasonable expiration dates on cookies, as well as to refrain from storing sensitive data in cookies.

NSStreams

Cocoa Socket Streams are most useful when the need arises to use network sockets for protocols other than those handled by the URL loading system, or in places where you need more control over how connections behave. To do this, you have to create an NSStream object, instructing it to receive input, send output, or both. For most networking purposes, both will be required (see Listing 3).

Listing 3. Creating a Socket Stream
// First we define the host to be contacted
NSHost *myhost = [NSHost hostWithName:[@"www.conglomco.com"]];

// Then we create
[NSStream getStreamsToHost:myhost
port:80
inputStream:&MyInputStream
outputStream:&MyOutputStream];

[MyInputStream setProperty:NSStreamSocketSecurityLevelTLSv1
forKey:NSStreamSocketSecurityLevelKey];

// After which you'll want to retain the streams and open them

The key here is to set NSStreamSocketSecurityLevel appropriately. For almost all situations, NSStreamSocketSecurityLevelSSLv3 or NSStreamSocketSecurityLevelTLSv1 should be used. Unless, you’re writing a program where transport security just doesn’t matter (for example, a web crawler), SSLv2 or security negotiation should not be used.

Peer to Peer (P2P)

iPhone OS 3.0 introduced the ability to do P2P networking between devices via Bluetooth. Although technically part of the GameKit, the GKSession class is likely to be used by non-game applications as well, for collaboration and data exchange. This means that opportunities for data theft are increased. Also, because data can potentially be streamed to the device by a malicious program or user, we have another untrusted input to deal with.

GKSessions can behave in one of three different modes—client, server, or peer (a combination of client and server). The easiest way to interface to this functionality is through a GKPeerPickerController object, which provides a UI to allow the user to select from a list of peers. It should be noted, however, that using this controller is not required. This effectively allows an application to initiate or scan for a session without user interaction.

To find other devices (peers), a server device advertises its availability using its sessionID, while a client device polls for a particular ID. This session identifier can be specified by the developer, or, if it’s unspecified, it can be generated from the application’s App ID.

Because of the use of developer-specified sessionIDs and the ability to have background P2P activity, issues can arise where a developer uses a GKSession to advertise or scan in the background, pairing with any matching device that knows a shared sessionID. If sessionIDs are predictable, this means that the user’s device might be paired without their knowledge and against their will. This can lead to all manner of mischief.

In addition to simple Bluetooth connectivity, the GKVoiceChatService allows for full-duplex voice communication between devices. This is another connection that can be done without user interaction. To establish a voice connection with another device, another developer-specified identifier is needed, the participantID. Because an active pairing is already necessary for the use of Voice Chat, this ID can be a simple username or other symbolic name.

Here are the three main important security considerations when working with the GameKit:

  • Ensure that you use a unique identifier for the sessionID to avoid unwanted peering, and use the provided Picker API to let users explicitly accept connections.

  • Remember that GKSession remote connections supply untrusted data—sanity checks must be performed before operating on this data.

  • Use GKPeerPickerController to allow users to confirm connections.

Other  
  •  Windows Phone 7 Development : Handling Device Exceptions
  •  Registering a Windows Phone Device for Debugging
  •  Programming the Mobile Web : WebKit CSS Extensions (part 5) - Transformations
  •  Programming the Mobile Web : WebKit CSS Extensions (part 4) - Animations
  •  Programming the Mobile Web : WebKit CSS Extensions (part 3) - Transitions
  •  Programming the Mobile Web : WebKit CSS Extensions (part 2) - Reflection Effects & Masked Images
  •  Programming the Mobile Web : WebKit CSS Extensions (part 1) - WebKit Functions & Gradients
  •  Windows Phone 7 Development : Debugging Application Exceptions (part 2) - Debugging a Web Service Exception
  •  Windows Phone 7 Development : Debugging Application Exceptions (part 1) - Debugging Page Load Exceptions
  •  Programming the Mobile Web : JavaScript Libraries
  •  Programming the Mobile Web : Ajax Support
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 5) - Deploying the Service to Windows Azure
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 4) - Coding NotepadViewModel
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 3) - Coding the BoolToVisibilityConvert
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 2) - Coding MainPage
  •  Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 1) - Building the User Interface
  •  Building Android Apps : Detecting Browsers with WURFL
  •  Building Android Apps : Submitting Your App to the Android Market - Preparing a Release Version of Your App
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 2) - Implementing a WCF Service to Access the SQL Azure Database
  •  Windows Phone 7 Development : Creating a Cloud Service to Access the Cloud Database (part 1) - Generating an Object Model to Access the Cloud Database
  •  
    Most View
    Windows 7 : Migrating User State Data - Planning User State Migration Using USMT
    Keyboard Basher (Part 1)
    Find It Online : Toodledo, CrashMyPad, Quora, RetailMeNot & Storify
    MasterClass: How To Automate Your Life (Part 1)
    Smartphone Supertech
    Using Standard NT Security Features in Windows 7 : WORKING DIRECTLY WITH WINDOWS NT SECURITY (part 1) - Checking User Permissions
    Lenovo Thinkpad Carbon Touch Ultrabook Review (Part 2)
    Active Directory Domain Services 2008 : Seize the RID Master Role, Seize the PDC Emulator Role, Seize the Infrastructure Master Role
    Mobile Phones Buying Guide – April 2013 (Part 1)
    VMware Fusion 5 - Your Mac is Virtually a PC
    Top 10
    10 Contenders For The 'Ultimate Protector' Crown (Part 5) : Microsoft Security Essentials 4.1, AVG Antivirus Free 2013
    10 Contenders For The 'Ultimate Protector' Crown (Part 4) : Norton Internet Security, Avast Free Antivirus Version 7
    10 Contenders For The 'Ultimate Protector' Crown (Part 3) : Eset Smart Security 6, Kaspersky Internet Security 2013, Zonealarm Internet Security 2013
    10 Contenders For The 'Ultimate Protector' Crown (Part 2) : Bitdefender Total Security 2013, Trend Micro Maximum Security, Mcafee Internet Security 2013
    10 Contenders For The 'Ultimate Protector' Crown (Part 1)
    Sony Xperia TL - Much Improved But Still Imperfect (Part 3)
    Sony Xperia TL - Much Improved But Still Imperfect (Part 2)
    Sony Xperia TL - Much Improved But Still Imperfect (Part 1)
    Simple.TV - Transmits TV Programs To Mobile (Part 2)
    Simple.TV - Transmits TV Programs To Mobile (Part 1)