1 Star 0 Fork 0

ricky.z/pkg_analyse

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.py 4.10 KB
一键复制 编辑 原始数据 按行查看 历史
ricky.z 提交于 2020-11-30 18:56 . modify
import os
import sys
import subprocess
import re
from utils import rpm_detect
from readelf import Read_ELF
def get_rpm_name(rpm):
rpm_name_list = rpm.split('/')[-1]
rpm_sub_name_list = rpm_name_list.split('-')
name = ''
for idx, rpm_sub_name in enumerate(rpm_sub_name_list):
if len(re.findall(r'^[0-9]\.', rpm_sub_name)) > 0:
break
if idx > 0:
name = name + '-' + rpm_sub_name
else:
name = rpm_sub_name
return name
def rpm_to_source_or_bin(path):
source_dir = ''
type = ''
name = ''
if os.path.isfile(path):
file_name = path.split('/')[-1]
if file_name.endswith('.src.rpm') > 0:
stdout = subprocess.run('rpm -ivh %s'%path, shell=True, check=True, stdout=subprocess.PIPE).stdout.decode()
name = get_rpm_name(file_name)
rpm_file_list = subprocess.run('rpm -qpl %s'%path, shell=True, check=True, stdout=subprocess.PIPE).stdout.decode().split('\n')
spec_name = ''
tar_name = ''
for rpm_file in rpm_file_list:
spec_name_tmp = re.findall(r'[0-9a-z\.\-]+\.spec$', rpm_file, re.I)
tar_name_tmp = re.findall(r'[0-9a-z\.\-]+\.tar.gz$', rpm_file, re.I)
if len(spec_name_tmp) > 0:
spec_name = spec_name_tmp[0]
if len(tar_name_tmp) > 0:
tar_name = tar_name_tmp[0]
if spec_name != '' and tar_name != '':
break
print(spec_name)
rpm_path = '/root/rpmbuild/SOURCES/%s'%tar_name
rpm_detect('tar', 'tar')
stdout = subprocess.run('tar -xzvf %s'%rpm_path, shell=True, check=True, stdout=subprocess.PIPE).stdout.decode()
tar_name = re.findall(r'([a-z0-9\-\.]+)\.tar.gz$', tar_name, re.I)[0]
source_dir = os.path.join(os.getcwd(), tar_name)
type = 'src.rpm'
elif file_name.endswith('.rpm'):
stdout = subprocess.run('rpm2cpio %s | cpio -div'%path, shell=True, check=True, stdout=subprocess.PIPE).stdout.decode()
name = get_rpm_name(file_name)
source_dir = os.getcwd()
type = 'rpm'
elif file_name.endswith('.zip'):
rpm_detect('unzip', 'unzip')
stdout = subprocess.run('unzip %s'%path, shell=True, check=True, stdout=subprocess.PIPE).stdout.decode()
zip_name = re.findall(r'([a-z0-9\-\.]+)\.zip$', file_name, re.I)[0]
print(zip_name)
source_dir = os.path.join(os.getcwd(), zip_name)
type = 'src'
elif file_name.endswith('.tar.gz'):
rpm_detect('tar', 'tar')
stdout = subprocess.run('tar -xzvf %s'%path, shell=True, check=True, stdout=subprocess.PIPE).stdout.decode()
tar_name = re.findall(r'([a-z0-9\-\.]+)\.tar.gz$', file_name, re.I)[0]
source_dir = os.path.join(os.getcwd(), tar_name)
type = 'src'
else:
source_dir = ''
type = 'unkown'
return source_dir, type
def get_api_from_EFI(debuginfo_path, func_name):
pass
def searh_api(path, path_debuginfo):
source, type = rpm_to_source_or_bin(path)
if type == 'rpm':
debuginfo, _ = rpm_to_source_or_bin(path_debuginfo)
if debuginfo == '':
print('No debuginfo rpm found.')
debuginfo_list = []
else:
debuginfo_list = subprocess.run("find %s -name '*.debug'"%debuginfo, shell=True, check=True, stdout=subprocess.PIPE).stdout.decode().split('\n')
so_list = subprocess.run("find %s -name '*.so*'"%source, shell=True, check=True, stdout=subprocess.PIPE).stdout.decode().split('\n')[:-1]
#print('_______________')
#print(so_list)
func_lists = []
for so in so_list:
#print(so)
so = re.findall(r'(.+\.so[\.0-9]*)$', so, re.I)
if len(so)==0:
continue
so_name = so[0].split('/')[-1]
for debuginfo_path in debuginfo_list:
if debuginfo_path.find(so_name)>=0:
#print('*' + debuginfo_path)
break
try:
stdout = subprocess.run("nm -D %s | grep ' T '"%so[0], shell=True, check=True, stdout=subprocess.PIPE).stdout.decode().split('\n')[:-1]
except Exception:
continue
if len(stdout)==0:
continue
func_list = []
for line in stdout:
#print(line)
func = re.findall(r'\sT\s(.+)$', line)
if len(func)==0:
continue
func_list.append(func[0])
#print(func_list)
elf = Read_ELF(debuginfo_path)
for func in func_list:
elf.get_api_from_EFI(func)
if __name__ == '__main__':
path = sys.argv[1]
path_debuginfo = sys.argv[2]
print(path)
print(searh_api(path, path_debuginfo))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/RickyZ5351/pkg_analyse.git
git@gitee.com:RickyZ5351/pkg_analyse.git
RickyZ5351
pkg_analyse
pkg_analyse
master

搜索帮助