Linux CentOS7系统,抓取http协议的数据包

使用 tcpdump 命令

1.首先确认是否安装

[root@localhost ~]# which tcpdump
/usr/bin/which: no tcpdump in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]#

我这里没有安装

1.1 安装 tcpdump

yum install tcpdump
安装成功如下:

[root@localhost ~]# yum install tcpdump
已加载插件:fastestmirror
Bad id for repo: mirrors.aliyun.com_docker-ce_linux_centos_docker-ce.xn--repo()-mu8ig98h4l0hokj, byte = ( 60
Loading mirror speeds from cached hostfile
* epel: mirror.nyist.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 tcpdump.x86_64.14.4.9.2-4.el7_7.1 将被 安装
--> 正在处理依赖关系 libpcap >= 14:1.5.3-10,它被软件包 14:tcpdump-4.9.2-4.el7_7.1.x86_64 需要
--> 正在处理依赖关系 libpcap.so.1()(64bit),它被软件包 14:tcpdump-4.9.2-4.el7_7.1.x86_64 需要
--> 正在检查事务
---> 软件包 libpcap.x86_64.14.1.5.3-13.el7_9 将被 安装
--> 解决依赖关系完成

依赖关系解决

====================================================================================================================================================================================
Package                                 架构                                   版本                                                  源                                       大小
====================================================================================================================================================================================
正在安装:
tcpdump                                 x86_64                                 14:4.9.2-4.el7_7.1                                    base                                    422 k
为依赖而安装:
libpcap                                 x86_64                                 14:1.5.3-13.el7_9                                     updates                                 139 k

事务概要
====================================================================================================================================================================================
安装  1 软件包 (+1 依赖软件包)

总下载量:560 k
安装大小:1.3 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): libpcap-1.5.3-13.el7_9.x86_64.rpm                                                                                                                     | 139 kB  00:00:00
(2/2): tcpdump-4.9.2-4.el7_7.1.x86_64.rpm                                                                                                                    | 422 kB  00:00:00
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                                2.3 MB/s | 560 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
 正在安装    : 14:libpcap-1.5.3-13.el7_9.x86_64                                                                                                                                1/2
 正在安装    : 14:tcpdump-4.9.2-4.el7_7.1.x86_64                                                                                                                               2/2
 验证中      : 14:libpcap-1.5.3-13.el7_9.x86_64                                                                                                                                1/2
 验证中      : 14:tcpdump-4.9.2-4.el7_7.1.x86_64                                                                                                                               2/2

已安装:
 tcpdump.x86_64 14:4.9.2-4.el7_7.1

作为依赖被安装:
 libpcap.x86_64 14:1.5.3-13.el7_9

完毕!
[root@localhost ~]#

2.使用教程

先确认要监听的网卡,我这里是虚拟机,是ens33
因此命令:

# http捕获的信息保存到文件
tcpdump -i ens33 port 80 -Xx -w capture.pcap

# 指定端口为80,这里捕获的信息打印在 了命令行,不利于分析
tcpdump -i ens33 port 80 -Xx

在这里插入图片描述

2.1 使用Python对抓包数据进行分析

抓包的结果通常是一个二进制格式的文件,我们可以使用Python中的第三方库dpkt对抓包数据进行分析。dpkt是一个功能强大的网络数据包处理库,可以解析和操作网络协议头部以及数据。

首先,我们需要安装dpkt库:

pip install dpkt
import dpkt

# 从文件中读取抓包数据
with open('capture.pcap', 'rb') as f:
    pcap = dpkt.pcap.Reader(f)

    # 遍历每个数据包
    for timestamp, buf in pcap:
        # 解析以太网帧
        eth = dpkt.ethernet.Ethernet(buf)

        # 解析IP数据报
        if isinstance(eth.data, dpkt.ip.IP):
            ip = eth.data

            # 解析TCP数据报
            if isinstance(ip.data, dpkt.tcp.TCP):
                tcp = ip.data

                # 输出源IP地址、目的IP地址和目的端口号
                print(f'Source IP: {
      
      dpkt.inet.inet_ntoa(ip.src)}')
                print(f'Destination IP: {
      
      dpkt.inet.inet_ntoa(ip.dst)}')
                print(f'Destination Port: {
      
      tcp.dport}')

我这里捕获的数据:

