android elf 加固_android so加固之section加密

[TOC]

引言

如何对so文件中的核心代码进行保护?

通过将核心代码写到自定义节中,并且对该节使用加密工具进行加密,在so文件执行时,利用attribute((constructor));属性,先于main执行解密函数,作用类似于java中的构造函数

实现流程

确定好自定义节的名称

开始加密流程遍历所有节头,根据节头名来定位需要加密的节

获取节头中节的起始位置和大小,对节头指向的数据进行加密

编写解密代码用属性: attribute((constructor));声明解密函数

在native层编写解密函数

代码实现

加密流程

#include

#include

#include

#include

#include

int main(int argc, char** argv)

{

int fd;

Elf32_Ehdr ehdr;

Elf32_Shdr shdr;

char * section_name_table;

int i;

unsigned int base, length;

char *content;

//参数验证

if(argc != 3)

{

printf("Encrypt section of elf file\n\nUsage:\n\t%s \n", *argv);

goto _error;

}

if((fd = open(argv[1], O_RDWR, 0777)) == -1)

{

perror("open");

goto _error;

}

if(read(fd, &ehdr, sizeof(Elf32_Ehdr)) != sizeof(Elf32_Ehdr))

{

perror("read elf header");

goto _error;

}

//读取节头字符串表

printf("[+] Begining find section %s\n", argv[2]);

lseek(fd, ehdr.e_shoff+sizeof(Elf32_Shdr)*ehdr.e_shstrndx, SEEK_SET);

if(read(fd, &shdr, sizeof(Elf32_Shdr)) != sizeof(Elf32_Shdr))

{

perror("read elf section header which contain string table");

goto _error;

}

if((section_name_table = (char*) malloc(shdr.sh_size)) == NULL)

{

perror("malloc for SHT_STRTAB");

goto _error;

}

lseek(fd, shdr.sh_offset, SEEK_SET);

if(read(fd, section_name_table, shdr.sh_size) != shdr.sh_size)

{

perror("read string table");

goto _error;

}

lseek(fd, ehdr.e_shoff, SEEK_SET);

//根据节头名来定位需要加密的节头

for(i=0; i

{

if(read(fd, &shdr, sizeof(Elf32_Shdr)) != sizeof(Elf32_Shdr))

{

perror("read section");

goto _error;

}

if(strcmp(section_name_table+shdr.sh_name, argv[2]) =

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值