linux学习:网络(防火墙)及系统安全相关命令学习

指令: top、htop、free、pstree、lsof、ifconfig、w3m、tcpdump、netstat、ufw

网络:

top      #查看内存,cpu,进程之间的状态。
htop        #在top的基础上更好显示(执行sudo apt-get install htop安装)
free     #查看当前的内存使用情况

free -m | grep Mem | awk '{print $2}'   #查看物理内存大小

free -m | grep Mem | awk '{print $3}'   #查看已经使用了的物理内存
pstree     #查看当前进程树


lsof file    #查看哪个进程打开了文件file
lsof -i :22   #查看22端口现在运行什么程序
lsof -c vim  #查看vim进行现在打开的文件

lsof -p [进程ID]    #根据进程id跟踪某个进程所使用的资源

ifconfig ens33 | egrep -o "inet addr:[^ ]*" | grep -o "[0-9.]*"    #提取本地ens33网卡的IP地址
ifconfig ens33 hw ether 00:cc:bf:5a:aa:dd    #设置MAC地址,在软件层面上进行硬件地址的欺骗
ifconfig ens33 192.168.0.12 netmask 255.255.252.0    #设置IP地址的子网掩码
ifconfig ens33 192.168.0.12    #设置网卡ens33的ip地址


cat /etc/resolv.conf #查看DNS
echo nameserver 8.8.8.8 >> /etc/resolve.conf    #追加DNS:8.8.8.8到/etc/resolve.conf的DNS地址文件中。
host google.com    #列出域名所有的IP地址
nslookup google.com    #查询DNS相关的细节信息以及名字解析
route 或 netstat -rn 或 sudo route -n    #查看路由表信息
ping ADDRESS    #检查某个主机是否可以到达 ADDRESS可以是IP,域名和主机名 
ping address -c 5     #选项-c 5表示限制发送的echo分组的数量为5,5次后自动停止发送

arping IP      #根据IP查网卡地址 
nmblookup -A IP  #根据IP查电脑名
arp -a | awk '{ print $4 }'    #查看当前网卡的物理地址
ifconfig eth0:0 1.2.3.4 netmask 255.255.255.0    #同一个网卡增加第二个IP地址
echo 'blacklist ipv6' | sudo tee /etc/modprobe.d/blacklist-ipv6    #屏蔽IPV6
whois test.com     #查看域名的注册备案情况
tracepath test.com   #查看到某一个域名的路由情况:

netstat -ntlp | grep 9052   #查看哪些进程在监听9052端口
netstat -na|grep :80|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -r -n    #统计80端口的连接并排序
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'    #查看网络连接状态
netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -r -n    #统计当前IP连接的个数

netstat -anp | grep "php-fpm" | grep "tcp" | grep "pool" | wc -l         #查看已经有多少个php-cgi进程用来处理tcp请求  

netstat -atnp    #察看当前网络连接状况以及程序

 

sudo tcpdump -c 10000 -i eth0 -n dst port 80   #TCP抓包工具分析80端口数据流

sudo tcpdump -i any tcp port 9501    # 参数 -i 制定了网卡,any表示所有网卡,tcp 指定仅监听TCP协议,port 制定监听的端口

sudo tcpdump tcp port 23 host 192.27.48.1    #获取主机192.27.48.1接收或发出的telnet包

sudo tcpdump host 210.27.48.1 and / (210.27.48.2 or 210.27.48.3 /)   #想要截获主机210.27.48.1 和主机210.27.48.2 或210.27.48.3的通信(在命令行中适用括号时,一定要转义)

sudo tcpdump ip host 210.27.48.1 and ! 210.27.48.2   #获取主机210.27.48.1除了和主机210.27.48.2之外所有主机通信的ip包

sudo tcpdump -i eth0 src host hostname    #系统将只对名为hostname的主机的通信数据包进行监视。主机名可以是本地主机,也可以是网络上的任何一台计算机.

sudo tcpdump -i eth0 dst host hostname  #监视所有送到主机hostname的数据包

sudo tcpdump -i eth0 gateway Gatewayname    #监视通过指定网关的数据包

 

nc -zv localhost 1-65535    #查看当前系统所有的监听端口


w3m -dump_head http://www.xxx.com    #查看HTTP头
w3m -no-cookie -dump www.123cha.com|grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'    #查看当前外网的IP地址


sudo apt-get install rkhunter; rkhunter –checkall    #检查本地是否存在安全隐患


