標籤:python tcp-syn掃描 raw-socket
1. TCP SYN掃描
連接埠掃描常用於用於探測伺服器或主機開放連接埠情況,被電腦系統管理員用於確認安全性原則,同時被攻擊者用於識別目標主機上的可運作的網路服務。連接埠掃描是向一定範圍的伺服器連接埠發送對應請求,以此確認可使用的連接埠。雖然其本身並不是惡意的網路活動,但也是網路攻擊者探測目標主機服務,以利用該服務的已知漏洞的重要手段。
TCP SYN掃描是連接埠掃描眾多方式中的一種,其他方式包括TCP掃描,UDP掃描,ACK掃描,視窗掃描和FIN掃描等。
TCP SYN掃描是另一種TCP掃描。連接埠掃描工具不使用作業系統原生網路功能,而是自行產生、發送IP資料包,並監控其回應。這種掃描模式被稱為“半開放掃描”,因為它從不建立完整的TCP串連。連接埠掃描工具產生一個SYN包,如果目標連接埠開放,則會返回SYN-ACK包。掃描端回應一個RST包,然後在握手完成前關閉串連。如果連接埠關閉了但未使用過濾,目標連接埠應該會持續返回RST包。
TCP SYN掃描優點:
2. python 代碼
使用raw socket進行SYN 洪泛,封裝多個函數使其模組化和易於理解。利用結構體可以方便的使用格式化字串和變數列表來編碼資料包。
#!/usr/bin/env python# -*- coding: UTF-8 -*- # 必須以root許可權運行import socketimport sysimport timefrom struct import *# 計算校正和def checksum(msg): s = 0 # 每次取2個位元組 for i in range(0,len(msg),2): w = (ord(msg[i]) << 8) + (ord(msg[i+1])) s = s+w s = (s>>16) + (s & 0xffff) s = ~s & 0xffff return sdef CreateSocket(source_ip,dest_ip): try: s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP) except socket.error, msg: print ‘Socket create error: ‘,str(msg[0]),‘message: ‘,msg[1] sys.exit() # 設定手工提供IP頭部 s.setsockopt(socket.IPPROTO_TCP, socket.IP_HDRINCL, 1) return s# 建立IP頭部def CreateIpHeader(source_ip, dest_ip): packet = ‘‘ # ip 頭部選項 headerlen = 5 version = 4 tos = 0 tot_len = 20 + 20 id = random.randrange(18000,65535,1) frag_off = 0 ttl = 255 protocol = socket.IPPROTO_TCP check = 10 saddr = socket.inet_aton ( source_ip ) daddr = socket.inet_aton ( dest_ip ) hl_version = (version << 4) + headerlen ip_header = pack(‘!BBHHHBBH4s4s‘, hl_version, tos, tot_len, id, frag_off, ttl, protocol, check, saddr, daddr) return ip_header# 建立TCP頭部def create_tcp_syn_header(source_ip, dest_ip, dest_port): # tcp 頭部選項 source = random.randrange(32000,62000,1) # 隨機化一個源連接埠 seq = 0 ack_seq = 0 doff = 5 # tcp flags fin = 0 syn = 1 rst = 0 psh = 0 ack = 0 urg = 0 window = socket.htons (8192) # 最大視窗大小 check = 0 urg_ptr = 0 offset_res = (doff << 4) + 0 tcp_flags = fin + (syn<<1) + (rst<<2) + (psh<<3) + (ack<<4) + (urg<<5) tcp_header = pack(‘!HHLLBBHHH‘, source, dest_port, seq, ack_seq, offset_res, tcp_flags, window, check, urg_ptr) # 偽頭部選項 source_address = socket.inet_aton( source_ip ) dest_address = socket.inet_aton( dest_ip ) placeholder = 0 protocol = socket.IPPROTO_TCP tcp_length = len(tcp_header) psh = pack(‘!4s4sBBH‘, source_address, dest_address, placeholder, protocol, tcp_length); psh = psh + tcp_header; tcp_checksum = checksum(psh) # 重新打包TCP頭部,並填充正確地校正和 tcp_header = pack(‘!HHLLBBHHH‘, source, dest_port, seq, ack_seq, offset_res, tcp_flags, window, tcp_checksum, urg_ptr) return tcp_headerdef range_scan(source_ip, dest_ip, start_port, end_port) : syn_ack_received = [] # 開放連接埠儲存列表 for j in range (start_port, end_port) : s = CreateSocket(source_ip, dest_ip) ip_header = CreateIpHeader(source_ip, dest_ip) tcp_header = create_tcp_syn_header(source_ip, dest_ip,j) packet = ip_header + tcp_header s.sendto(packet, (dest_ip, 0)) data = s.recvfrom(1024) [0][0:] ip_header_len = (ord(data[0]) & 0x0f) * 4 ip_header_ret = data[0: ip_header_len - 1] tcp_header_len = (ord(data[32]) & 0xf0)>>2 tcp_header_ret = data[ip_header_len:ip_header_len+tcp_header_len - 1] if ord(tcp_header_ret[13]) == 0x12: # SYN/ACK flags syn_ack_received.append(j) return syn_ack_received# 程式從這裡開始:open_port_list = []ipsource = ‘192.168.1.95‘ipdest = ‘192.168.1.31‘start = 100stop = 450step = (stop-start)/10scan_ports = range(start, stop, step)if scan_ports[len(scan_ports)-1] < stop: scan_ports.append(stop)for i in range(len(scan_ports)-1): opl = range_scan(ipsource, ipdest, scan_ports[i], scan_ports[i+1]) open_port_list.append(opl)for i in range(len(open_port_list)): print ‘Process #: ‘,i,‘ Open ports: ‘,open_port_list[i]print ‘A list of all open ports found: ‘for i in range(len(open_port_list)): for j in range(len(open_port_list[i])): print open_port_list[i][j],‘, ‘
python 使用raw socket進行TCP SYN掃描