如何列出iOS中的开放端口?
我正在使用*越狱设备,我想编写一个应用程序列出开放端口(例如TCP端口)。
我有两个想法:
- 使用一些本机API获取已打开端口的列表
- 执行shell命令获取已打开端口的列表并解析此shell命令的结果。
我应该使用哪个API或shell命令以及如何以编程方式触发它?
我这样做的方法是让你的应用程序以编程方式调用UNIX lsof
命令。 lsof
列出打开的“文件”,它在BSD系统上包括套接字,其中包括TCP套接字。
过去,Saurik发布了Cydia上提供的lsof
版本。 不幸的是,我最近无法上class。 您可以自己尝试一下,因为Saurik的软件通常应该值得信赖。 您也可以尝试自己编译lsof
源代码,因为它可以在线获得。
但是,我在这里找到了一个关于此的讨论主题 。 用户Firewire888能够在iOS上获得lsof
的自制版本。 如果您信任他们的工作,那么您可以在此处下载二进制文件 。 根据他们的指示:
- 在mac osx上下载ldid for macosx。 https://networkpx.googlecode.com/files/ldid
- 在mac osx上下载scaner的lsof版本在这个post中 。 再次感谢!
- 在mac osx上运行
ldid -S lsof
- scp或者其他什么意思在iPhone上获取
lsof
到/usr/sbin/lsof
(必须在/ usr / sbin中否则得到错误无法分叉 )
所以,你需要伪造codeign那个版本的lsof
,然后将它安装在你的iPhone /usr/sbin/lsof
。
然后,您可以使用私有API NSTask
(OS X上的公共)从您的应用程序中运行shell命令,并捕获输出。
例如,使用命令:
lsof -i4tcp
将列出所有IPv4 TCP端口。
在Objective-C代码中,您可以这样做:
#include "NSTask.h" - (void) listTcpPorts { NSTask *task = [[NSTask alloc] init]; [task setLaunchPath: @"/usr/sbin/lsof"]; [task setArguments: [[NSArray alloc] initWithObjects: @"-i4tcp", nil]]; NSPipe *pipe= [NSPipe pipe]; [task setStandardOutput: pipe]; NSFileHandle *file = [pipe fileHandleForReading]; [task launch]; NSData *data = [file readDataToEndOfFile]; NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog(@"tcp ports: \n %@", output); }
这需要下载NSTask
标头, 您可以在此处找到它 。
这给了我标准输出:
Sep 11 18:53:47 iPhone5 HelloJB[34535] : tcp ports: COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME apsd 143 mobile 9u IPv4 0x12345678 0t0 TCP 10.244.7.127:51216->17.172.238.202:5223 (ESTABLISHED) apsd 143 mobile 10u IPv4 0x12345678 0t0 TCP 10.244.7.127:51215->17.149.37.18:5223 (ESTABLISHED) apsd 143 mobile 12u IPv4 0x12345678 0t0 TCP 10.244.7.127:51215->17.149.37.18:5223 (ESTABLISHED) apsd 143 mobile 14u IPv4 0x12345678 0t0 TCP 10.244.7.127:51216->17.172.238.202:5223 (ESTABLISHED) dataacces 166 mobile 25u IPv4 0x12345678 0t0 TCP 10.244.7.127:51276->pc-in-f193.1e100.net:https (ESTABLISHED) dataacces 166 mobile 27u IPv4 0x12345678 0t0 TCP 10.244.7.127:51276->pc-in-f193.1e100.net:https (ESTABLISHED) afcd 26764 mobile 9u IPv4 0x12345678 0t0 TCP localhost:51284->localhost:51285 (ESTABLISHED) MobileSaf 33165 mobile 11u IPv4 0x12345678 0t0 TCP 192.168.4.119:51349->stackoverflow.com:http (ESTABLISHED) MobileSaf 33165 mobile 12u IPv4 0x12345678 0t0 TCP 192.168.4.119:51349->stackoverflow.com:http (ESTABLISHED) Weather 33191 mobile 5u IPv4 0x12345678 0t0 TCP 192.168.4.119:50181->yts2.yql.vip.gq1.yahoo.com:http (LAST_ACK) Weather 33191 mobile 7u IPv4 0x12345678 0t0 TCP 192.168.4.119:50182->yts2.yql.vip.gq1.yahoo.com:http (LAST_ACK) Weather 33191 mobile 8u IPv4 0x12345678 0t0 TCP 192.168.4.119:50181->yts2.yql.vip.gq1.yahoo.com:http (LAST_ACK) Weather 33191 mobile 10u IPv4 0x12345678 0t0 TCP 192.168.4.119:50182->yts2.yql.vip.gq1.yahoo.com:http (LAST_ACK) notificat 33929 mobile 4u IPv4 0x12345678 0t0 TCP localhost:51295->localhost:51296 (ESTABLISHED) notificat 33929 mobile 5u IPv4 0x12345678 0t0 TCP localhost:51301->localhost:51302 (ESTABLISHED) notificat 33929 mobile 6u IPv4 0x12345678 0t0 TCP localhost:51318->localhost:51319 (ESTABLISHED) notificat 33929 mobile 7u IPv4 0x12345678 0t0 TCP localhost:51330->localhost:51331 (ESTABLISHED) syslog_re 34468 mobile 3u IPv4 0x12345678 0t0 TCP localhost:51321->localhost:51322 (ESTABLISHED)
您可以选择使用不同的命令行选项,和/或解析输出,以满足您的需求。 祝你好运!
- 单击时未调用UITableView didSelectRowatIndexPath
- 为什么在主线程上绘制UI?
- “此捆绑包无效 – 文件扩展名必须是.zip”提交给iTunes的错误
- 用像Instagram这样的“更多”button展开UITableView中的UILabel
- 越界boundsubstringFromRange的问题
- NSXMLParser如何将NSMutableDictionary传递给NSMutableArray
- 如何在iPhone中点击更改tableView单元格button
- 在Xcode iOS模拟器上获取旧的操作系统版本
- 通过prepareforsegue传递ManagedObjectContext