在运行时检测iPhone上的UDID欺骗

ken*_*enn 21 iphone spoofing udid

Jailbroken iPhone通过使用MobileSubstrate在iOS上玷污了一些基本的API,让我感到震惊.

http://www.iphonedevwiki.net/index.php/MobileSubstrate

我相信很多应用程序使用UDID作为验证设备和/或用户的手段,因为它是半自动和方便的,但你应该意识到这个问题:UIDevice并不像应该的那样防篡改.有一个叫做UDID Faker的应用程序,它可以让你轻松地在运行时欺骗别人的UDID.

http://www.iphone-network.net/how-to-fake-udid-on-ios-4/

这是它的源代码:

//
//  UDIDFaker.m
//  UDIDFaker
//

#include "substrate.h"

#define ALog(...) NSLog(@"*** udidfaker: %@", [NSString stringWithFormat:__VA_ARGS__]);
#define kConfigPath @"/var/mobile/Library/Preferences/com.Reilly.UDIDFaker.plist"

@protocol Hook
- (NSString *)orig_uniqueIdentifier;
@end

NSString *fakeUDID = nil;

static NSString *$UIDevice$uniqueIdentifier(UIDevice<Hook> *self, SEL sel) {  

    if(fakeUDID != nil) {
                 ALog(@"fakeUDID %@", fakeUDID);
        /* if it's a set value, make sure it's sane, and return it; else return the default one */
                return ([fakeUDID length] == 40) ? fakeUDID : [self orig_uniqueIdentifier];

    }
    /* ... if it doesn't then return the original UDID */
    else {
        return [self orig_uniqueIdentifier];
    }
}

__attribute__((constructor)) static void udidfakerInitialize() {  

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSString *appsBundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
        ALog(@"Loading UDID Faker into %@", appsBundleIdentifier);


        NSDictionary *config = [NSDictionary dictionaryWithContentsOfFile: kConfigPath];
        fakeUDID = [config objectForKey: appsBundleIdentifier];
        [fakeUDID retain];

        if(fakeUDID != nil) {

                ALog(@"Hooking UDID Faker into %@", appsBundleIdentifier);
                MSHookMessage(objc_getClass("UIDevice"), @selector(uniqueIdentifier), (IMP)&$UIDevice$uniqueIdentifier, "orig_");
        }

    [pool release];
}
Run Code Online (Sandbox Code Playgroud)

如您所见,UIDevice类中的uniqueIdentifier方法现在可以在任何应用程序上返回fakeUDID.

似乎Skype和其他一些应用程序检测到这种污点,但我不知道该怎么做.

我想要做的是:当启动时检测到受污染的UIDevice时,发出警报并退出(0).

想法?

ken*_*ytm 36

没有一种真正安全的方法来检查UDID是否真实.UDID是通过liblockdown获得的,它通过安全通道与lockdown通信以接收UDID:

+-----------+
| your code |
+-----------+
      |
+----------+       +-------------+       +-----------+
| UIDevice |<----->| liblockdown |<=====>| lockdownd |   (trusted data)
+----------+       +-------------+       +-----------+
         untrusted user                   trusted user
Run Code Online (Sandbox Code Playgroud)

当设备越狱时,可以更换所有4个组件.


检测UDID Faker存在的一种方法是检查是否存在对其唯一的某些标识(文件,函数等).这是一种非常具体和脆弱的反击,因为当暴露检测方法时,欺骗者可以简单地改变识别以隐藏它们的存在.

例如,UDID Faker依赖于plist文件/var/mobile/Library/Preferences/com.Reilly.UDIDFaker.plist.因此,您可以检查此文件是否存在:

NSString* fakerPrefPath = @"/var/mobile/Library/Preferences/com.Reilly.UDIDFaker.plist";
if ([[NSFileManager defaultManager] fileExistsAtPath:fakerPrefPath])) {
   // UDID faker exists, tell user the uninstall etc.
}
Run Code Online (Sandbox Code Playgroud)

它还定义了-[UIDevice orig_uniqueIdentifier]可用于绕过faker的方法:

UIDevice* device = [UIDevice currentDevice];
if ([device respondsToSelector:@selector(orig_uniqueIdentifier)])
   return [device orig_uniqueIdentifier];
else
   return device.uniqueIdentifier;
Run Code Online (Sandbox Code Playgroud)

当然,欺骗者可以简单地重命名这些东西.


更可靠的方法在于移动基板的工作原理.注入的代码必须位于dylib/bundle中,该dylib/bundle将加载到与UIKit 不同的内存区域中.因此,您只需要检查-uniqueIdentifier方法的函数指针是否在可接受的范围内.

// get range of code defined in UIKit 
uint32_t count = _dyld_image_count();
void* uikit_loc = 0;
for (uint32_t i = 0; i < count; ++ i) {
   if (!strcmp(_dyld_get_image_name(i), "/System/Library/Frameworks/UIKit.framework/UIKit")) {
     uikit_loc = _dyld_get_image_header(i);
     break;
   }
}

....

IMP funcptr = [UIDevice instanceMethodForSelector:@selector(uniqueIdentifier)];
if (funcptr < uikit_loc) {
   // tainted function
}
Run Code Online (Sandbox Code Playgroud)

无论如何UDID Faker是一个非常高级别的黑客(即它可以很容易避免).它通过提供假ID来劫持UIDevice和liblockdown之间的链接.

+-----------+
| your code |
+-----------+
      |
+----------+       +-------------+       +-----------+
| UIDevice |<--.   | liblockdown |<=====>| lockdownd |   (trusted data)
+----------+   |   +-------------+       +-----------+
               |   +------------+
               ‘-->| UDID Faker |
                   +------------+
Run Code Online (Sandbox Code Playgroud)

因此,您可以将代码请求UDID降低到liblockdown级别.这可以用于Jailbroken平台的应用程序,但这对于AppStore应用程序是不可能的,因为liblockdown是一个私有API.此外,欺骗者可以劫持liblockdown(这很容易,我希望没有人这样做),甚至可以取代锁定本身.

                   +-----------+
                   | your code |
                   +-----------+
                         |
+----------+       +-------------+       +-----------+
| UIDevice |<--.   | liblockdown |<=====>| lockdownd |   (trusted data)
+----------+   |   +-------------+       +-----------+
               |   +------------+
               ‘-->| UDID Faker |
                   +------------+
Run Code Online (Sandbox Code Playgroud)

(我不打算在这里展示如何使用liblockdown.你应该能够从你链接到的网站上找到足够的信息.)