如何列出iOS中的开放端口? [英] How to list open ports in iOS?
问题描述
我正在使用 *越狱设备,我想编写应用程序以列出开放端口(例如TCP端口)。
我有两个想法:
- 使用一些原生API获取已打开端口的列表
- 执行shell命令以获取已打开端口的列表并解析此shell命令的结果。
哪个API或我应该使用shell命令以及如何以编程方式触发它?
我这样做的方法是让你的应用程序调用以编程方式运行UNIX lsof
命令。 lsof
列出打开的文件,这些文件在BSD系统上包含套接字,其中包括TCP套接字。
使用它Saurik发布了Cydia上提供的 lsof
版本。不幸的是,我最近无法上班。您可以自己尝试一下,因为Saurik的软件通常应该值得信赖。您也可以尝试自己编译 lsof
源代码,因为它可以在线获得。
但是,我发现这里有关于此的讨论主题。用户Firewire888能够在iOS上获得 lsof
的自制版本。如果您信任他们的工作,那么您可以下载二进制文件这里。根据他们的指示:
- 在mac osx下载ldid for macosx。 https://networkpx.googlecode.com/files/ldid
- 在mac osx上下载 scaner的lsof版本线程。再次感谢!
- 在mac osx上运行
ldid -S lsof
- scp或者其他任何方式在iPhone上获取
lsof
到/ usr / sbin / lsof
(必须在/ usr / sbin中获取错误无法分叉)
所以,你需要伪造代码签名版本 lsof
然后将其安装在iPhone上 / usr / sbin / lsof
。
然后,您可以使用私有API NSTask
(OS X上的公共)从您的应用程序中运行shell命令,并捕获例如,使用命令:
lsof -i4tcp
将列出所有IPv4 TCP端口。
在你的Objective-C代码中,你要这样做:
#includeNSTask.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];
[任务启动];
NSData * data = [file readDataToEndOfFile];
NSString * output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@tcp ports:\ n%@,输出);
}
这需要下载 NSTask
标题,您可以在这里找到。
给了我标准输出:
9月11日18:53:47 iPhone5 HelloJB [34535]<警告>:tcp端口:
命令PID用户FD类型设备大小/关闭节点名称
apsd 143移动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)
dataac ces 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->stoveroverflow.com:http(ESTABLISHED)
天气33191移动5u IPv4 0x12345678 0t0 TCP 192.168.4.119:50181->yts2.yql.vip.gq1.yahoo.com:http(LAST_ACK)
天气33191移动7u IPv4 0x12345678 0t0 TCP 192.168.4.119:50182-> yts2.yql.vip.gq1.yahoo.com:http(LAST_ACK)
天气33191手机8u IPv4 0x12345678 0t0 TCP 192.168.4.119:50181->yts2。 yql.vip.gq1.yahoo.com:http(LAST_ACK)
天气33191移动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)
你可以选择使用不同的命令行选项,和/或解析输出,以满足您的需求。祝你好运!
I am using a *jailbroken device and I want to write an application to list the open ports (e.g. TCP ports).
I have two ideas:
- Use some native API to get list of opened ports
- execute shell command to get a list of opened ports and parse the result of this shell command.
Which API or shell command should I use and how can I trigger it programmatically?
The way I would do this is to have your app invoke the UNIX lsof
command programmatically. lsof
lists open "files", which on a BSD system includes sockets, which includes TCP sockets.
It used to be that Saurik published a version of lsof
that was available on Cydia. Unfortunately, I haven't been able to get that to work recently. You might try it yourself, as software from Saurik should generally be trustworthy. You could also try compiling the lsof
source code yourself, as it's available online.
However, I found a discussion thread about this here. User Firewire888 was able to get a homebuilt version of lsof
working on iOS. If you trust their work, then you can download the binary file here. Per their instructions:
- On mac osx download ldid for macosx. https://networkpx.googlecode.com/files/ldid
- On mac osx download scaner's version of lsof in this thread. Thanks again!
- On mac osx run
ldid -S lsof
- scp or whatever means get
lsof
to/usr/sbin/lsof
on iPhone ( has to be in /usr/sbin otherwise get error can't fork )
So, you need to fake codesign that version of lsof
and then install it on your iPhone at /usr/sbin/lsof
.
Then, you can use the Private API NSTask
(public on OS X) to run the shell command from within your app, and capture the output.
For example, using the command:
lsof -i4tcp
will list all IPv4 TCP ports.
In your Objective-C code, you'd do this:
#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);
}
This requires downloading the NSTask
header, which you can find here.
which gave me the standard output:
Sep 11 18:53:47 iPhone5 HelloJB[34535] <Warning>: 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)
You can choose to use different command line options, and/or parse the output, to suit your needs. Good luck!
这篇关于如何列出iOS中的开放端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!