难易程度:★★★
阅读点:linux;python;web安全;
文章作者:xiaoye
文章来源:i春秋
关键字:网络渗透技术


前言
linux被越来越多的企业使用,因此掌握一些基本的linux安全加固是有必要的,今天我们来浅谈一些企业常用的linux加固方式,当然仅仅一篇文章是不可能面面俱到的,大牛们勿喷啦^_^
---------------------

一、防止ssh暴破之denyhosts
说到denyhosts,不得不说linux里面两个重要的文件:hosts.deny、hosts.allow
hosts.deny里面存放禁止访问的设定,.allow里面存放的是允许访问的设定
比如
.allow里写入 sshd:210.12.123.*:allow 就代表允许此ip段对sshd服务的使用
同样.deny里写入sshd:210.12.123.*就代表禁止此ip段连上本机的ssh
四、利用iptables和python scapy建立简单的ids网络入侵检测小程序

python 里神奇的库特别多,scapy绝对算一个,为什么呢,因为用它,两行代码可以构建一个简易的wireshark!!
demo:

from scapy.all import sniff
 
sniff(iface='ens33', count=0, filter='icmp', prn=lambda x: x.show())

 

效果见截图:
 
不停地检测数据包,在屏幕上打印出数据包,还支持过滤,强大的很

好了,直接放出来程序吧,其实早就想发出来了,这个是最近老师布置的一个考核试题,构建小程序来检测smurf,并且与iptables联动进行防御,在屏幕中打印出攻击报文,之所以今天才发出来,是因为今天下午刚刚答辩完,然后就可以放出来了

注意:这个小程序是防御smurf攻击的,但是这个不是重点,不需要关注,重点是掌握过滤并检测出自己想要的数据包,联动iptables进行防御的思想,这个小程序本身很简单,大牛勿喷……^_^

'''
@author: xiaoye
'''
from scapy.all import srp,Ether,ARP,conf,sniff
from scapy.utils import wrpcap
from subprocess import PIPE, Popen
from argparse import ArgumentParser
import time
 
arg = ArgumentParser(description='smurf scan && iptables defend by xiaoye')
arg.add_argument('-l','--level',help='sniff level',dest='scapy_level',default=1,type=int)
arg.add_argument('-w','--write',help='write position',dest='wr_position',default='demo.pcap')
result = arg.parse_args()
 
 
global a
a = 1
 
def iptables_policy(a):
    print 'smurf attack !! create iptables policy to defend it...\n the attacker mac is:'
    print a
    #broadcast
    print 'iptables -A INPUT -m mac --mac-source ' + a + ' -j DROP \n'
    Popen('iptables -A INPUT -m mac --mac-source ' + a + ' -j DROP ', stdin=PIPE, stdout=PIPE, shell=True)
    time.sleep(2)
    print 'iptables -A INPUT -m pkttype --pkt-type broadcast -j DROP\n'
    Popen('iptables -A INPUT -m pkttype --pkt-type broadcast -j DROP', stdin=PIPE, stdout=PIPE, shell=True)
    time.sleep(2)
    #broadcast icmp
    print 'iptables -A INPUT -p ICMP --icmp-type echo-request -m pkttype --pkt-type broadcast -j DROP\n '
    Popen('iptables -A INPUT -p ICMP --icmp-type echo-request -m pkttype --pkt-type broadcast -j DROP', stdin=PIPE, stdout=PIPE, shell=True)
    time.sleep(2)
    #icmp 3/s limit
    print 'iptables -A INPUT -p ICMP --icmp-type echo-request -m limit --limit 3/s -j ACCEPT\n'
    Popen('iptables -A INPUT -p ICMP --icmp-type echo-request -m limit --limit 3/s -j ACCEPT', stdin=PIPE, stdout=PIPE, shell=True)
    time.sleep(2)
    #policy
    #Popen('iptables -A INPUT -j DROP')
     
 
def callback(packet):
    global a
     
    if a == 1 and result.scapy_level == 2:
        iptables_policy(packet[Ether].src)
        #print 'xiaoye'
    a = a + 1
    wrpcap(result.wr_position, packet)
    if result.scapy_level == 1 or result.scapy_level == 2:
        return packet.show()
    if result.scapy_level == 3:
        Popen('echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all', stdin=PIPE, stdout=PIPE, shell=True)
        #exit(0)
     
     
#filter smurf:
#filter="icmp and src 192.168.43.65 and dst 192.168.43.205"
if __name__ == "__main__":
    sniff(iface="ens33", count=0, filter="icmp and src 172.20.10.6 and dst 172.20.10.15", prn=callback)

 

filter="icmp and src 172.20.10.6 and dst 172.20.10.15"  这里src填充自己的ip,dst填充广播地址,用来过滤数据包

源程序依赖于python库scapy,linux的iptables防火墙以及python自带库及模块

思路:scapy嗅探ens33网卡流过的数据包,并用sniff方法的filter参数过滤规则过滤出具有smurf特征的数据包,并打印在屏幕上,联动iptables建立防火墙规则,进行smurf防御;

程序有三个level,
level 1只打印出smurf攻击报文;
level 2打印出攻击报文,并与iptabes联动,建立防火墙规则,阻断与攻击源的通信并对icmp echo request报文的接收频率做出限制,实现阻断smurf攻击;
level 3,临时禁止icmp报文,即一切icmp报文将不会得到响应,这里只是应急策略,用level 2就足够了。
 
浏览器崩了。。图片只能传几张了。。这是我的实验报告上的一张演示图 :
注:这是防御程序,攻击可以用kali里的hping3    hping3 --icmp --spoof targetip(目标ip) broadcastip(广播地址)
当攻击开始后,防御程序-l 2时会自动添加iptables规则,并且打印出攻击报文
重点还是iptables规则,这里写了4条规则,第一条是从攻击报文中检测对方的mac地址,将此mac地址的数据包全部丢弃,其余:
iptables -A INPUT -m pkttype --pkt-type broadcast -j DROP

INPUT链添加规则,-m指用扩展模块 此条规则对目标地址是广播地址的数据包进行丢弃

iptables -A INPUT -p ICMP --icmp-type echo-request -m pkttype --pkt-type broadcast -j DROP

INPUT链添加规则,-p匹配数据包中类型,此条规则较上条规则,添加了icmp类型的限制

iptables -A INPUT -p ICMP --icmp-type echo-request -m limit --limit 3/s -j ACCEPT

INPUT链添加规则,对icmp请求应答报文,进行3/s的频率限制                                                  

总结
浏览器有点问题,先写成这样吧,大牛勿喷,有什么error的话请留言,一定会改正

 

相关文章: