转载请注明:@小五义:http://www.cnblogs/xiaowuyi

在安装完scapy(前两篇笔记有介绍)后,linux环境下,执行sudo scapy运行scapy。

一、简单的发送包

1、send()在第三层发送数据包,但没有接收功能。如:

>>> send(IP(dst="www.baidu.com",ttl=)/ICMP())
.
Sent packets.

这里相当于ping了下百度,ttl=1

2、sendp(),在第二层发送数据包,同样没有接收功能。如:

>>> sendp(Ether()/IP(dst="www.baidu.com",ttl=)/ICMP())
WARNING: Mac address to reach destination not found. Using broadcast.
.
Sent packets.
>>> sendp(Ether()/IP(dst="127.0.0.1",ttl=)/ICMP())
.
Sent packets.

3、sr(),在第三层发送数据包,有接收功能。如:

>>> p=sr(IP(dst="www.baidu.com",ttl=)/ICMP())
Begin emission:
..Finished to send packets.
.*
Received packets, got answers, remaining packets
>>> p
(<Results: TCP: UDP: ICMP: Other:>, <Unanswered: TCP: UDP: ICMP: Other:>)
>>> p[]
<Results: TCP: UDP: ICMP: Other:>
>>> p[].show()
IP / ICMP 27.214.222.160 > 61.135.169.105 echo-request ==> IP / ICMP 27.214.220.1 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
再比如,连续发送ttl=1,2,3,4四个包的情况
>>> p=sr(IP(dst="www.baidu.com",ttl=(,))/ICMP())
Begin emission:
Finished to send packets.
.*.*.*.*
Received packets, got answers, remaining packets
>>> p
(<Results: TCP: UDP: ICMP: Other:>, <Unanswered: TCP: UDP: ICMP: Other:>)
>>> p[].show()
IP / ICMP 27.214.222.160 > 61.135.169.125 echo-request ==> IP / ICMP 27.214.220.1 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
IP / ICMP 27.214.222.160 > 61.135.169.125 echo-request ==> IP / ICMP 222.132.4.1 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
IP / ICMP 27.214.222.160 > 61.135.169.125 echo-request ==> IP / ICMP 119.190.5.126 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
IP / ICMP 27.214.222.160 > 61.135.169.125 echo-request ==> IP / ICMP 112.253.4.197 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
>>>

4、sr1(),在第三层发送数据包,有接收功能,但只接收第一个包。以上面的发送四个包为例:

>>> q=sr1(IP(dst="www.baidu.com",ttl=(,))/ICMP())
Begin emission:
Finished to send packets.
.*.*.*.*
Received packets, got answers, remaining packets
>>> q
<IP version=4L ihl=5L tos=0xc0 len= id= flags= frag=0L ttl= proto=icmp chksum=0xb611 src=27.214.220.1 dst=27.214.222.160 options=[] |<ICMP type=time-exceeded code=ttl-zero-during-transit chksum=0xf4ff unused= |<IPerror version=4L ihl=5L tos=0x0 len= id= flags= frag=0L ttl= proto=icmp chksum=0xd879 src=27.214.222.160 dst=61.135.169.105 options=[] |<ICMPerror type=echo-request code= chksum=0xf7ff id=0x0 seq=0x0 |>>>>
>>> q.show()
###[ IP ]###
version= 4L
ihl= 5L
tos= 0xc0
len=
id=
flags=
frag= 0L
ttl=
proto= icmp
chksum= 0xb611
src= 27.214.220.1
dst= 27.214.222.160
\options\
###[ ICMP ]###
type= time-exceeded
code= ttl-zero-during-transit
chksum= 0xf4ff
unused=
###[ IP in ICMP ]###
version= 4L
ihl= 5L
tos= 0x0
len=
id=
flags=
frag= 0L
ttl=
proto= icmp
chksum= 0xd879
src= 27.214.222.160
dst= 61.135.169.105
\options\
###[ ICMP in ICMP ]###
type= echo-request
code=
chksum= 0xf7ff
id= 0x0
seq= 0x0

5、srloop(),在第三层工作,如下:

>>> p=srloop(IP(dst="www.baidu.com",ttl=)/ICMP())
RECV : IP / ICMP 27.214.220.1 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
RECV : IP / ICMP 27.214.220.1 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
RECV : IP / ICMP 27.214.220.1 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
RECV : IP / ICMP 27.214.220.1 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
RECV : IP / ICMP 27.214.220.1 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
^C
Sent packets, received packets. 100.0% hits.
>>> p=srloop(IP(dst="www.baidu.com",ttl=)/ICMP(),inter=,count=)
RECV : IP / ICMP 27.214.220.1 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror
RECV : IP / ICMP 27.214.220.1 > 27.214.222.160 time-exceeded ttl-zero-during-transit / IPerror / ICMPerror Sent packets, received packets. 100.0% hits.
这里第一条语句在执行时,将会不停的ping百度,第二条执行时每隔3秒ping一次,一共执行两次。inter表示间隔,count记录次数。

