有的时候吧,总会有那么一种奇葩需求。比如你在使用某个app的时候,你不想让他知道你用的是啥型号的手机,你想把自己伪装成某个设备型号,比如把你的土豪金版本的iPhone XS Max伪装成iPhone 3GS,至于为啥要这么干,其实我也没明白...


管它为啥呢,咱就为了装逼不行么?那么如何实现呢?

越狱环境下

越狱环境下不要太简单,下个插件就可以解决了。或者你可以自己写一个,也很简单。无非就是用到MSHookFunction Hook 一下int uname(struct utsname *);int sysctlbyname(const char *, void *, size_t *, void *, size_t);这两个c方法。这里还是要懂点c语言的。
具体我就不讲了,也不是本文的重点。

非越狱环境

我也是掉坑里了才知道MSHookFunction在非越狱环境下是无效的,至于为啥,大家去google一下吧,有大神解释了,反正我也看懂😆,那么要怎么办呢?这就需要祭出某个404公司出品的fishhook了。

fishhook

直接贴上官方demo教程

#import <dlfcn.h>

#import <UIKit/UIKit.h>

#import "AppDelegate.h"
#import "fishhook.h"
 
static int (*orig_close)(int);
static int (*orig_open)(const char *, int, ...);
 
int my_close(int fd) {
  printf("Calling real close(%d)\n", fd);
  return orig_close(fd);
}
 
int my_open(const char *path, int oflag, ...) {
  va_list ap = {0};
  mode_t mode = 0;
 
  if ((oflag & O_CREAT) != 0) {
    // mode only applies to O_CREAT
    va_start(ap, oflag);
    mode = va_arg(ap, int);
    va_end(ap);
    printf("Calling real open('%s', %d, %d)\n", path, oflag, mode);
    return orig_open(path, oflag, mode);
  } else {
    printf("Calling real open('%s', %d)\n", path, oflag);
    return orig_open(path, oflag, mode);
  }
}
 
int main(int argc, char * argv[])
{
  @autoreleasepool {
    rebind_symbols((struct rebinding[2]){{"close", my_close, (void *)&orig_close}, {"open", my_open, (void *)&orig_open}}, 2);
 
    // Open our own binary and print out first 4 bytes (which is the same
    // for all Mach-O binaries on a given architecture)
    int fd = open(argv[0], O_RDONLY);
    uint32_t magic_number = 0;
    read(fd, &magic_number, 4);
    printf("Mach-O Magic Number: %x \n", magic_number);
    close(fd);
 
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
  }
}

官方的demo里面示例了如何Hookopenclose方法,看起来好像也(huang)不(de)复(yi)杂(pi),努力回忆一下大学里学的C语言基础吧...
仔细研究一下发现,其实就那么回事,咱们按照它的模式依葫芦画瓢就好了,无非就是


//先申明一个原方法 orig_原方法名
static int (*orig_open)(const char *, int, ...);

//然后实现一个新方法 new_原方法名
int my_open(const char *path, int oflag, ...) {
    //新方法需要添加的逻辑
    ...
    ...
    //调用原方法,返回内容
    return orig_open(path, oflag, mode);
}

//最后在main里面调用
rebind_symbols((struct rebinding[1]){{"open", my_open, (void *)&orig_open}}, 1);

//反正大概意思就是用my_open取代了原来的open方法,就实现了咱们Hook open方法的目的了

看懂了之后,咱们就开工。

Hook uname

为啥要Hookuname这个方法呢?咱们看一下ios上是如何获取设备型号的吧。下面是一份网络找来的代码:

- (NSString*)iphoneType {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *platform = [NSString stringWithCString: systemInfo.machine encoding:NSASCIIStringEncoding];
    // simulator 模拟器
    if ([platform isEqualToString:@"i386"]) return @"Simulator";
    if ([platform isEqualToString:@"x86_64"]) return @"Simulator";
    //常用机型不需要的可自行删除
    if([platform isEqualToString:@"iPhone1,1"]) return@"iPhone 2G";
    if([platform isEqualToString:@"iPhone1,2"]) return@"iPhone 3G";
    if([platform isEqualToString:@"iPhone2,1"]) return@"iPhone 3GS";
    if([platform isEqualToString:@"iPhone3,1"]) return@"iPhone 4";
    if([platform isEqualToString:@"iPhone3,2"]) return@"iPhone 4";
    if([platform isEqualToString:@"iPhone3,3"]) return@"iPhone 4";
    if([platform isEqualToString:@"iPhone4,1"]) return@"iPhone 4S";
    if([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    if([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    if([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
    if([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
    if([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
    if([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
    if([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    if([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    if([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    if([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    if([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    if([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    if([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    if([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
    if([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
    if([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
    if([platform isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";
    if([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
    if([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X";
    if([platform isEqualToString:@"iPhone10,8"]) return @"iPhone XR";
    if([platform isEqualToString:@"iPhone10,2"]) return @"iPhone XS";
    if([platform isEqualToString:@"iPhone10,4"]) return @"iPhone XS Max";
    if([platform isEqualToString:@"iPhone10,6"]) return @"iPhone XS Max";
    return platform;
}

可以看到方法的最上面是调用了uname(&systemInfo);,这就是为什么我们要Hookuname方法了。
我们就按照上面fishhook的demo,依葫芦画瓢,写出我们的代码,直接贴出我们的Hook代码:

static int (*origianl_uname)(struct utsname *);
int my_uname(struct utsname *value);

int my_uname(struct utsname *value) {
    int ret = origianl_uname(value);
    NSString *newModelName = @"iPhone2,1" //说好的咱们要装逼为`3GS`的呢。
    const char *newModelNameChar = newModelName.UTF8String;
    strcpy(value->machine, newModelNameChar);
    return ret;
}
int main(int argc, char * argv[])
{
  @autoreleasepool {
      rebind_symbols((struct rebinding[1]){{"uname", my_uname, (void *)&origianl_uname}}, 1);
  }
}

另外,还有另外一种获取设备型号的方法,就是前面提过的sysctlbyname,这个就交给读者们自己去摸索吧,我就不给你们“留作业”了。

温馨提示

文章配图我是PS的啊,明眼人一眼就能看出来。不准拿我的代码去干啥不该干的事情啊,要是万一被我发现了!哼,我好像也拿你没辙...