Python Scapy
Python Scapy
root@cript#
Python Introduction
root@cript#
Basics: Variables
Python is a dynamically-typed language: value="Hello" value=84/2 The last computed value is represented with _: 84/2 value=_ Concatenation occurs with + (or ,): value="Monty"+"Python" value="Monty","Python" Repetition occurs with *: value="Hello"*5
root@cript#
Basics: Printing
Use either set of quotation marks, but be consistent print"Hello" print'Hello' print"'Hello',saysJohn" print'"Hello",saysJohn'
root@cript#
Basics: Strings
String indexing is very flexible in Python: value="CRIPT" value[0]#"C" value[1:3]#"RI" value[:3]#"CRI" value[3:]#"PT" value[1]#"T"(1:lastchar) value[2:]#"PT"(2:2ndlastchar) value[1:1]#"RIP"
root@cript#
Basics: Strings
Strings also have many other useful operations:
value="RIPITCRIPT" value.count("RIP")#2 value.find("RIP")#0 value.rfind("RIP")#8 value.startswith("RIP")#True value.endswith("IPT")#True value2="for{0}years"#Python3.0+ value2.format("99")#'for99years' value3="for%(0)dyears"#Python2.6 value3%{"val":99}#'for99years'
root@cript#
Basics: Strings
root@cript#
root@cript#
So is list comprehension:
#allx,suchthatxisin[0..10] list1=[xforxinrange(10)] list2=[xforxinlist1if(x%2)==0andx<5]
root@cript#
root@cript#
...and queues:
queue=[] queue.append(1) queue.append(2) queue.append(3) queue.pop(0)#1 queue.pop(0)#2 queue.pop(0)#3
root@cript#
root@cript#
Control Structures: if
root@cript#
root@cript#
root@cript#
Modularity: functions
root@cript#
Modularity: classes
Classes can be defined in the traditional way:
classChat: serverIP="" serverPort=8888 def__init__(self,ip,port): serverIP=ip serverPort=port defsendMessage(self,message): ifhasattr(self,'nickname'): printself.nickname+":"+message else: print"Anonymous:"+message
root@cript#
Modularity: classes
root@cript#
Modularity: objects
Objects can be instantiated, but are also dynamic (like other types in Python):
>>>myChat=Chat("1.2.3.4",7777) >>>myChat.sendMessage("Hello") Anonymous:Hello >>>myChat.nickname="rfortier" >>>myChat.sendMessage("Hello") rfortier:Hello >>>delmyChat.nickname >>>myChat.sendMessage("Hello") Anonymous:Hello
root@cript#
root@cript#
root@cript#
Scapy
root@cript#
Scapy: Basics
To see the supported protocols: ls() To find out details about a specific protocol: ls(DNS) To see the available commands (i.e. Python functions): lsc()
root@cript#
Scapy: Basics
Here is some sample code showing how to:
Create a TCP segment, inside an IP datagram Display the TCP segment Send it to some host (192.168.1.1), port 22 Display any response
sendPacket=IP(dst='192.168.1.1')/TCP(dport=22, sport=RandShort(),seq=RandShort()) sendPacket.show2() response=sr1(sendPacket) print"Receivedaresponse:" response.summary()
root@cript#
root@cript#
root@cript#
Scapy: Ping
We have just about enough information to write our own ping function (default ICMP type is 'echo'):
defping(host,repeat=3): packet=IP(dst=host)/ICMP()
forxinrange(repeat):
response=sr1(packet) response.show2()
root@cript#
forxinrange(repeat):
response=sr1(packet) response.show2()
root@cript#
Scapy: Traceroute
...and traceroute:
defmytraceroute(host,maxttl=8): ipps=IP(dst=host,ttl=(1,maxttl)) ans,unans=sr(ipps/ICMP()) forsent,rcvdinans: printsent.ttl,rcvd.src
root@cript#
Scapy: Sniffing
root@cript#
...and a resolver:
defresolve(host): dns=DNS(rd=1,qd=DNSQR(qname=host)) response=sr1(IP(dst='192.168.1.1')/UDP()/dns); ifresponse.haslayer(DNS): answer=response.getlayer(DNS).an answer.show()
root@cript#
root@cript#
root@cript#
DNS poisoning Customized port scanning Fuzzing network protocols Sending exploits (incl. Shellcode) via TCP, UDP IP spoofing (except for sequence number prediction) Network applications