lost and found ( for me ? )

Showing posts with label packet creation. Show all posts
Showing posts with label packet creation. Show all posts

scapy

[ what’s scapy ]


scapy is packet creation python modules.

scapy machine : backtrack linux 5 : 192.168.10.20
target machine : centos 5.6 : 192.168.10.11

create IP objects
root@bt:~# scapy
Welcome to Scapy (2.1.0)
>>> i = IP()
>>> i.dst = "192.168.10.11"
>>> i.display()
###[ IP ]###
 version= 4
 ihl= None
 tos= 0x0
 len= None
 id= 1
 flags=
 frag= 0
 ttl= 64
 proto= ip
 chksum= None
 src= 192.168.10.20
 dst= 192.168.10.11
 \options\


create TCP objects
>>> t = TCP()
>>> t.dport = 80
>>> t.flags = "S"
>>> t.sport = 12345
>>> t.display()
###[ TCP ]###
 sport= 12345
 dport= www
 seq= 0
 ack= 0
 dataofs= None
 reserved= 0
 flags= S
 window= 8192
 chksum= None
 urgptr= 0
 options= {}


send a syn packet I created as above.

sr1(i/t) sends n’ revieve one packet.

Let’s try :)
>>> sr1(i/t)
Begin emission:
.Finished to send 1 packets.
*
Received 2 packets, got 1 answers, remaining 0 packets
<IP  version=4L ihl=5L tos=0x0 len=44 id=0 flags=DF frag=0L ttl=64 proto=tcp chksum=0xa55c src=192.168.10.11 dst=192.168.10.20 options=[] |<TCP  sport=www dport=12345 seq=239470845 ack=1 dataofs=6L reserved=0L flags=SA window=5840 chksum=0xa409 urgptr=0 options=[('MSS', 1460)] |>>
>>>


capture data on target machine
# tshark -nr scapy.pcap
Running as user "root" and group "root". This could be dangerous.
 1   0.000000 192.168.10.20 -> 192.168.10.11 TCP 12345 > 80 [SYN] Seq=0 Win=8192 Len=0
 2   0.000005 192.168.10.11 -> 192.168.10.20 TCP 80 > 12345 [SYN, ACK] Seq=0 Ack=0 Win=5840 Len=0 MSS=1460
 3   0.002936 192.168.10.20 -> 192.168.10.11 TCP 12345 > 80 [RST] Seq=0 Win=0 Len=0


To avoid scapy machine sending RST packet , add the following command on scapy machine.
root@bt:~# iptables -A OUTPUT -p tcp --tcp-flags RST RST -j DROP

root@bt:~# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
DROP       tcp  --  anywhere             anywhere            tcp flags:RST/RST
root@bt:~#


send a syn packet again.
>>> sr1(i/t)
Begin emission:
.Finished to send 1 packets.
*
Received 2 packets, got 1 answers, remaining 0 packets
<IP  version=4L ihl=5L tos=0x0 len=44 id=0 flags=DF frag=0L ttl=64 proto=tcp chksum=0xa55c src=192.168.10.11 dst=192.168.10.20 options=[] |<TCP  sport=www dport=12345 seq=607198198 ack=1 dataofs=6L reserved=0L flags=SA window=5840 chksum=0x7b25 urgptr=0 options=[('MSS', 1460)] |>>


RST packet on scapy machine was dropped by iptables.
# tshark -nr scapy.pcap
Running as user "root" and group "root". This could be dangerous.
 1   0.000000 192.168.10.20 -> 192.168.10.11 TCP 12345 > 80 [SYN] Seq=0 Win=8192 Len=0
 2   0.000005 192.168.10.11 -> 192.168.10.20 TCP 80 > 12345 [SYN, ACK] Seq=0 Ack=0 Win=5840 Len=0 MSS=1460
 3   2.999577 192.168.10.11 -> 192.168.10.20 TCP 80 > 12345 [SYN, ACK] Seq=0 Ack=0 Win=5840 Len=0 MSS=1460


