python读取so文件输出到text

python读取so文件输出到text

import os
import sys
import re

def extract_strings(binary_data):
    """从二进制数据中提取可打印的字符串"""
    # 使用正则表达式匹配至少4个连续的可打印字符
    strings = re.findall(rb'[\x20-\x7E]{4,}', binary_data)
    # 解码并过滤掉过短的字符串
    return [s.decode('utf-8', errors='replace') for s in strings if len(s) >= 4]

def format_hex_dump(binary_data):
    """生成易读的十六进制转储格式(无大小限制)"""
    result = []
   
    # 按16字节一行处理所有数据
    for i in range(0, len(binary_data), 16):
        chunk = binary_data[i:i+16]
       
        # 地址偏移
        address = f"{i:08x}: "
       
        # 十六进制表示(两个字符一组,空格分隔)
        hex_part = ' '.join([f"{b:02x}" for b in chunk]).ljust(48)  # 16*2 + 15 = 47个字符,再加一个空格
       
        # ASCII表示(替换不可打印字符为.)
        ascii_part = ''.join([chr(b) if 32 <= b <= 126 else '.' for b in chunk])
       
        # 组合成一行
        result.append(f"{address}{hex_part} |{ascii_part}|")
   
    return '\n'.join(result)

def read_so_files():
    # 定义so文件目录路径
    so_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib', 'armeabi')
   
    # 检查目录是否存在
    if not os.path.exists(so_dir):
        print(f"错误:目录不存在 - {so_dir}")
        return
   
    # 获取目录下的所有so文件
    so_files = [f for f in os.listdir(so_dir) if f.endswith('.so')]
   
    if not so_files:
        print(f"在目录 {so_dir} 中未找到.so文件")
        return
   
    print(f"找到 {len(so_files)} 个so文件:")
   
    # 为每个so文件创建对应的txt文件
    for so_file in so_files:
        so_path = os.path.join(so_dir, so_file)
        txt_file = so_file.replace('.so', '.txt')
        txt_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), txt_file)
       
        print(f"处理文件:{so_file}")
       
        try:
            # 获取文件信息
            file_size = os.path.getsize(so_path)
            print(f"  文件大小:{file_size:,} 字节")
           
            # 以二进制模式读取so文件
            with open(so_path, 'rb') as f:
                content = f.read()
           
            # 提取字符串
            strings = extract_strings(content)
            print(f"  提取到 {len(strings)} 个可打印字符串")
           
            # 生成输出内容,包含完整信息
            output_content = []
            output_content.append(f"===== {so_file} 文件完整分析报告 =====\n")
            output_content.append(f"文件大小: {file_size:,} 字节\n")
           
            # 添加ELF头部信息(如果是ELF文件)
            if content.startswith(b'\x7fELF'):
                output_content.append("\n[ELF文件格式信息]:\n")
                # 简单解析ELF头部信息
                elf_class = '32位' if content[4] == 1 else '64位'
                elf_endian = '小端' if content[5] == 1 else '大端'
                elf_os_abi = content[7]
                output_content.append(f"ELF类型: {elf_class}")
                output_content.append(f"字节序: {elf_endian}")
                output_content.append(f"OS/ABI: {elf_os_abi}")
           
            # 添加所有可打印字符串(无数量限制)
            output_content.append("\n[所有提取的可打印字符串]:\n")
            if strings:
                for i, string in enumerate(strings, 1):
                    output_content.append(f"{i:6d}: {string}")
            else:
                output_content.append("未找到可打印字符串")
           
            # 添加完整的十六进制转储(无大小限制)
            output_content.append("\n[完整十六进制转储]:\n")
            hex_dump = format_hex_dump(content)
            output_content.append(hex_dump)
           
            # 写入txt文件
            print(f"  正在写入完整内容到 {txt_file}...")
            with open(txt_path, 'w', encoding='utf-8') as f:
                f.write('\n'.join(output_content))
           
            print(f"  已转换为:{txt_file}")
           
        except Exception as e:
            print(f"  处理失败:{str(e)}")
   
    print("处理完成!")

if __name__ == "__main__":
    read_so_files()
© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容