6、srp()、srp1()、srploop()与上面3、4、5相同,只是工作在第二层。

二、SYN扫描

SYN扫描:也叫“半开式扫描”(half-open scanning),因为它没有完成一个完整的TCP连接。这种方法向目标端口发送一个SYN分组(packet),如果目标端口返回SYN/ACK,那么可以肯定该端口处于检听状态;否则,返回的是RST/ACK。

>>> sr1(IP(dst="61.135.169.105")/TCP(dport=,flags="S"))
Begin emission:
Finished to send packets.
.*
Received packets, got answers, remaining packets
<IP version=4L ihl=5L tos=0x0 len= id= flags= frag=0L ttl= proto=tcp chksum=0xa168 src=61.135.169.105 dst=27.214.222.160 options=[] |<TCP sport=http dport=ftp_data seq=3516051844L ack= dataofs=5L reserved=0L flags=SA window= chksum=0x2aef urgptr= |>> >>> sr1(IP(dst="61.135.169.105")/TCP(dport=,flags="S"))
Begin emission:
Finished to send packets.
.*
Received packets, got answers, remaining packets
<IP version=4L ihl=5L tos=0x0 len= id= flags= frag=0L ttl= proto=icmp chksum=0xd677 src=123.125.248.102 dst=27.214.222.160 options=[] |<ICMP type=dest-unreach code=communication-prohibited chksum=0xfc8d unused= |<IPerror version=4L ihl=5L tos=0x0 len= id= flags= frag=0L ttl= proto=tcp chksum=0xa168 src=27.214.222.160 dst=61.135.169.105 options=[] |<TCPerror sport=ftp_data dport= seq= |>>>>

从结果看,当扫描百度(61.135.169.105)的80端口时,返回的包中ACK=1或者flags=SA,说明该端口处于监听状态,当扫描81端口时,无ACK=1,或者flags=,说明其未处于监听状态。

如果要扫描多个端口,可以使用以下语句,如扫描百度的80-83端口:

>>>sr(IP(dst="www.baidu.com")/TCP(dport=(,),flags="S"))

如要扫描21,80,3389等端口:

>>>sr(IP(dst="www.baidu.com")/TCP(dport=[,,],flags="S"))

简单要显示结果:

>>>ans,unans=_

>>>ans.summary(lambda(s,r):r.sprintf("%TCP.sport% \t %TCP.flags%"))

http SA

   RA

   RA

   RA

这里我在扫描80-83时,总是在不停的扫,用ctrl+C停止后,只能得到两个结果,目前没搞明白是什么原因。如下:

>>> sr(IP(dst="www.baidu.com",ttl=)/TCP(dport=(,),flags="S"))
Begin emission:
Finished to send packets.
.*.*.................................................................................
^C
Received packets, got answers, remaining packets
(<Results: TCP: UDP: ICMP: Other:>, <Unanswered: TCP: UDP: ICMP: Other:>)
>>> ans,unans=_
>>> ans.summary()
IP / TCP 27.214.134.124:ftp_data > 61.135.169.105:http S ==> IP / TCP 61.135.169.105:http > 27.214.134.124:ftp_data SA
IP / TCP 27.214.134.124:ftp_data > 61.135.169.105: S ==> IP / ICMP 123.125.248.42 > 27.214.134.124 dest-unreach communication-prohibited / IPerror / TCPerror
>>> ans.summary(lambda(s,r):r.sprintf("%TCP.sport% \t %TCP.flags%"))
http SA
?? ??

三、TCP traceroute

traceroute:用来追踪出发点到目的地所经过的路径,通过Traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径。当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一样,但基本上来说大部分时候所走的路由是相同的。

>>> ans,unans=sr(IP(dst="www.baidu.com",ttl=(,),id=RandShort())/TCP(flags=0x2))
Begin emission:
...*.*.*.*.*.*.*.*.*.*.*Finished to send packets.
.*.*.*.*.*.*.*.*.*.*....^C
Received packets, got answers, remaining packets
>>> for snd,rcv in ans:
... print snd.ttl,rcv.src,isinstance(rcv.payload,TCP)
...
112.253.4.177 False
219.158.98.221 False
124.65.194.22 False
124.65.58.182 False
123.125.248.42 False
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True
61.135.169.105 True

