The ping collection

Let's say our network contains a mix of Windows, Unix, and Linux machines with users adding their own Bring Your Own Device (BYOD); they may or may not support ICMP ping. We can now construct a file with three types of common pings for our network, the ICMP, TCP, and UDP pings in scapy_ping_collection.py:

#!/usr/bin/env python2

from scapy.all import *

def icmp_ping(destination):
# regular ICMP ping
ans, unans = sr(IP(dst=destination)/ICMP())
return ans

def tcp_ping(destination, dport):
# TCP SYN Scan
ans, unans = sr(IP(dst=destination)/TCP(dport=dport,flags="S"))
return ans

def udp_ping(destination):
# ICMP Port unreachable error from closed port
ans, unans = sr(IP(dst=destination)/UDP(dport=0))
return ans

In this example, we will also use summary() and sprintf() for the output:

def answer_summary(answer_list):
# example of lambda with pretty print
answer_list.summary(lambda(s, r): r.sprintf("%IP.src% is alive"))
If you were wondering what a lambda is from the answer_summary() function mentioned previously, it is a way to create a small anonymous function; it is a function without a name. More information on it can be found at https://docs.python.org/3.5/tutorial/controlflow.html#lambda-expressions.

We can then execute all the three types of pings on the network in one script:

def main():
print("** ICMP Ping **")
ans = icmp_ping("10.0.0.13-14")
answer_summary(ans)
print("** TCP Ping **")
ans = tcp_ping("10.0.0.13", 22)
answer_summary(ans)
print("** UDP Ping **")
ans = udp_ping("10.0.0.13-14")
answer_summary(ans)

if __name__ == "__main__":
main()

At this point, hopefully you will agree with me that by having the ability to construct your own packet, you can be in charge of the type of operations and tests that you would like to run.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.237.131