nmap +shell脚本实现内网端口巡检

简介:

nmap的命令很多,这里就不介绍了,想了解的话,网上很多的文章可以参考。


#############################################################

通过nmap端口扫描获取开放的端口,以达到内网体检的目的

#############################################################


1、通过salt获得主机列表

1
salt  '*'  cmd.run  'ifconfig|grep addr|sed -n "4p"|cut -d ":" -f 2|cut -d " " -f 1'  /root/hosts_list

获得的文件内容大致如下:

node1:

192.168.2.11

node2:

192.168.2.12

node3:

192.168.2.13

注意:

主机地址一定要是上面这种结构的,不然下面的sed后的结果不是纯IP列表,发给nmap扫描会报错的。



2、nmap对这个主机列表进行端口扫描

# 注意:刚开始没显式指定端口范围,发现例如27017这些端口都没扫描出来,后来就加了-p 1-65535参数,但是发现扫描特别慢,慎重考虑。

1
sed  -n  'n;p'  /root/hosts_list | xargs  -p 1-65535 nmap -sS >  /tmp/port_list  &&  egrep  "Nmap scan report|open"  /tmp/port_list  /tmp/list  &&  rm  -f  /tmp/port_list

获得的文件内容大致如下:

Nmap scan report for node1 (192.168.2.11)

22/tcp open  ssh

Nmap scan report for node2 (192.168.2.12)

22/tcp open  ssh

Nmap scan report for node3 (192.168.2.13)

22/tcp open  ssh


3、每天执行一次下面的这个脚本,获取最新的开放的端口,并比对旧的数据,发现端口异动就自动报警。

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash
# Description: nmap扫描线上服务器的开放端口
# Author: lirl
# Date: 2016/10/02
sed  -n  'n;p'  /root/hosts_list | xargs  nmap -p 1-65535 -sS >  /tmp/port_list  &&  egrep  "Nmap scan report|open"  /tmp/port_list  /tmp/list_ $( date  +%F)
if  [ $? - eq  0 ]; then 
if  diff  /tmp/list  /tmp/list_ "$(date +%F)"  -y -W 100 >  /tmp/port_change_list  2>  /dev/null  ; then
echo  -e  "[+] some port changed $(date +%F) , Please check file /tmp/port_change_list.\n"   >>  /tmp/openports_stats
# mail -s "Port stats Changed,Please login salt to check." xxxx@126.com < /tmp/port_change_list
else
echo  -e  "[-] none port changed $(date +%F).\n"  >>  /tmp/openports_stats
fi
fi

获得的/tmp/port_change_list 文件内容大致如下:

Nmap scan report for node1 (192.168.2.11)Nmap scan report for node1 (192.168.2.11)

22/tcp open  ssh22/tcp open  ssh

Nmap scan report for node2 (192.168.2.12)Nmap scan report for node2 (192.168.2.12)

22/tcp open  ssh22/tcp open  ssh

Nmap scan report for node3 (192.168.2.13)Nmap scan report for node3 (192.168.2.13)

22/tcp open  ssh22/tcp open  ssh

80/tcp   open  http      <

3306/tcp open  mysql      <

很明显,我们能看出哪些端口是增加的或者减少的。




4、如果确定当前端口是正常流程修改的,可以更新端口列表模板

1
cp  /tmp/list_ $( date  +%F)  /tmp/list



基本上完成这几步骤,就差不多了,还有很多不完善的地方,等想到了在补充进来。










本文转自 lirulei90 51CTO博客,原文链接:http://blog.51cto.com/lee90/1858381,如需转载请自行联系原作者
目录
相关文章
|
5天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
31 3
|
2天前
|
监控 Shell 应用服务中间件
第十二章 Shell脚本编写及常见面试题(二)
第十二章 Shell脚本编写及常见面试题(二)
|
2天前
|
监控 关系型数据库 Shell
第十二章 Shell脚本编写及常见面试题(一)
第十二章 Shell脚本编写及常见面试题(一)
|
2天前
|
监控 Shell
生产环境Shell脚本Ping监控主机是否存活(多种方法)
生产环境Shell脚本Ping监控主机是否存活(多种方法)
|
2天前
|
运维 Shell
Shell脚本判断IP是否合法性(多种方法)
Shell脚本判断IP是否合法性(多种方法)
|
8天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
32 0
|
9天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
10天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
32 1
|
14天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
144 0
|
14天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
141 0