so netx ,  let’s establish TCP connection between scapy machine n’ target machine.

on target machine , open a socket w/ nc command.
nc - arbitrary TCP and UDP connections and listens
# nc -l 80

# lsof -ni:80
COMMAND   PID USER   FD   TYPE  DEVICE SIZE NODE NAME
nc      16945 root    3u  IPv4 2091820       TCP *:http (LISTEN)


send a SYN packet from scapy
>>> sr1(i/t)
Begin emission:
.Finished to send 1 packets.
*
Received 2 packets, got 1 answers, remaining 0 packets
<IP  version=4L ihl=5L tos=0x0 len=44 id=0 flags=DF frag=0L ttl=64 proto=tcp chksum=0xa55c src=192.168.10.11 dst=192.168.10.20 options=[] |<TCP  sport=www dport=12345 seq=1761317211 ack=1 dataofs=6L reserved=0L flags=SA window=5840 chksum=0xc0f5 urgptr=0 options=[('MSS', 1460)] |>>


on target machine
# netstat -an | grep 12345
tcp        0      0 192.168.10.11:80            192.168.10.20:12345         SYN_RECV


create ACK packet on scapy
increment ACK number.
>>> a=TCP()
>>> a.dport=80
>>> a.sport=12345
>>> a.flag="A"
>>> a.ack=1761317212
>>> a.seq=1
>>> a.display()
###[ TCP ]###
 sport= 12345
 dport= www
 seq= 1
 ack= 1761317212
 dataofs= None
 reserved= 0
 flags= A
 window= 8192
 chksum= None
 urgptr= 0
 options= {}


send ACK packet from scapy
>>> send(i/a)
.
Sent 1 packets.
>>>


connection established. netstat –an on target machine.
# netstat -an | grep 12345
tcp        0      0 192.168.10.11:80            192.168.10.20:12345         ESTABLISHED


