编译iOS10.x可用lsof(越狱)

lsof常用语法:

检测本机端口开放 lsof -i:[port]
查找指定进程开放端口 lsof -aPi -p [pid]
查找所有监听端口 lsof -aPi

为什么自己编译?
目前没发现针对iOS10.x可用的lsof,我用它来查找进程占用的描述符,包括tcp端口

1.下载最新版本lsof
https://opensource.apple.com/tarballs/lsof/

2.Mac上解压后编译
根目录下有Makefile lsof/ lsof.plist
切换到lsof/下执行./Configure darwin(问题都选no),得到以下结果:

Configuring libproc-based Darwin lsof

This configuration step (the Inventory script) takes inventory of
the lsof distribution.  The script runs for a minute or two while
it checks that all the subdirectories, information files, scripts,
header files and source files that should be present really are.

It's not absolutely necessary that you take inventory, but it's a
good idea to do it right after the lsof distribution has been
unpacked.  Once the inventory has been taken, this script creates
the file ./.ck00MAN as a signal that the inventory step has been
done.

You can call the Inventory script directly at any time to take
inventory.  You can inhibit the inventory step permanently by
creating the file ./.neverInv, and you can tell the Configure script
to skip the inventory and customization steps with the -n option.

Do you want to take inventory (y|n) [y]? n
rm -f ddev.c dfile.c dlsof.h dmnt.c dnode*.c dproc.c dproto.h dsock.c dstore.c dzfs.h kernelbase.h machine.h machine.h.old new_machine.h __lseek.s Makefile Makefile.zfs ./tests/config.cflags
rm -f ./tests/config.cc ./tests/config.xobj ./tests/config.ldflags
Testing C library for localtime() and strftime(), using cc ... npresent
ln -s dialects/darwin/libproc/dlsof.h dlsof.h
ln -s dialects/darwin/libproc/ddev.c ddev.c
ln -s dialects/darwin/libproc/dfile.c dfile.c
ln -s dialects/darwin/libproc/dmnt.c dmnt.c
ln -s dialects/darwin/libproc/dproc.c dproc.c
ln -s dialects/darwin/libproc/dproto.h dproto.h
ln -s dialects/darwin/libproc/dsock.c dsock.c
ln -s dialects/darwin/libproc/dstore.c dstore.c
ln -s dialects/darwin/libproc/dnetpolicy.c dnetpolicy.c
ln -s dialects/darwin/libproc/dchannel.c dchannel.c
ln -s dialects/darwin/libproc/dnexus.c dnexus.c
ln -s dialects/darwin/libproc/machine.h machine.h

Makefile and lib/Makefile created.
./tests/config.cc created
./tests/config.cflags created
./tests/config.ldflags created
./tests/config.xobj created

You may now customize the machine.h header file for this UNIX
dialect.  The customizations will take effect when you compile
lsof.  You may also choose to skip customization and proceed to
the compilation of lsof.

If you don't know if you need to customize or want to know more
about what you can customize, consult the 00DCACHE, 00FAQ, 00PORTING,
and 00README files of the lsof distribution.  You might also find
it helpful to examine the machine.h header file for the dialect
you're customizing.

You don't need to use this procedure to customize lsof; you can
edit the machine.h header file directly.  If you later decide you
want to use this procedure to customize machine.h, execute the
./Customize script.
Do you want to customize (y|n) [y]? #

执行make得到如下结果:

