iPhone上的网络活动监控

16

我花了5天时间学习并尝试在iPhone上实现网络监控。 我查看了来自苹果的netstat代码,结果才抓了25%的头发。

我找到了JB设备的链接,但我需要在非JB设备上执行它(无论苹果是否接受它在应用商店中)。

我找到了一些有用的链接:

如何获取iPhone上的TCP UDP开放端口列表(我无法解析这个问题返回的数据:( )

iPhone上的数据使用情况

sysctlbyname buf返回类型(我不是一个网络人员..无法理解这个,也许你们可以帮忙:))

TCP/UPD端口列表

我可以说我从第一个链接中得到了一些东西。你们能帮我解析数据吗? 还有其他方法可以实现这个吗?


所以基本上你想通过你的iPhone查看你的网络上正在发生的事情。 - Antp
你想解析如何在iPhone上获取TCP UDP开放端口列表的帖子响应吗?https://dev59.com/sl3Ua4cB1Zd3GeqP_10i?rq=1 即使苹果不接受,也无妨。 - dcorbatta
@dcorbatta - 是的,我可以接受这个..因为我不会将它上传到应用商店。 - Shrey
@Hisenberg,你看到我的示例项目了吗?你还需要什么更详细的信息吗?我会给你一个包含连接的字典。 - dcorbatta
@dcorbatta - 我正在看它... :) - Shrey
好的,如果你需要更多的帮助,请告诉我。 - dcorbatta
1个回答

11

好的,您已经拥有了桌子上所有需要的东西。

您可以查看问题“iPhone上是否有任何私有API可用于监视网络流量?”在这里您可以找到inet代码的源代码。该代码包含了解析返回的数据所需的全部内容,如何获取iPhone上的TCP UDP开放端口列表

size_t len = 0;
if (sysctlbyname("net.inet.tcp.pcblist_n", 0, &len, 0, 0) < 0) {
    perror("sysctlbyname");
} else {
    char *buf = malloc(len);
    sysctlbyname("net.inet.tcp.pcblist_n", buf, &len, 0, 0);
    NSData *data = [NSData dataWithBytesNoCopy:buf length:len];
    NSLog(@"data = %@", data);
}

好的,是的inet的源代码有点复杂。但是你知道netstat打印网络状态。因此,你只需要查看inet何时执行printf,在这一点上,您将解析出数据。

如果您尝试为iOS编译inet的源代码,您会发现很多问题:某些标头文件不存在于ios sdk中。好的,没问题,复制这些标头文件即可。

对于模拟器,您只需要复制netstat.h的标头文件。并添加一些私有结构声明:

struct  xtcpcb_n {
    u_int32_t                      xt_len;
    u_int32_t                        xt_kind;                /* XSO_TCPCB */

    u_int64_t t_segq;
    int     t_dupacks;              /* consecutive dup acks recd */

    int t_timer[TCPT_NTIMERS_EXT];  /* tcp timers */

    int     t_state;                /* state of this connection */
    u_int   t_flags;

    int     t_force;                /* 1 if forcing out a byte */

    tcp_seq snd_una;                /* send unacknowledged */
    tcp_seq snd_max;                /* highest sequence number sent;
                                     * used to recognize retransmits
                                     */
    tcp_seq snd_nxt;                /* send next */
    tcp_seq snd_up;                 /* send urgent pointer */

    tcp_seq snd_wl1;                /* window update seg seq number */
    tcp_seq snd_wl2;                /* window update seg ack number */
    tcp_seq iss;                    /* initial send sequence number */
    tcp_seq irs;                    /* initial receive sequence number */

    tcp_seq rcv_nxt;                /* receive next */
    tcp_seq rcv_adv;                /* advertised window */
    u_int32_t rcv_wnd;              /* receive window */
    tcp_seq rcv_up;                 /* receive urgent pointer */

    u_int32_t snd_wnd;              /* send window */
    u_int32_t snd_cwnd;             /* congestion-controlled window */
    u_int32_t snd_ssthresh;         /* snd_cwnd size threshold for
                                     * for slow start exponential to
                                     * linear switch
                                     */
    u_int   t_maxopd;               /* mss plus options */

    u_int32_t t_rcvtime;            /* time at which a packet was received */
    u_int32_t t_starttime;          /* time connection was established */
    int     t_rtttime;              /* round trip time */
    tcp_seq t_rtseq;                /* sequence number being timed */

    int     t_rxtcur;               /* current retransmit value (ticks) */
    u_int   t_maxseg;               /* maximum segment size */
    int     t_srtt;                 /* smoothed round-trip time */
    int     t_rttvar;               /* variance in round-trip time */

    int     t_rxtshift;             /* log(2) of rexmt exp. backoff */
    u_int   t_rttmin;               /* minimum rtt allowed */
    u_int32_t t_rttupdated;         /* number of times rtt sampled */
    u_int32_t max_sndwnd;           /* largest window peer has offered */

    int     t_softerror;            /* possible error not yet reported */
    /* out-of-band data */
    char    t_oobflags;             /* have some */
    char    t_iobc;                 /* input character */
    /* RFC 1323 variables */
    u_char  snd_scale;              /* window scaling for send window */
    u_char  rcv_scale;              /* window scaling for recv window */
    u_char  request_r_scale;        /* pending window scaling */
    u_char  requested_s_scale;
    u_int32_t ts_recent;            /* timestamp echo data */