scapy学习笔记(3)发送包,SYN及TCP traceroute 扫描的更多相关文章

  1. Ext JS4 学习笔记之发送表单(Form)时也将表单下的表格(Grid)数据一同发送的方法

    Ext JS4 学习笔记之发送表单(Form)时也将表单下的表格(Grid)数据一同发送的方法 昨天在开发的时候遇到个小问题,就是如何将Grid的内容与Form一起发送到服务器端.默认情况下,表单(F ...

  2. [转帖]Linux学习笔记之rpm包管理功能全解

    Linux学习笔记之rpm包管理功能全解 https://www.cnblogs.com/JetpropelledSnake/p/11177277.html rpm 的管理命令 之前学习过 yum 的 ...

  3. scapy学习笔记(2)--包及包的定义

    转载请注明:@小五义:http://www.cnblogs/xiaowuyi 一.包 包(Packet)是TCP/IP协议通信传输中的数据单位,一般也称“数据包”.其主要由“目的IP地址”.“源IP地 ...

  4. scapy学习笔记(3)

    转自:@小五义:http://www.cnblogs/xiaowuyi 在安装完scapy(前两篇笔记有介绍)后,linux环境下,执行sudo scapy运行scapy. 一.简单的发送包 1.se ...

  5. scapy学习笔记(2)

    一.包 包(Packet)是TCP/IP协议通信传输中的数据单位,一般也称“数据包”.其主要由“目的IP地址”.“源IP地址”.“净载数据”等部分构成,包括包头和包体,包头是固定长度,包体的长度不定, ...

  6. TCP协议学习笔记(一)首部以及TCP的三次握手连接四次挥手断开

    TCP协议是一种面向连接的.可靠的流协议. 流即不间断的数据结构.这样能够保证接收到数据顺序与发送相同.但是犹如数据间没有间隔,因此在TCP通信中,发送端应用可以在自己所要发送的消息中设置一个标示长度 ...

  7. scapy学习笔记(1)

    转载请注明:小五义 http://www.cnblogs.com/xiaowuyi scapy是python写的一个功能强大的交互式数据包处理程序,可用来发送.嗅探.解析和伪造网络数据包,常常被用到网 ...

  8. Netty4 学习笔记之三:粘包和拆包

    前言 在上一篇Netty 心跳 demo 中,了解了Netty中的客户端和服务端之间的心跳.这篇就来讲讲Netty中的粘包和拆包以及相应的处理. 名词解释 粘包: 会将消息粘粘起来发送.类似吃米饭,一 ...

  9. scapy学习笔记(5)

    1.ACK Scan >>>ans,unans=sr(IP(dst=,],flags="A") 扫描后,若要找出未过虑的端口: for s,r in ans: i ...

随机推荐

  1. Python 进阶必备函数

    1. lambda 表达式 匿名函数(英语:anonymous function)是指一类无需定义标识符(函数名)的函数.通俗来说呢,就是它可以让我们的函数,可以不需要函数名. 正常情况下,我们定义一 ...

  2. 集合框架四(Map)

    Map的主要实现类: --HashMap:Map的主要实现类(掌握) --LinkedHashMap:使用链表维护添加进Map中的顺序,遍历时按添加时的顺序遍历 --TreeMap:按照添加进Map中 ...

  3. JS 词法作用域 p2

    关于js 还是写的简短些,利于个人理解: 先看一个例子: var a = 2; function fn(){ var a = 3; console.log(a); } fn(a); 说明:作用域查找会 ...

  4. 设计模式原则(6)--Open-Closed Principle(OCP)--开闭原则

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.定义: 一个软件实体应当对扩展开放,对修改关闭.即软件实体应尽量在不修改原有代码的情况下进行扩展. 2.使用场 ...

  5. apache2.2 +php7.3安装 编译安装

    1.下载 http://archive.apache.org/dist/httpd/httpd-2.2.0.tar.gz tar -xvf httpd-2.2.0.tar.gz 2.安装 ./conf ...

  6. 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型

    前文提要:[js实例]js中的5种基本数据类型和9种操作符 Array类型的9个数组方法 Array中有9个数组方法: 1.检测数组 2.转换方法 3.栈方法 4.队列方法 5.冲排序方法6.操作方法 ...

  7. SD从零开始41-44

    [原创] SD从零开始41 科目确定(Account determination) 使用科目确定Using Account Determination 你将需要在几个不同的领域确定将要记账的科目: 用 ...

  8. 解决Python 爬取ssh证书 的报错问题

    Python3 中会要求添加信任证书,但只是进行爬取数据就没必要了,我们可以忽略它 r1 =requests.get("https://www.baidu.com", verify ...

  9. Finereport和Finebi的区别

    1.问题描述 大家现在可能都知道,目前帆软旗下特色产品主要是FineReport和FineBI,但是部分用户可能会有个疑问:FineReport和FineBI到底有什么区别? 2.产品介绍FineRe ...

  10. Ubuntu 18.04 修改为静态IP

    1.进入/etc/netplan目录 cd /etc/netplan 2.查看文件 ls 3.编辑 01-network-manager-all.yaml vim 01-network-manager ...