----------------------------------------------------------------------
入侵报告工具 以auth.log文件为输入
filename:check.sh

#!/bin/bash
AUTHLOG=/var/log/auth.log
if [[ -n $1 ]];
then
AUTHLOG=$1
echo Using Log file:$AUTHLOG
fi
LOG=/tmp/valid.$$.log
grep -v "invalid" $AUTHLOG > $LOG
users=$(grep "Failed password" $LOG | awk '{ print $(NF-5) }' | sort | uniq)
printf "%-5s|%-10s|%-10s|%-13s|%-33s|%s\n" "Sr#" "User" "Attempts" "IP address" "Host_Mapping" "Time range"
ucount=0;
ip_list="$(egrep -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" $LOG | sort | uniq)"
for ip in $ip_list;
do
grep $ip $LOG > /tmp/temp.$$.log
for user in $users;
do
grep $user /tmp/temp.$$.log > /tmp/$$.log
cut -c-16 /tmp/$$.log > $$.time
tstart=$(head -1 $$.time);
start=$(date -d "$tstart" "+%s");
tend=$(tail -l $$.time);
end=$(date -d "$tend" "+%s")
limit=$(( $end - $start))
if [ $limit -gt 120 ];
then
let ucount++;
IP=$(egrep -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" /tmp/$$.log | head -1 );
TIME_RANGE="$start-->$tend"
ATTEMPTS=$(cat /tmp/$$.log|wc -l);
HOST=$(host $IP | awk '{ print $NF }' )
printf "%-5s|%-10s|%-10s|%-10s|%-33s|%-s\n" "$ucount" "$user" "$ATTEMPTS" "$IP" "$HOST" "$TIME_RANGE";
fi
done
done
rm /tmp/valid.$$.log /tmp/$$.log $$.time /tmp/temp.$$.log 2> /dev/null


-------------------------------------------------------------------------------

防火墙ufw

sudo apt-get install ufw   #安装ufw防火墙
sudo ufw enable              #启用 ufw防火墙,并在系统启动时自动开启

sudo ufw disable       #关闭ufw防火墙

sudo ufw status       #查看防火墙状态 

sudo ufw default deny         #关闭所有外部对本机的访问,但本机访问外部正常。
sudo ufw allow|deny [service]   #开启/禁用 
sudo ufw allow smtp       #允许所有的外部IP访问本机的25/tcp (smtp)端口 
sudo ufw allow 22/tcp       #允许所有的外部IP访问本机的22/tcp (ssh)端口 
sudo ufw allow 53         #允许外部访问53端口(tcp/udp) 

sudo ufw delete allow 53     #禁用 53 端口
sudo ufw allow from 192.168.1.12      #允许此IP访问所有的本机端口 

sudo ufw delete allow from 192.168.1.12   #删除上一条的规则
sudo ufw deny smtp            #禁止外部访问smtp服务 
sudo ufw delete allow smtp               #删除上面建立的某条规则 

sudo ufw allow proto udp 192.168.0.1 port 53 to 192.168.0.2 port 53 

 

用户操作

1、强制使某个用户退出:

首先:使用w查看当前登录的用户,注意TTY所示登录进程终端号

其次:使用pkill –9 -t pts/1 结束pts/1进程所对应用户登录

2、查看所有登录用户的操作历史

不管是root用户还是其它的用户只有登陆系统后用进入操作我们都可以通过命令history来查看历史记录,但history只针对登录用户下执行有效,即使root用户也无法得到其它用户histotry历史。如果root用户要查看其它用户的操作记录,通过在/etc/profile里面加入以下代码就可以实现:

PS1="`whoami`@`hostname`:"'[$PWD]'
history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
if [ "$USER_IP" = "" ]
then
USER_IP=`hostname`
fi
if [ ! -d /tmp/dbasky ]
then
mkdir /tmp/dbasky
chmod 777 /tmp/dbasky
fi
if [ ! -d /tmp/dbasky/${LOGNAME} ]
then
mkdir /tmp/dbasky/${LOGNAME}
chmod 300 /tmp/dbasky/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date "+%Y-%m-%d_%H:%M:%S"`
export HISTFILE="/tmp/dbasky/${LOGNAME}/${USER_IP} dbasky.$DT"
chmod 600 /tmp/dbasky/${LOGNAME}/*dbasky* 2>/dev/null

添加完后执行 source /etc/profile 使脚本生效
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值