[root@localhost ~]#  tcpdump -i ens33 port 80 -Xx
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
09:18:57.433624 IP 10.0.1.64.openmailpxy > localhost.localdomain.http: Flags [S], seq 755891672, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
        0x0000:  4500 0034 b0e6 4000 8006 3389 0a00 0140  E..4..@...3....@
        0x0010:  0a00 0115 1688 0050 2d0d fdd8 0000 0000  .......P-.......
        0x0020:  8002 faf0 1c0d 0000 0204 05b4 0103 0308  ................
        0x0030:  0101 0402                                ....
09:18:57.433648 IP localhost.localdomain.http > 10.0.1.64.openmailpxy: Flags [S.], seq 3674812333, ack 755891673, win 29200, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
        0x0000:  4500 0034 0000 4000 4006 2470 0a00 0115  E..4..@.@.$p....
        0x0010:  0a00 0140 0050 1688 db09 2fad 2d0d fdd9  ..[email protected]..../.-...
        0x0020:  8012 7210 167b 0000 0204 05b4 0101 0402  ..r..{
    
    ..........
        0x0030:  0103 0307                                ....
09:18:57.433679 IP 10.0.1.64.spramsca > localhost.localdomain.http: Flags [S], seq 1554988307, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
        0x0000:  4500 0034 b0e7 4000 8006 3388 0a00 0140  E..4..@...3....@
        0x0010:  0a00 0115 1689 0050 5caf 3d13 0000 0000  .......P\.=.....
        0x0020:  8002 faf0 ad2f 0000 0204 05b4 0103 0308  ...../..........
        0x0030:  0101 0402                                ....
09:18:57.433684 IP localhost.localdomain.http > 10.0.1.64.spramsca: Flags [S.], seq 1632946978, ack 1554988308, win 29200, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
        0x0000:  4500 0034 0000 4000 4006 2470 0a00 0115  E..4..@.@.$p....
        0x0010:  0a00 0140 0050 1689 6154 cb22 5caf 3d14  ..[email protected]..aT."\.=.
        0x0020:  8012 7210 167b 0000 0204 05b4 0101 0402  ..r..{..........
        0x0030:  0103 0307                                ....
09:18:57.434513 IP 10.0.1.64.openmailpxy > localhost.localdomain.http: Flags [.], ack 1, win 513, length 0
        0x0000:  4500 0028 b0e8 4000 8006 3393 0a00 0140  E..([email protected]....@
        0x0010:  0a00 0115 1688 0050 2d0d fdd9 db09 2fae  .......P-...../.
        0x0020:  5010 0201 4b08 0000 0000 0000 0000       P...K.........
09:18:57.434535 IP 10.0.1.64.spramsca > localhost.localdomain.http: Flags [.], ack 1, win 513, length 0
        0x0000:  4500 0028 b0e9 4000 8006 3392 0a00 0140  E..([email protected]....@
        0x0010:  0a00 0115 1689 0050 5caf 3d14 6154 cb23  .......P\.=.aT.#
        0x0020:  5010 0201 ba6a 0000 0000 0000 0000       P....j........
09:18:57.434538 IP 10.0.1.64.spramsca > localhost.localdomain.http: Flags [P.], seq 1:468, ack 1, win 513, length 467: HTTP: GET /trdl/system/GetSupportVer.action?t=88888888 HTTP/1.1
        0x0000:  4500 01fb b0ea 4000 8006 31be 0a00 0140  [email protected]....@
        0x0010:  0a00 0115 1689 0050 5caf 3d14 6154 cb23  .......P\.=.aT.#
        0x0020:  5018 0201 4a2a 0000 4745 5420 2f74 7264  P...J*..GET./trd
        0x0030:  6c2f 7379 7374 656d 2f47 6574 5375 7070  l/system/GetSupp
        0x0040:  6f72 7456 6572 2e61 6374 696f 6e3f 743d  ortVer.action?t=
        0x0050:  3838 3838 3838 3838 2048 5454 502f 312e  88888888.HTTP/1.
        0x0060:  310d 0a48 6f73 743a 2031 302e 302e 312e  1..Host:.10.0.1.
        0x0070:  3231 0d0a 436f 6e6e 6563 7469 6f6e 3a20  21..Connection:.
        0x0080:  6b65 6570 2d61 6c69 7665 0d0a 5570 6772  keep-alive..Upgr
        0x0090:  6164 652d 496e 7365 6375 7265 2d52 6571  ade-Insecure-Req
        0x00a0:  7565 7374 733a 2031 0d0a 5573 6572 2d41  uests:.1..User-A
        0x00b0:  6765 6e74 3a20 4d6f 7a69 6c6c 612f 352e  gent:.Mozilla/5.
        0x00c0:  3020 2857 696e 646f 7773 204e 5420 3130  0.(Windows.NT.10
        0x00d0:  2e30 3b20 5769 6e36 343b 2078 3634 2920  .0;.Win64;.x64).
        0x00e0:  4170 706c 6557 6562 4b69 742f 3533 372e  AppleWebKit/537.
        0x00f0:  3336 2028 4b48 544d 4c2c 206c 696b 6520  36.(KHTML,.like.
        0x0100:  4765 636b 6f29 2043 6872 6f6d 652f 3131  Gecko).Chrome/11
        0x0110:  362e 302e 302e 3020 5361 6661 7269 2f35  6.0.0.0.Safari/5
        0x0120:  3337 2e33 360d 0a41 6363 6570 743a 2074  37.36..Accept:.t
        0x0130:  6578 742f 6874 6d6c 2c61 7070 6c69 6361  ext/html,applica
        0x0140:  7469 6f6e 2f78 6874 6d6c 2b78 6d6c 2c61  tion/xhtml+xml,a
        0x0150:  7070 6c69 6361 7469 6f6e 2f78 6d6c 3b71  pplication/xml;q
        0x0160:  3d30 2e39 2c69 6d61 6765 2f61 7669 662c  =0.9,image/avif,
        0x0170:  696d 6167 652f 7765 6270 2c69 6d61 6765  image/webp,image
        0x0180:  2f61 706e 672c 2a2f 2a3b 713d 302e 382c  /apng,*/*;q=0.8,
        0x0190:  6170 706c 6963 6174 696f 6e2f 7369 676e  application/sign
        0x01a0:  6564 2d65 7863 6861 6e67 653b 763d 6233  ed-exchange;v=b3
        0x01b0:  3b71 3d30 2e37 0d0a 4163 6365 7074 2d45  ;q=0.7..Accept-E
        0x01c0:  6e63 6f64 696e 673a 2067 7a69 702c 2064  ncoding:.gzip,.d
        0x01d0:  6566 6c61 7465 0d0a 4163 6365 7074 2d4c  eflate..Accept-L
        0x01e0:  616e 6775 6167 653a 207a 682d 434e 2c7a  anguage:.zh-CN,z
        0x01f0:  683b 713d 302e 390d 0a0d 0a              h;q=0.9....
09:18:57.434541 IP localhost.localdomain.http > 10.0.1.64.spramsca: Flags [.], ack 468, win 237, length 0
        0x0000:  4500 0028 397a 4000 4006 eb01 0a00 0115  E..(9z@.@.......
        0x0010:  0a00 0140 0050 1689 6154 cb23 5caf 3ee7  [email protected].#\.>.
        0x0020:  5010 00ed 166f 0000                      P....o..
09:18:57.446525 IP localhost.localdomain.http > 10.0.1.64.spramsca: Flags [P.], seq 1:480, ack 468, win 237, length 479: HTTP: HTTP/1.1 200 OK
        0x0000:  4500 0207 397b 4000 4006 e921 0a00 0115  E...9{@.@..!....
        0x0010:  0a00 0140 0050 1689 6154 cb23 5caf 3ee7  [email protected].#\.>.
        0x0020:  5018 00ed 184e 0000 4854 5450 2f31 2e31  P....N..HTTP/1.1
        0x0030:  2032 3030 204f 4b0d 0a58 2d50 6f77 6572  .200.OK..X-Power
        0x0040:  6564 2d42 793a 2045 7870 7265 7373 0d0a  ed-By:.Express..
        0x0050:  5661 7279 3a20 4f72 6967 696e 2c20 4163  Vary:.Origin,.Ac
        0x0060:  6365 7074 2d45 6e63 6f64 696e 670d 0a41  cept-Encoding..A
        0x0070:  6363 6573 732d 436f 6e74 726f 6c2d 416c  ccess-Control-Al
        0x0080:  6c6f 772d 4372 6564 656e 7469 616c 733a  low-Credentials:
        0x0090:  2074 7275 650d 0a43 6163 6865 2d43 6f6e  .true..Cache-Con
        0x00a0:  7472 6f6c 3a20 6e6f 2d63 6163 6865 0d0a  trol:.no-cache..
        0x00b0:  5072 6167 6d61 3a20 6e6f 2d63 6163 6865  Pragma:.no-cache
        0x00c0:  0d0a 4578 7069 7265 733a 202d 310d 0a58  ..Expires:.-1..X
        0x00d0:  2d43 6f6e 7465 6e74 2d54 7970 652d 4f70  -Content-Type-Op
        0x00e0:  7469 6f6e 733a 206e 6f73 6e69 6666 0d0a  tions:.nosniff..
        0x00f0:  436f 6e74 656e 742d 5479 7065 3a20 6170  Content-Type:.ap
        0x0100:  706c 6963 6174 696f 6e2f 6a73 6f6e 3b20  plication/json;.
        0x0110:  6368 6172 7365 743d 7574 662d 380d 0a43  charset=utf-8..C
        0x0120:  6f6e 7465 6e74 2d4c 656e 6774 683a 2038  ontent-Length:.8
        0x0130:  340d 0a45 5461 673a 2057 2f22 3534 2d66  4..ETag:.W/"54-f
        0x0140:  7432 656e 7372 422b 4a64 3734 6773 2b6a  t2ensrB+Jd74gs+j
        0x0150:  795a 576c 6172 724f 5641 220d 0a44 6174  yZWlarrOVA"..Dat
        0x0160:  653a 2046 7269 2c20 3235 2041 7567 2032  e:.Fri,.25.Aug.2
        0x0170:  3032 3320 3031 3a31 383a 3537 2047 4d54  023.01:18:57.GMT
        0x0180:  0d0a 436f 6e6e 6563 7469 6f6e 3a20 6b65  ..Connection:.ke
        0x0190:  6570 2d61 6c69 7665 0d0a 4b65 6570 2d41  ep-alive..Keep-A
        0x01a0:  6c69 7665 3a20 7469 6d65 6f75 743d 350d  live:.timeout=5.
        0x01b0:  0a0d 0a5b 0a20 207b 0a20 2020 2022 6964  ...[...{....."id
        0x01c0:  223a 2035 2c0a 2020 2020 2263 6f6d 223a  ":.5,....."com":
        0x01d0:  2022 e585 89e5 ae87 222c 0a20 2020 2022  ."......",....."
        0x01e0:  7665 7222 3a20 2276 312e 3022 2c0a 2020  ver":."v1.0",...
        0x01f0:  2020 2273 7563 6365 7373 223a 2074 7275  .."success":.tru
        0x0200:  650a 2020 7d0a 5d                        e...}.]
09:18:57.494793 IP 10.0.1.64.spramsca > localhost.localdomain.http: Flags [.], ack 480, win 511, length 0
        0x0000:  4500 0028 b0eb 4000 8006 3390 0a00 0140  E..(..@...3....@
        0x0010:  0a00 0115 1689 0050 5caf 3ee7 6154 cd02  .......P\.>.aT..
        0x0020:  5010 01ff b6ba 0000 0000 0000 0000       P.............
09:19:02.451444 IP localhost.localdomain.http > 10.0.1.64.spramsca: Flags [F.], seq 480, ack 468, win 237, length 0
        0x0000:  4500 0028 397c 4000 4006 eaff 0a00 0115  E..(9|@.@.......
        0x0010:  0a00 0140 0050 1689 6154 cd02 5caf 3ee7  ..[email protected]..aT..\.>.
        0x0020:  5011 00ed 166f 0000                      P....o..
09:19:02.452513 IP 10.0.1.64.spramsca > localhost.localdomain.http: Flags [.], ack 481, win 511, length 0
        0x0000:  4500 0028 b102 4000 8006 3379 0a00 0140  E..(..@...3y...@
        0x0010:  0a00 0115 1689 0050 5caf 3ee7 6154 cd03  .......P\.>.aT..
        0x0020:  5010 01ff b6b9 0000 0000 0000 0000       P.............
09:19:04.737027 IP 10.0.1.64.spramsca > localhost.localdomain.http: Flags [F.], seq 468, ack 481, win 511, length 0
        0x0000:  4500 0028 b10c 4000 8006 336f 0a00 0140  E..(..@...3o...@
        0x0010:  0a00 0115 1689 0050 5caf 3ee7 6154 cd03  .......P\.>.aT..
        0x0020:  5011 01ff b6b8 0000 0000 0000 0000       P.............
09:19:04.737049 IP localhost.localdomain.http > 10.0.1.64.spramsca: Flags [.], ack 469, win 237, length 0
        0x0000:  4500 0028 fc58 4000 4006 2823 0a00 0115  E..(.X@.@.(#....
        0x0010:  0a00 0140 0050 1689 6154 cd03 5caf 3ee8  ..[email protected]..aT..\.>.
        0x0020:  5010 00ed b7ca 0000                      P.......



猜你喜欢

转载自blog.csdn.net/p309654858/article/details/132488046