cool tools :)
>>> lsc()
arpcachepoison      : Poison target's cache with (your MAC,victim's IP) couple
arping              : Send ARP who-has requests to determine which hosts are up
bind_layers         : Bind 2 layers on some specific fields' values
corrupt_bits        : Flip a given percentage or number of bits from a string
corrupt_bytes       : Corrupt a given percentage or number of bytes from a string
defrag              : defrag(plist) -> ([not fragmented], [defragmented],
defragment          : defrag(plist) -> plist defragmented as much as possible
dyndns_add          : Send a DNS add message to a nameserver for "name" to have a new "rdata"
dyndns_del          : Send a DNS delete message to a nameserver for "name"
etherleak           : Exploit Etherleak flaw
fragment            : Fragment a big IP datagram
fuzz                : Transform a layer into a fuzzy layer by replacing some default values by random objects
getmacbyip          : Return MAC address corresponding to a given IP address
hexdiff             : Show differences between 2 binary strings
hexdump             : --
hexedit             : --
is_promisc          : Try to guess if target is in Promisc mode. The target is provided by its ip.
linehexdump         : --
ls                  : List  available layers, or infos on a given layer
promiscping         : Send ARP who-has requests to determine which hosts are in promiscuous mode
rdpcap              : Read a pcap file and return a packet list
send                : Send packets at layer 3
sendp               : Send packets at layer 2
sendpfast           : Send packets at layer 2 using tcpreplay for performance
sniff               : Sniff packets
split_layers        : Split 2 layers previously bound
sr                  : Send and receive packets at layer 3
sr1                 : Send packets at layer 3 and return only the first answer
srbt                : send and receive using a bluetooth socket
srbt1               : send and receive 1 packet using a bluetooth socket
srflood             : Flood and receive packets at layer 3
srloop              : Send a packet at layer 3 in loop and print the answer each time
srp                 : Send and receive packets at layer 2
srp1                : Send and receive packets at layer 2 and return only the first answer
srpflood            : Flood and receive packets at layer 2
srploop             : Send a packet at layer 2 in loop and print the answer each time
traceroute          : Instant TCP traceroute
tshark              : Sniff packets and print them calling pkt.show(), a bit like text wireshark
wireshark           : Run wireshark on a list of packets
wrpcap              : Write a list of packets to a pcap file
>>>

python-dpkt

[ what’s python-dpkt ? ]

python-dpkt is packet creation python modules.
# tail -1 /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 11.04"

install python-dpkt w/ apt-get command
# apt-cache search python-dpkt
python-dpkt - Python packet creation / parsing module

# apt-get install python-dpkt

sample scripts will be installed into /usr/share/doc/python-dpkt/examples directory.
# dpkg -L python-dpkt | egrep examples
/usr/share/doc/python-dpkt/examples
/usr/share/doc/python-dpkt/examples/dhcprequest.py
/usr/share/doc/python-dpkt/examples/dnsping.py
/usr/share/doc/python-dpkt/examples/nbtping.py
/usr/share/doc/python-dpkt/examples/ping.py

python modules will be installed into /usr/share/pyshared/dpkt directory.
# dpkg -L python-dpkt | egrep "pyshared/dpkt" | awk -F \/ '{print $6}'

aim.py
crc32c.py
ah.py
rx.py
gre.py
rtp.py
tpkt.py
sctp.py
icmp.py
ppp.py
__init__.py
rip.py
rfb.py
mrt.py
gzip.py
h225.py
tftp.py
tns.py
dhcp.py
arp.py
smb.py
stun.py
telnet.py
loopback.py
rpc.py
netbios.py
cdp.py
dns.py
ip.py
dpkt.py
ospf.py
diameter.py
pcap.py
tcp.py
qq.py
dtp.py
pmap.py
udp.py
stp.py
pppoe.py
sll.py
ipx.py
ethernet.py
sccp.py
netflow.py
asn1.py
yahoo.py
bgp.py
sip.py
http.py
ip6.py
igmp.py
vrrp.py
ntp.py
pim.py
hsrp.py
icmp6.py
esp.py
radius.py
ssl.py


[ Let’s send ping packets using sample script called ping.py ]

send ping packets w/ /usr/share/doc/python-dpkt/examples/ping.py.
# cp /usr/share/doc/python-dpkt/examples/ping.py .

usage : ping.py targetIP

python-dpkt machine : 192.168.10.14
target IP : 192.168.10.15
# python ping.py 192.168.10.15
PING 192.168.10.15: 12 data bytes
20 bytes from 192.168.10.15: icmp_seq=0 ip_id=61922 ttl=64 time=6.614 ms
20 bytes from 192.168.10.15: icmp_seq=1 ip_id=61923 ttl=64 time=0.600 ms
20 bytes from 192.168.10.15: icmp_seq=2 ip_id=61924 ttl=64 time=0.425 ms

capture data on target machine ( 192.168.10.15 )
# tshark -i br0 icmp
Capturing on br0
 0.000000 192.168.10.14 -> 192.168.10.15 ICMP Echo (ping) request  (id=0x3a4e, seq(be/le)=34/8704, ttl=64)
 0.000023 192.168.10.15 -> 192.168.10.14 ICMP Echo (ping) reply    (id=0x3a4e, seq(be/le)=34/8704, ttl=64)
 1.001911 192.168.10.14 -> 192.168.10.15 ICMP Echo (ping) request  (id=0x8115, seq(be/le)=35/8960, ttl=64)
 1.001930 192.168.10.15 -> 192.168.10.14 ICMP Echo (ping) reply    (id=0x8115, seq(be/le)=35/8960, ttl=64)
 2.003821 192.168.10.14 -> 192.168.10.15 ICMP Echo (ping) request  (id=0xa25d, seq(be/le)=36/9216, ttl=64)
 2.003838 192.168.10.15 -> 192.168.10.14 ICMP Echo (ping) reply    (id=0xa25d, seq(be/le)=36/9216, ttl=64)
^C6 packets captured