首页
社区
课程
招聘
[原创]Frida编译
2021-4-13 16:19 13293

[原创]Frida编译

2021-4-13 16:19
13293


摘要:本文记录编译frida可能遇到的问题和解决方案。

         直接安装和使用官方Release版本的可以忽略。


编译测试平台:【Windows】、【Ubuntu 20.04.02】


一、【Windows】

    测试平台是Window 10, Visual Studio 2019 (也具有2017、2015等)


官方说明比较清楚:

    简单说,就是环境先得有git、python3.8、node.js、powershell和vs2019(但需要支持vs2017)



(1)基本环境配置

假设已安装了git、python3.8、node.js、powershell和vs2019;

window10 一般自带 powershell

如果已经在全局或用户环境变量PATH中添加了上述工具的目录,则直接启动vs2019即可。

由于我没有将工具添加到全局PATH中,尤其是python3.8,由于有多个python版本,都是根据需要bat需要使用。

得到源码

git clone --recurse-submodules https://github.com/frida/frida
@rem set python3.8
set path=C:\Python38-32;C:\Python38-32\Scripts;%path%
set PYTHONPATH=C:\Python38-32\DLLs;c:\Python38-32\Lib;
@rem set git
set path=C:\ProgramData\Git\cmd;%path%
@rem set nodejs
set path=C:\nodejs;%path%
@start visual 2019
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019.lnk"
再最后启动Visual Studio 2019之前,我们可能还需要检测下.py文件关联的执行程序,
一般安装VS时会设定为它自己安装的一个版本,比如,
cmd命令执行【assoc .py】得到【.py=Python.File】
cmd命令执行【ftype Python.File】得到
【Python.File="C:\Python27amd64\python.exe" "%1" %*】
这里关联的是vs以前安全时的python27,这里需要更改为我们的python3.8
管理员权限执行命令【ftype Python.File="C:\Python38-32\python.exe" "%1" %*】

否则编译过程中会出现deps.py执行错误

(2)Spectre漏洞缓解

关于Spectre漏洞缓解,如下图为【frida-core】项目属性,其开启了Specture漏洞缓解编译,基本是所有项目类型都开了了漏洞缓解策略。

如果没有按照v141 spectre则需要改动所有项目的该编译选项。这里我们不做改动,而选择按照v141 Spectre组件来解决。

【Visual Studio 2019】》【工具】》【获取工具和功能】》【单个组件】》选择v141-VS 2017 C++ Spectre缓解库安装


(3)C4819 警告

中文系统一般会在编译过程产生大量的C4819警告,严重影响编译信息(尤其错误信息)的查阅

1>D:\src\frida\build\sdk-windows\x64-debug\include\glib-2.0\glib\gatomic.h : warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
2>D:\src\frida\build\sdk-windows\x64-debug\include\glib-2.0\glib\gtestutils.h : warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
1>


这里我们批量处理将UTF8转换为UTF16格式,下述python3代码(get_conv_files)主要完成C4819警告文件路径提取,(converfile)完成编码转换

import chardet
#chardet.detect(fbs)
#{'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}
import codecs
def convertfile(fn,dc='866',ec='UTF-16'): #866 855 russian 
  fstr = ''
  with open(fn,'rb') as fin:
    fstr = fin.read()
  ustr = fstr.decode(dc)
  with codecs.open(fn,'wb',ec) as fout:
    fout.write(ustr)

def get_conv_files(wc4819_log):
  with open(wc4819_log,'rb') as fin:
    lns = fin.readlines()
  wlns = [ln for ln in lns if b'warning C4819:' in ln]
  fn_pat = re.compile(b'(?s)(.:.*?)[\(:]')
  files = []
  for ln in wlns:
    if fn_pat.search(ln):
      fn = fn_pat.search(ln).groups()[0].strip().decode()
      if fn not in files:
        files.append(fn)
  return files

(3.1)无论是Release|Debug 或 win32|x64 ,在全新生成所有项目产生的信息中,全选复制保存为 C4819.txt文件
fs= get_conv_files(C4819.txt文件路径)
for f in fs:
  convertfile(f,'utf8')


(4)pdb非pdb

如下,这个编译错误比较吊诡,虽然VS提示添加/FS,但于事无补,网络上也没什么有效的解决方案。

走投无路时,回到了PDB文件的本源。实际上对应项目产生的是两个不同的PDB,但使用了相同的名称。

27>D:\src\frida\build\tmp-windows\win32-release\frida-server\server\server.c : fatal error C1041: 无法打开程序数据库“D:\src\frida\build\frida-windows\Win32-Release\bin\frida-server.pdb”;如果要将多个 CL.EXE 写入同一个 .PDB 文件,请使用 /FS
27>已完成生成项目“frida-server.vcxproj”的操作 - 失败。
28>LINK : fatal error LNK1201: 写入程序数据库“D:\src\frida\build\frida-windows\Win32-Release\bin\Frida.pdb”时出错;请检查是否是磁盘空间不足、路径无效或权限不够
28>已完成生成项目“frida-clr.vcxproj”的操作 - 失败

如下图所示,在编译器和连接器的选项中,

一个是编译器cl.exe 产生的PDB文件是【程序数据库文件】,名称为$(OutDir)$(TargetName).pdb

一个是链接器link.exe产生的PDB文件是【生成程序数据库文件】,名称也为$(OutDir)$(TargetName).pdb

项目指定了相同的名称,所以产生了冲突错误。

这里我们如下图所示,在产生该类错误的项目工程的编译器和连接器的对应选项中,修改为不同文件名,这里分别加了_cl和_ll后缀,实际只需改动一个造成不同名称即可。

(5)valac编译错误

提示【“%s”】错误的问题,由于相应的c文件(如keyed-archive.c)来源于vala文件(如 keyed-archive.vala)

所以,需要将对应valva里的所有宽字符【“】【”】替换,这里替换为【‘】【’】。

其它不是由vala产生的,直接双击跟进修改即可

    D:\B\src\frida\frida-core\src\fruity\fruity-host-session.vala
    throw new Error.INVALID_ARGUMENT ("Unable to find app with bundle identifier '%s'", program);
    D:\src\frida\frida-core\src\fruity\keyed-archive.vala
    throw new Error.PROTOCOL ("Expected dictionary to contain “%s”", key);


(6)编译完成后

我们可以cmd切换到【D:\src\frida\build\frida-windows\Win32-Release\lib\python3.8\site-packages】进行基本测试

import frida
proname = r'target.exe'
session = frida.attach(proname)
script = session.create_script('''
rpc.exports.eM = function (){
  return Process.enumerateModules();
};
''')

def on_msg(msg,data):
  print("[signal message] msg:", msg,"data:", data)
 
script.on('message',on_msg)
script.load()

ms = script.exports.e_m()
print([m['name'] for m in ms])



二、【Ubuntu 20.04.02】


官方要求也必将简单,基础编译器,git,lib32stdc++-0-dev,libc6-dev-i386,nodejs,npm等


这里使用的编译参数是
make python-linux-x86_64 PYTHON=/usr/bin/python3.8

出现的错误提示是GumRegs 宏定义的 struct user_regs_struct相关错误

struct user_regs_struct实际是 sys/user.h中的定义,但meson编译环境无法识别,这里直接硬编码,屏蔽掉sys/user.h的条件包含

而直接包含sys/user.h文件。

vi frida-gum/gum/backend-linux/gumprocess-linux.c


另外参考【Frida编译2022】