    u_int32_t ts_recent_age;        /* when last updated */
    tcp_seq last_ack_sent;
    /* RFC 1644 variables */
    tcp_cc  cc_send;                /* send connection count */
    tcp_cc  cc_recv;                /* receive connection count */
    tcp_seq snd_recover;            /* for use in fast recovery */
    /* experimental */
    u_int32_t snd_cwnd_prev;        /* cwnd prior to retransmit */
    u_int32_t snd_ssthresh_prev;    /* ssthresh prior to retransmit */
    u_int32_t t_badrxtwin;          /* window for retransmit recovery */
};


struct  xinpcb_n {
    u_int32_t               xi_len;         /* length of this structure */
    u_int32_t               xi_kind;                /* XSO_INPCB */
    u_int64_t               xi_inpp;
    u_short                 inp_fport;      /* foreign port */
    u_short                 inp_lport;      /* local port */
    u_int64_t               inp_ppcb;       /* pointer to per-protocol pcb */
    inp_gen_t               inp_gencnt;     /* generation count of this instance */
    int                             inp_flags;      /* generic IP/datagram flags */
    u_int32_t               inp_flow;
    u_char                  inp_vflag;
    u_char                  inp_ip_ttl;     /* time to live */
    u_char                  inp_ip_p;       /* protocol */
    union {                                 /* foreign host table entry */
        struct  in_addr_4in6    inp46_foreign;
        struct  in6_addr        inp6_foreign;
    }                               inp_dependfaddr;
    union {                                 /* local host table entry */
        struct  in_addr_4in6    inp46_local;
        struct  in6_addr        inp6_local;
    }                               inp_dependladdr;
    struct {
        u_char          inp4_ip_tos;    /* type of service */
    }                               inp_depend4;
    struct {
        u_int8_t        inp6_hlim;
        int                     inp6_cksum;
        u_short         inp6_ifindex;
        short           inp6_hops;
    }                               inp_depend6;
    u_int32_t               inp_flowhash;
};


#define SO_TC_STATS_MAX 4

struct data_stats {
    u_int64_t       rxpackets;
    u_int64_t       rxbytes;
    u_int64_t       txpackets;
    u_int64_t       txbytes;
};

struct xgen_n {
    u_int32_t   xgn_len;            /* length of this structure */
    u_int32_t   xgn_kind;       /* number of PCBs at this time */
};

#define XSO_SOCKET  0x001
#define XSO_RCVBUF  0x002
#define XSO_SNDBUF  0x004
#define XSO_STATS   0x008
#define XSO_INPCB   0x010
#define XSO_TCPCB   0x020

struct  xsocket_n {
    u_int32_t       xso_len;        /* length of this structure */
    u_int32_t       xso_kind;       /* XSO_SOCKET */
    u_int64_t       xso_so; /* makes a convenient handle */
    short           so_type;
    u_int32_t       so_options;
    short           so_linger;
    short           so_state;
    u_int64_t       so_pcb;     /* another convenient handle */
    int             xso_protocol;
    int             xso_family;
    short           so_qlen;
    short           so_incqlen;
    short           so_qlimit;
    short           so_timeo;
    u_short         so_error;
    pid_t           so_pgid;
    u_int32_t       so_oobmark;
    uid_t           so_uid;     /* XXX */
};

struct xsockbuf_n {
    u_int32_t       xsb_len;        /* length of this structure */
    u_int32_t       xsb_kind;       /* XSO_RCVBUF or XSO_SNDBUF */
    u_int32_t       sb_cc;
    u_int32_t       sb_hiwat;
    u_int32_t       sb_mbcnt;
    u_int32_t       sb_mbmax;
    int32_t         sb_lowat;
    short           sb_flags;
    short           sb_timeo;
};

struct xsockstat_n {
    u_int32_t       xst_len;        /* length of this structure */
    u_int32_t       xst_kind;       /* XSO_STATS */
    struct data_stats   xst_tc_stats[SO_TC_STATS_MAX];
};

但是对于设备,您需要复制一些内核的其他头文件,我不知道为什么这些头文件不在设备sdk中(也许如果您使用它们,苹果将不会批准您的应用程序,我不知道,但这不重要)。

您可以从模拟器SDK中复制缺失的头文件到项目中。或者您可以更改头文件路径以指向模拟器SDK。(我复制了这些头文件)。
如果您复制了这些头文件,则需要更改一些包含声明。

您不需要全部的inet.c代码。您只需要以下几个函数:

  • protopr
  • inetprint
  • inetname

然后在这些函数中,您将看到解析数据的printf,您可以将其添加到字典中。

这里是我的代码。您可以看到一个名为DHInet的类,其中有两种方法,返回一个包含连接信息的NSDictionary列表的NSArray。

希望您觉得这很有用,如果有人想帮助我改进代码,那就太好了,我需要清理代码,因为它有很多不必要的ifdef。


我得到了类似于以下内容的东西: “键:外部地址,值:maa03s17-in-f31..https” - Shrey
我把它改成了“1”.. 结果还是一样的。 - Shrey
我把nflag改成了1,现在我可以获取外国IP地址... :) - Shrey
不,只需要nflag。我们能否也得到进程ID?我知道这不是问题的一部分,但你能指点我如何获得它吗? - Shrey
1
@dcorbatta,你有没有想过如何使用netstat按连接获取接口名称? - Legoless
显示剩余11条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接