#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019-04-23 1:13
# @Author  : Zhang Bin
# @Site    : 
# @File    : vulnerability_scanner.py
# @Software: PyCharm
# 漏洞扫描脚本
import socket
import os
import sys


def ret_banner(ip, port):
    """通过服务器返回的banner，进行漏洞扫描"""
    try:
        socket.setdefaulttimeout(5)
        s = socket.socket()
        s.connect((ip, port))
        banner = s.recv(1024)
        return banner
    except:
        return

def check_vulns(banner, filename):
    with open(filename, 'r') as f:
        for line in f.readlines():
            if line.strip('\n') in banner:
                print '[+] Server is vulnerable:' + banner.strip('\n')
def main():
    if len(sys.argv) == 2:
        filename = sys.argv[1]
        if not os.path.isfile(filename):
            # 文件不存在
            print '[-]' + filename + 'does not exist'
            exit(0)
        if not os.access(filename, os.R_OK):
            # 无权限访问
            print '[-]' + filename + 'access denied.'
            exit(0)
    else:
        print '[-] Usage: ' + str(sys.argv[0]) + ' <vuln filename>'
        exit(0)
    portlist = [21, 22, 25, 80, 110, 443]
    for x in range(147, 150):
        ip = '192.168.80.' + str(x)
        for port in portlist:
            banner = ret_banner(ip, port)
            print '[+]scanne' + ip + ':' + str(port)
            if banner:
                print '[+]' + ip + ':' + banner
                check_vulns(banner, filename)

if __name__ == '__main__':
    main()