[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

最后于 2022-10-20 21:36 被tritium编辑 ,原因:
收藏
点赞3
打赏
分享
最新回复 (9)
雪    币: 831
活跃值: (2830)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
爬虫不看学历 2021-4-22 17:48
2
0
好东西总是只有少部分人才能看到!
雪    币: 62
活跃值: (545)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
万里星河 2021-4-25 09:15
3
0
吃瓜群众路过
雪    币: 4633
活跃值: (2054)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
hekes 2021-7-30 22:13
4
0

请教下windows编译不成功,请帮忙看下是哪里的问题:

已启动生成…
1>------ 已启动生成: 项目: frida-deps, 配置: Debug x64 ------
1>Generating frida-version.h
1>Traceback (most recent call last):
1>  File "E:\dbg\frida\releng\generate-version-header.py", line 46, in <module>
1>    generate_version_header()
1>  File "E:\dbg\frida\releng\generate-version-header.py", line 10, in generate_version_header
1>    description = subprocess.Popen(["git", "describe", "--tags", "--always", "--long"], cwd=build_dir, stdout=subprocess.PIPE).communicate()[0]
1>  File "D:\Python\Python38\lib\subprocess.py", line 854, in __init__
1>    self._execute_child(args, executable, preexec_fn, close_fds,
1>  File "D:\Python\Python38\lib\subprocess.py", line 1307, in _execute_child
1>    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
1>FileNotFoundError: [WinError 2] 系统找不到指定的文件。
1>D:\Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(238,5): error MSB8066: “generate-version-header.py;windows-node-modules.txt;deps.py”的自定义生成已退出,代码为 1。
1>已完成生成项目“frida-deps.vcxproj”的操作 - 失败。
2>------ 已启动生成: 项目: gum-64, 配置: Debug x64 ------
3>------ 已启动生成: 项目: gumquickcompile, 配置: Debug Win32 ------
2>Generating gumenumtypes.c
2>D:\Python\Python38\python.exe: can't open file 'E:\dbg\frida\build\toolchain-windows\bin\glib-mkenums': [Errno 2] No such file or directory
2>Generating gumenumtypes.h
2>D:\Python\Python38\python.exe: can't open file 'E:\dbg\frida\build\toolchain-windows\bin\glib-mkenums': [Errno 2] No such file or directory
2>D:\Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(238,5): error MSB8066: “gum\gumenumtypes.c.template;gum\gumenumtypes.h.template”的自定义生成已退出,代码为 2。
2>已完成生成项目“gum-64.vcxproj”的操作 - 失败。
3>gumquickcompile.c
3>E:\dbg\frida\frida-gum\bindings\gumjs\gumquickcompile.c : fatal error C1083: 无法打开包括文件: “E:\dbg\frida\build\tmp-windows\frida-version.h”: No such file or directory
3>已完成生成项目“gumquickcompile.vcxproj”的操作 - 失败。
4>------ 已启动生成: 项目: gumjs-64, 配置: Debug x64 ------
4>Generating runtime
4>Traceback (most recent call last):
4>  File "E:\dbg\frida\frida-gum\bindings\gumjs\generate-runtime.py", line 335, in <module>
4>    subprocess.check_call([node_script_path("frida-compile"), "./runtime/entrypoint-quickjs.js", "-o", runtime] + quick_options, cwd=input_dir)
4>  File "D:\Python\Python38\lib\subprocess.py", line 359, in check_call
4>    retcode = call(*popenargs, **kwargs)
4>  File "D:\Python\Python38\lib\subprocess.py", line 340, in call
4>    with Popen(*popenargs, **kwargs) as p:
4>  File "D:\Python\Python38\lib\subprocess.py", line 854, in __init__
4>    self._execute_child(args, executable, preexec_fn, close_fds,
4>  File "D:\Python\Python38\lib\subprocess.py", line 1307, in _execute_child
4>    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
4>FileNotFoundError: [WinError 2] 系统找不到指定的文件。
4>D:\Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(238,5): error MSB8066: “generate-runtime.py”的自定义生成已退出,代码为 1。
4>已完成生成项目“gumjs-64.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 4 个,最新 0 个,跳过 0 个 ==========

最后于 2021-7-30 22:14 被hekes编辑 ,原因:
雪    币: 71
活跃值: (810)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
rongkao 2021-7-31 10:36
5
0
make
雪    币: 440
活跃值: (848)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
猫子 2021-8-18 10:27
6
0
hekes 请教下windows编译不成功,请帮忙看下是哪里的问题:已启动生成…1&gt;------&nbsp;已启动生成:&nbsp;项目:&nbsp;frida-deps,& ...
python 安装到系统盘试一下,按照官方推荐的按照目录 C:\Program Files (x86)\Python 3.8\
雪    币: 177
活跃值: (232)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
mergerly 1 2022-1-4 13:58
7
0
hekes 请教下windows编译不成功,请帮忙看下是哪里的问题:已启动生成…1&gt;------&nbsp;已启动生成:&nbsp;项目:&nbsp;frida-deps,& ...
进入frida文件夹,执行powershell脚本
$ powershell
PS> Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted
PS> .\releng\stage-python.ps1
雪    币: 221
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
lamorm 2022-4-22 14:58
8
1
大佬们,能不能帮我看下报错是啥原因
lm@lamorm:~/frida$ make core-android-arm
make[1]: 进入目录“/home/lm/frida”
. build/frida-env-android-arm.rc; builddir=build/tmp-android-arm/frida-gum; if [ ! -f $builddir/build.ninja ]; then         meson_args="--native-file build/frida-linux-x86_64.txt"; if [ android-arm != linux-x86_64 ]; then meson_args="$meson_args --cross-file build/frida-android-arm.txt"; fi; python3 /home/lm/frida/releng/meson/meson.py setup $meson_args --prefix /home/lm/frida/build/frida-android-arm --libdir /home/lm/frida/build/frida-android-arm/lib --default-library static -Doptimization=s -Db_ndebug=true --strip -Dgumpp=enabled -Dgumjs=enabled -Dv8=auto -Ddatabase=enabled -Dfrida_objc_bridge=auto -Dfrida_swift_bridge=auto -Dfrida_java_bridge=auto -Dtests=enabled  frida-gum $builddir || exit 1; fi; python3 /home/lm/frida/releng/meson/meson.py install -C $builddir || exit 1
The Meson build system
Version: 0.60.99
Source dir: /home/lm/frida/frida-gum
Build dir: /home/lm/frida/build/tmp-android-arm/frida-gum
Build type: cross build
Project name: frida-gum
Project version: 1.0.0

frida-gum/meson.build:1:0: ERROR: Unknown compiler(s): [['/home/lm/frida/build/frida-android-arm-clang', '-include', '/home/lm/frida/build/frida-version.h']]

A full log can be found at /home/lm/frida/build/tmp-android-arm/frida-gum/meson-logs/meson-log.txt
make[1]: *** [Makefile.linux.mk:111:build/frida-android-arm/lib/pkgconfig/frida-gum-1.0.pc] 错误 1
make[1]: 离开目录“/home/lm/frida”
make: *** [Makefile:4:core-android-arm] 错误 2
雪    币: 61
活跃值: (41)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
fkueihcy 2022-10-19 16:57
9
0
大佬们,vs2022 编译到了最后一步报错,求教
18>------ 已启动生成: 项目: frida-core, 配置: Debug Win32 ------
18>Serializing Windows resources
18>Serializing Agent resources
18>Serializing compiler resources
18>npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
18>
18>> frida-compiler-agent@1.0.0 build:typescript
18>> rollup --config rollup.config.typescript.ts --configPlugin typescript "--environment" "FRIDA_HOST_OS_FAMILY:windows,FRIDA_HOST_CPU_MODE:32" "--silent"
18>
18>npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
18>
18>> frida-compiler-agent@1.0.0 build:agent-core
18>> rollup --config rollup.config.agent-core.ts --configPlugin typescript "--environment" "FRIDA_HOST_OS_FAMILY:windows,FRIDA_HOST_CPU_MODE:32" "--silent"
18>
18>[WinError 2] 系统找不到指定的文件。
18>C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(247,5): error MSB8066: “src\windows\frida-windows.resources;src\agent.resources;src\frida.vala;src\compiler\compiler.resources”的自定义生成已退出,代码为 1。
雪    币: 61
活跃值: (41)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
fkueihcy 2022-10-20 16:11
10
0
fkueihcy 大佬们,vs2022 编译到了最后一步报错,求教 18>------ 已启动生成: 项目: frida-core, 配置: Debug Win32 ------ 18>Serializ ...
要先用release 模式编译一次
让编译器把 sdk-windows\x64-Release 和 sdk-windows\Win32-Release 先下载下来才能用debug模式编译
不然就会找不到v8-mksnapshot-windows-x86_64.exe路径
游客
登录 | 注册 方可回帖
返回