Python RawSocket Syn, pythonrawsocket

Source: Internet
Author: User

Python RawSocket Syn, pythonrawsocket
#! /Bin/env python #-*-coding: UTF-8-*-# must run the import socket import sys import timeimport random from struct import * # Calculate the checksum (msg): s = 0 #2 bytes for I in range (0, len (msg), 2): w = (ord (msg [I]) <8) + (ord (msg [I + 1]) s = s + w s = (s> 16) + (s & 0 xffff) s = ~ S & 0 xffff return s def CreateSocket (source_ip, dest_ip): try: s = socket. socket (socket. AF_INET, socket. SOCK_RAW, socket. IPPROTO_TCP) s. setsockopt (socket. IPPROTO_IP, socket. IP_HDRINCL, 1) blocks t socket. error, msg: print 'socket create error: ', str (msg [0]), 'message:', msg [1] sys. exit () # Set to manually provide the IP header # s. setsockopt (socket. IPPROTO_TCP, socket. IP_HDRINCL, 1) return s # create an IP header def CreateIpHeader (source_ip, Dest_ip): packet = ''# ip header option 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 # create a TCP header def terminate (source_ip, dest_ip, dest_port ): # tcp Header option source = random. randrange (32000,62000, 1) # randomize a source port 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) # maximum window size 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) # repackage the TCP header, and fill in the correct checksum tcp_header = pack ('! Comment ', source, dest_port, seq, ack_seq, offset_res, tcp_flags, window, tcp_checksum, urg_ptr) return tcp_header def range_scan (source_ip, dest_ip, start_port, end_port ): syn_ack_received = [] # Open Port Storage list 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) print's. sendto', dest_ip, start_port, end_port data = s. recvfrom (1024) [0] [0:] ip_header_len = (ord (data [0]) & 0x0f) * 4 ip_header_ret = data [0: ip_header_len-1] Primary = (ord (data [32]) & 0xf0)> 2 primary = 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 # The program starts from here: open_port_list = [] ipsource = '2017. 168.18.17 'ipdest = '192. 168.19.43 'start = 20 stop = 100 step = (stop-start)/10 scan_ports = range (start, stop, step) if scan_ports [len (scan_ports)-1] <stop: scan_ports.append (stop) print '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], ','View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.