(cd lib; /Applications/Xcode.app/Contents/Developer/usr/bin/make DEBUG="-O" CFGF="-DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"")
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o ckkv.o ckkv.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o cvfs.o cvfs.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o dvch.o dvch.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o fino.o fino.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o isfn.o isfn.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o lkud.o lkud.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o pdvn.o pdvn.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o prfp.o prfp.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o ptti.o ptti.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o rdev.o rdev.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o regex.o regex.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o rmnt.o rmnt.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o rnam.o rnam.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o rnch.o rnch.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o rnmh.o rnmh.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR="16.7.0"  -O   -c -o snpf.o snpf.c
ar cr liblsof.a ckkv.o cvfs.o dvch.o fino.o isfn.o lkud.o pdvn.o prfp.o ptti.o rdev.o regex.o rmnt.o rnam.o rnch.o rnmh.o snpf.o
ranlib liblsof.a
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o ddev.o ddev.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o dfile.o dfile.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o dmnt.o dmnt.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o dproc.o dproc.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o dsock.o dsock.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o dstore.o dstore.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o arg.o arg.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o main.o main.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o misc.o misc.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o node.o node.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o print.o print.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o proc.o proc.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o store.o store.c
Constructing version.h
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o usage.o usage.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o util.o util.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o dnetpolicy.o dnetpolicy.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o dchannel.o dchannel.c
dchannel.c:52:7: error: use of undeclared identifier 'PROC_CHANNEL_TYPE_USER_PIPE'
        case PROC_CHANNEL_TYPE_USER_PIPE:
             ^
dchannel.c:55:7: error: use of undeclared identifier 'PROC_CHANNEL_TYPE_KERNEL_PIPE'
        case PROC_CHANNEL_TYPE_KERNEL_PIPE:
             ^
dchannel.c:58:7: error: use of undeclared identifier 'PROC_CHANNEL_TYPE_NET_IF'
        case PROC_CHANNEL_TYPE_NET_IF:
             ^
dchannel.c:61:7: error: use of undeclared identifier 'PROC_CHANNEL_TYPE_FLOW_SWITCH'
        case PROC_CHANNEL_TYPE_FLOW_SWITCH:
             ^
dchannel.c:78:16: error: use of undeclared identifier 'PROC_CHANNEL_FLAGS_MONITOR'
        if ((flags & (PROC_CHANNEL_FLAGS_MONITOR)) != 0) {
                      ^
dchannel.c:80:17: error: use of undeclared identifier 'PROC_CHANNEL_FLAGS_MONITOR'
                if ((flags & (PROC_CHANNEL_FLAGS_MONITOR))
                              ^
dchannel.c:81:10: error: use of undeclared identifier 'PROC_CHANNEL_FLAGS_MONITOR'
                    == PROC_CHANNEL_FLAGS_MONITOR) {
                       ^
dchannel.c:83:23: error: use of undeclared identifier 'PROC_CHANNEL_FLAGS_MONITOR_TX'
                } else if ((flags & PROC_CHANNEL_FLAGS_MONITOR_TX) != 0) {
                                    ^
dchannel.c:88:16: error: use of undeclared identifier 'PROC_CHANNEL_FLAGS_MONITOR_NO_COPY'
                if ((flags & PROC_CHANNEL_FLAGS_MONITOR_NO_COPY) != 0) {
                             ^
dchannel.c:93:15: error: use of undeclared identifier 'PROC_CHANNEL_FLAGS_EXCLUSIVE'
        if ((flags & PROC_CHANNEL_FLAGS_EXCLUSIVE) != 0) {
                     ^
dchannel.c:102:15: error: use of undeclared identifier 'PROC_CHANNEL_FLAGS_USER_PACKET_POOL'
        if ((flags & PROC_CHANNEL_FLAGS_USER_PACKET_POOL) != 0) {
                     ^
dchannel.c:123:24: error: variable has incomplete type 'struct channel_fdinfo'
        struct channel_fdinfo ci;
                              ^
dchannel.c:123:9: note: forward declaration of 'struct channel_fdinfo'
        struct channel_fdinfo ci;
               ^
dchannel.c:131:31: error: use of undeclared identifier 'PROC_PIDFDCHANNELINFO'
        nb = proc_pidfdinfo(pid, fd, PROC_PIDFDCHANNELINFO, &ci, sizeof(ci));

如上,下面着手解决这些错误,这些错误都是类型和值未定义,其实可以通过逆向mac自带lsof来还原,这里为了简便,直接将dchannel.c这些发生错误语句删掉,最终dchannel.c中的代码如下:

#include "lsof.h"
void process_channel(int pid, int32_t fd){}

再次执行make

(cd lib; /Applications/Xcode.app/Contents/Developer/usr/bin/make DEBUG="-O" CFGF="-DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"")
make[1]: Nothing to be done for `all'.
Constructing version.h
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o usage.o usage.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o dchannel.o dchannel.c
cc  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o dnexus.o dnexus.c
cc -o lsof  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O ddev.o dfile.o dmnt.o dproc.o dsock.o dstore.o arg.o main.o misc.o node.o print.o proc.o store.o usage.o util.o dnetpolicy.o dchannel.o dnexus.o -L./lib -llsof  -lcurses

成功编译lsof的x86_64版本,可以直接在Mac上运行。下面来编译arm/arm64的版本make CC="$(xcrun --sdk iphoneos --find clang) -isysroot $(xcrun --sdk iphoneos --show-sdk-path) -arch armv7 -arch armv7s -arch arm64"得到如下结果:

(cd lib; /Applications/Xcode.app/Contents/Developer/usr/bin/make DEBUG="-O" CFGF="-DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"")
make[1]: Nothing to be done for `all'.
Constructing version.h
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.0.sdk -arch armv7 -arch armv7s -arch arm64  -DHASIPv6 -mdynamic-no-pic -DHASUTMPX -DDARWINV=1600 -DHAS_STRFTIME -DLSOF_VSTR=\"16.7.0\"  -O   -c -o usage.o usage.c
In file included from usage.c:39:
In file included from ./lsof.h:213:
./dlsof.h:56:10: fatal error: 'netinet/tcp_fsm.h' file not found
#include <netinet/tcp_fsm.h>
         ^~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [usage.o] Error 1

该错误是iphone对应SDK /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/缺少头文件导致,这些缺少的头文件从/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include拷贝即可,这些文件包括:

netinet/tcp_fsm.h
rpc/pmap_prot.h
libproc.h
sys/proc_info.h
sys/kern_control.h
net/route.h

再次make此时编译成功,执行file命令:

lsof: Mach-O universal binary with 3 architectures: [arm_v7: Mach-O executable arm_v7] [arm_v7s: Mach-O executable arm_v7s] [arm64: Mach-O 64-bit executable arm64]
lsof (for architecture armv7):  Mach-O executable arm_v7
lsof (for architecture armv7s): Mach-O executable arm_v7s
lsof (for architecture arm64):  Mach-O 64-bit executable arm64

拷贝到越狱iphone上执行,会出现killed: 9,需要对lsof进行签名:
创建文件entitlement.xml

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
     <key>com.apple.springboard.debugapplications</key>
     <true/>
     <key>get-task-allow</key>
     <true/>
     <key>task_for_pid-allow</key>
     <true/>
     <key>run-unsigned-code</key>
     <true/>
</dict>
</plist>

执行签名:ldid -Sentitlement.xml lsof

此时再将lsof拷贝到越狱iphone上,可以执行了!

我自己用的放在git上https://github.com/lichao890427/codesegment/blob/master/lsof_10.x

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,590评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,808评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,151评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,779评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,773评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,656评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,022评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,678评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,038评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,756评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,411评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,005评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,973评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,053评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,495评论 2 343

推荐阅读更多精彩内容