2018-09-25 MonkeyDev hook glTexImage2D(成功)

  1. MonkeyDev
  2. IPA 必须脱壳
  3. fishhook

把一下代码添加到 MDTestDylib.m 去。

// glTexImage2D
#include 
#include 
#include 
static void saveImageToPNG(const char *path, int _width, int _height, unsigned char * _data) {
    bool needToCopyPixels = false;
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    {
//        bitsPerPixel = 24;
    }
    
    
    int bytesPerRow    = (bitsPerPixel/8) * _width;
    int myDataLength = bytesPerRow * _height;

    unsigned char *pixels    = _data;
    
    if (bitsPerPixel == 24)
    {
        pixels = (unsigned char*) malloc(sizeof(unsigned char) *myDataLength);
        
        for (int i = 0; i < _height; ++i)
        {
            for (int j = 0; j < _width; ++j)
            {
                pixels[(i * _width + j) * 3] = _data[(i * _width + j) * 4];
                pixels[(i * _width + j) * 3 + 1] = _data[(i * _width + j) * 4 + 1];
                pixels[(i * _width + j) * 3 + 2] = _data[(i * _width + j) * 4 + 2];
            }
        }
        
        needToCopyPixels = true;
    }

    // make data provider with data.
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    {
//        bitmapInfo |= kCGImageAlphaPremultipliedLast;
    }
    CGDataProviderRef provider        = CGDataProviderCreateWithData(0, pixels, myDataLength, 0);
    CGColorSpaceRef colorSpaceRef     = CGColorSpaceCreateDeviceRGB();
    CGImageRef iref                   = CGImageCreate(_width, _height,
                                                       bitsPerComponent, bitsPerPixel, bytesPerRow,
                                                       colorSpaceRef, bitmapInfo, provider,
                                                       0, false,
                                                       kCGRenderingIntentDefault);

    UIImage* image                    = [[UIImage alloc] initWithCGImage:iref];

    CGImageRelease(iref);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);

    NSData *data;
    
//    if (saveToPNG)
    {
        data = UIImagePNGRepresentation(image);
    }
//    else
//    {
//        data = UIImageJPEGRepresentation(image, 1.0f);
//    }
    
    [data writeToFile:[NSString stringWithUTF8String:path] atomically:YES];
    
    if (needToCopyPixels) free(pixels);
}

static void (*orig_glTexImage2D)( GLenum target,
                                GLint level,
                                GLint internalFormat,
                                GLsizei width,
                                GLsizei height,
                                GLint border,
                                GLenum format,
                                GLenum type,
                                 const GLvoid * data);
void my_glTexImage2D(GLenum target,
                     GLint level,
                     GLint internalFormat,
                     GLsizei width,
                     GLsizei height,
                     GLint border,
                     GLenum format,
                     GLenum type,
                     const GLvoid * data)
{

    static int fileIndex = 0;
    ++fileIndex;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    char fullNamebuf[128];
    snprintf(fullNamebuf, 128, "%s/%d.png", [documentsDirectory UTF8String], fileIndex);

    NSLog(@" >>>>>>>>>>>>>>>>>>> %s", fullNamebuf);
    saveImageToPNG(fullNamebuf, width, height, data);
    return orig_glTexImage2D(target, level, internalFormat, width, height, border, format, type, data);
}

CHConstructor{
    NSLog(INSERT_SUCCESS_WELCOME);

#define M 3
    rebind_symbols((struct rebinding[M]){
        {"close", my_close, (void *)&orig_close},
        {"open", my_open, (void *)&orig_open},
        {"glTexImage2D", my_glTexImage2D, (void*)&orig_glTexImage2D}
    }, M);
#undef M

缺点明显,必须把所有的资源都玩一边,才能获取到对应的资源。

UP: 2018.09.25
附上 saveImageToPNG 简约版本,没有压缩过的,所以生成的图片有点大,4倍多。

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

static void saveImageToPNG(const char *path, int _width, int _height, unsigned char * _data) {
    stbi_write_png(path, _width, _height, 4, _data, _width * 4);
}

你可能感兴趣的:(2018-09-25 MonkeyDev hook glTexImage2D(成功))