1. 程式人生 > >Scapy常用操作和命令(3)

Scapy常用操作和命令(3)



syn埠掃描的寫法:

>>> ans, unans=sr(IP(src="172.31.100.222", dst="172.31.100.149")/TCP(sport=60000,dport=(1,1000),flags="S"))

>>> ans.filter(lambda (s,r):r.sprintf("%TCP.flags%")=="SA").summary(lambda (s,r):r.sprintf("%TCP.sport% \tis open"))

http         is open

902         is open

上面的指令碼中的幾個注意事項:

  1. flags="S"表示tcpflags欄位為SYN
  2. lambda (s,r)這裡之所以兩個變數是因為ans是一個由兩個list組成的值,一個listsent packets,一個是received packets,所以sr分別遍歷兩個list
  3. sprintf字串格式化方法,通過%TCP.flags%格式化得到flags的字串值

也可以用summary或者nsummaryfilter來輸出結果:

>>> ans.summary(lfilter=lambda (s,r):r.sprintf("%TCP.flags%")=="SA")

IP / TCP 172.31.100.222:60000 > 172.31.100.149:http S ==> IP / TCP 172.31.100.149:http > 172.31.100.222:60000 SA / Padding

IP / TCP 172.31.100.222:60000 > 172.31.100.149:902 S ==> IP / TCP 172.31.100.149:902 > 172.31.100.222:60000 SA / Padding

當然也可以使用make_table()方法來制一個表:

>>> filter(lambda (s,r):r.sprintf("%TCP.flags%")=="SA")

Received 5272 packets, got 477 answers, remaining 1523 packets

>>> ans.filter(lambda (s,r):r.sprintf("%TCP.flags%")=="SA").make_table(lambda (s,r):(s.dst,r.sport,"X"

))

172.31.100.63172.31.100.149

139 X-

445 X-

902 -X

912 -X

>>>

注:make_tablelambda表示式裡面定義了一個row titles.dst),一個col titler.sport)和表格中的填充項“X

此外,還有一個自帶的方法report_ports()可以進行syn scan並反饋結果,結果的排版方式為LaTex格式:

>>> report_ports("139.219.196.160", (3300, 3400))

.........Begin emission:

.......................****.*.******.*.*.*..*.*...*.*.*.*..*.*.*.**.*..**.*..*.**.*.**..**.***...*..****..**.**.*.*...**.**..*..**.**..*..*.**...**....*..*.***...*..***.*...***.*.*.....*.**.**Finished to send 101 packets.

..**..**.*..**....*...*..*..*..*.**.*

Received 238 packets, got 101 answers, remaining 0 packets

'\\begin{tabular}{|r|l|l|}\n\\hline\n3389 & open & SA \\\\\n\\hline\n3300 & closed & TCP RA \\\\\n3302 & closed & TCP RA \\\\\n3301 & closed & TCP RA \\\\\n3303 & closed & TCP RA \\\\\n3304 & closed & TCP RA \\\\\nmysql & closed & TCP RA \\\\\n3308 & closed & TCP RA \\\\\n3305 & closed & TCP RA \\\\\n3310 & closed & TCP RA \\\\\n3307 & closed & TCP RA \\\\\n3309 & closed & TCP RA \\\\\n3311 & closed & TCP RA \\\\\n3313 & closed & TCP RA \\\\\n3312 & closed & TCP RA \\\\\n3314 & closed & TCP RA \\\\\n3315 & closed & TCP RA \\\\\n3316 & closed & TCP RA \\\\\n3317 & closed & TCP RA \\\\\n3318 & closed & TCP RA \\\\\n3319 & closed & TCP RA \\\\\n3320 & closed & TCP RA \\\\\n3321 & closed & TCP RA \\\\\n3322 & closed & TCP RA \\\\\n3323 & closed & TCP RA \\\\\n3324 & closed & TCP RA \\\\\n3325 & closed & TCP RA \\\\\n3327 & closed & TCP RA \\\\\n3326 & closed & TCP RA \\\\\n3328 & closed & TCP RA \\\\\n3329 & closed & TCP RA \\\\\n3330 & closed & TCP RA \\\\\n3331 & closed & TCP RA \\\\\n3332 & closed & TCP RA……

LaTex的結果再格式化一下:

>>> ans=report_ports("139.219.196.160", (3300, 3400))

>>> a = ans.split("\n")

>>> for p in a:

...if ("open" in p):

...print "%s is open" % p[:p.find('&')-1]

...

3389 is open

模擬TCP協議的traceroute(前4跳地址隱藏掉了):

>>> ans, unans=sr(IP(dst="139.219.196.160", ttl=(1,30), id=RandShort())/TCP(flags=0x02))

Begin emission:

.*..*...*.*.**..*.*.....*.Finished to send 30 packets.

..........................................^C

Received 68 packets, got 9 answers, remaining 21 packets

>>> for s,r in ans:

...print s.ttl, r.src

...

1 ???.???.???.???

2 ???.???.???.???

3 ???.???.???.???

4 ???.???.???.???

5 59.43.77.1

6 172.30.1.17

7 180.149.129.158

8 180.149.129.158

9 139.219.196.160

補充一個TCP flags的定義:

#define TH_FIN0x01

#define TH_SYN0x02

#define TH_RST0x04

#define TH_PUSH 0x08

#define TH_ACK0x10

#define TH_URG0x20

#define TH_ECNECHO0x40/* ECN Echo */

#define TH_CWR0x80/* ECN Cwnd Reduced */

使用wrpcap可以將報文寫入cap檔案中:

>>> ans, unans=sr(IP(dst="139.219.196.160", ttl=(1,30), id=RandShort())/TCP(flags=0x02))

wrpcap("/root/ans.cap", unans)

幾個轉碼的函式:

hexdump(pkt):用來將報文轉碼為16進位制的轉碼

import_hexcap():用來將報文的16進位制轉碼重新轉為報文格式

>>> a

<IPfrag=0 proto=tcp dst=123.123.123.123 |<TCPsport=http |>>

>>> hexdump(a)

000045 00 00 28 00 01 00 0040 06 72 DB AC 1F 64 DEE..([email protected]

00107B 7B 7B 7B 00 50 00 5000 00 00 00 00 00 00 00{{{{.P.P........

002050 02 20 00 87 4E 00 00P. ..N..

>>> b=Ether(import_hexcap())

000045 00 00 28 00 01 00 0040 06 72 DB AC 1F 64 DEE..([email protected]

00107B 7B 7B 7B 00 50 00 5000 00 00 00 00 00 00 00{{{{.P.P........

002050 02 20 00 87 4E 00 00

Traceback (most recent call last):

File "<console>", line 1, in <module>

File "/usr/lib/python2.7/dist-packages/scapy/utils.py", line 705, in import_hexcap

l = raw_input().strip()

KeyboardInterrupt

>>> b

<IPfrag=0 proto=tcp dst=123.123.123.123 |<TCPsport=http |>>

str():將報文轉為16進位制字串

可以直接將轉義後的字串使用特定的layer轉換回來

>>> a

<IPfrag=0 proto=tcp dst=123.123.123.123 |<TCPsport=http |>>

>>> str(a)

'E\x00\x00(\x00\x01\x00\[email protected]\x06r\xdb\xac\x1fd\xde{{{{\x00P\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00\x87N\x00\x00'

>>> IP(str(a))

<IPversion=4L ihl=5L tos=0x0 len=40 id=1 flags= frag=0L ttl=64 proto=tcp chksum=0x72db src=172.31.100.222 dst=123.123.123.123 options=[] |<TCPsport=http dport=http seq=0 ack=0 dataofs=5L reserved=0L flags=S window=8192 chksum=0x874e urgptr=0 |>>

>>> Ether(str(a))

<Etherdst=45:00:00:28:00:01 src=00:00:40:06:72:db type=0xac1f |<Rawload='d\xde{{{{\x00P\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00\x87N\x00\x00' |>>-- Wrong format

export_object():將報文轉換為base64編碼

import_object():將base64編碼的報文內容轉回普通報文格式

相關推薦

Scapy常用操作命令(3)

 syn埠掃描的寫法: >>> ans, unans=sr(IP(src="172.31.100.222", dst="172.31.100.149")/TCP(sport=

Scapy常用操作命令(4)

使用dir()可以檢視當前的臨時變數 >>> dir() ['__builtins__', 'a', 'ans', 'conf', 'p', 'r', 's', 'unans

Scapy常用操作命令(2)

>>> ans, unans = sr(IP(src="172.31.100.149/30", dst="www.baidu.com")/TCP(dport=80), inter=0.5, retry=-3, timeout=1) .Begin emission: ............

Linux 常用操作命令

linux 命令 1. 什麽是linux服務器load average?Load是用來度量服務器工作量的大小,即計算機cpu任務執行隊列的長度,值越大,表明包括正在運行和待運行的進程數越多。參考資料:http://en.wikipedia.org/wiki/Load_average2. 如何查看lin

H3C常用配置命令

info arp rim cond link bpp ogg ica 動態arp 鄰居發現命令display lldp neighbor-information list DHCP中繼配置dhcp enabledhcp relay server-group 1 ip x.x

1.linux 基本操作命令

shu init 奇數 oot 普通 安裝腳本 uname 工作目錄 文本文件 整理復習之前的linux學習筆記,正好貼出來了。 1.【root@chen ~】# 【當前登錄用戶@主機名 當前所在目錄】# 當前用戶身份 #號表示管理員root $

redis常用操作操作鍵值

string、list常用操作 string資料建立、覆蓋及設定過期時間 127.0.0.1:6379> get key1         檢視原來的key1的值 "b" 127.0.0.1:6379> set key1 123   &nbs

Composer 常用方法命令

//設定中國映象 方法一: 修改 composer 的全域性配置檔案(推薦方式) 開啟命令列視窗(windows使用者)或控制檯(Linux、Mac 使用者)並執行如下命令: composer config -g repo.packagist composer https://packa

python筆記 1-2 liunx基本操作 - 命令

day 1 liunx 常用命令 ls 命令 檢視 ls : 以列表方式檢視當前檔案內容 ls -a : 檢視所有內容 (包含隱藏檔案 , 隱藏檔案 是帶 . 的檔案) ls -l : 檢視詳細資訊 (包含可讀寫,許可權 大小用量 -h 是友好模式顯示) ls -h ; 友好模式

Manjaro常用軟體命令列推薦

簡介 最近安裝上了號稱世界排名第一的基於Arch Linux的發行版Manjaro,並進行了一些配置,可以點選檢視。安裝了很多好的軟體和命令列工具,以此記錄,給以後的自己和需要的人。 首先檢視我的manjaro詳細資訊,一個screenfetch命令: Man

MYSQL資料庫基本操作命令詳解

在學習MySQL資料庫之前我們要知道MySQL資料庫的操作語言的分類; DDL(資料定義語言) 表的定義、使用者的定義… create drop alter DML(資料操控語言) 對資料的增加、修改、刪除、查詢 insert update delete select

maven用途、核心概念、用法、常用引數命令、擴充套件

本文由淺入深,主要介紹maven的用途、核心概念(Pom、Repositories、Artifact、Build Lifecycle、Goal)介紹、用法(Archetype意義及建立各種專案)、maven常用引數和命令以及簡單故障排除、maven擴充套件(eclipse、cobertura、findb

m語言常用函式命令

1.Set 設定變數 set s=3 2.write 輸出變數 w s 3.kill 刪除所有變數或一些指定的變數或所有變數除了指定的變數。 4.new 初始化變數。 5.Do(d):執行命令 6.Quit(q): 結束執行一個函式並返回一

cache資料庫入門教程 資料庫m語言常用函式命令

1.Set 設定變數 set s=3 2.write 輸出變數 w s  3.kill 刪除所有變數或一些指定的變數或所有變數除了指定的變數。 4.new 初始化變數。 5.Do(d):執行命令 6.Quit(q): 結束執行一個函式並返回一個值 7.$Horolog$Hor

如何在VC中加減日期及 CTime COleDateTime的常用操作比較

如何在VC中加減日期 使用CTime類,如:    CTime t1( 1999, 3, 19, 22, 15, 0 ); // 10:15PM March 19, 1999    CTime t2( 1999, 3, 20, 22, 15, 0 ); // 10:15PM March 20, 1999  

Linux下oracle資料庫常用操作命令

本文出自 "高興F" 部落格,請務必保留此出處http://gaoxingf.blog.51cto.com/612518/121138 Linux下oracle資料庫常用命令 Oracle使用者下執行: $ sqlplus system/manager @ file.s

linux常用知識點命令總結

系統常用命令:①  檢視當前目錄:pwd②  切換目錄 cd③  檢視目錄檔案資訊  檢視檔案詳細資訊stata)    Ls //列出檔案資訊b)    Ls -l //以詳細資訊方式列出檔案資訊c)     Ls-al  //以詳細資訊方式列出所有檔案資訊,包含隱藏檔案d

Windows平臺下MySQL常用操作命令

1.匯出整個資料庫 mysqldump -u 使用者名稱 -p --default-character-set=latin1 資料庫名 > 匯出的檔名(資料庫預設編碼是latin1) mysqldump -u wcnc -p smgp_apps_wcnc >

讀Hadoop3.2原始碼,深入瞭解java呼叫HDFS的常用操作HDFS原理

> 本文將通過一個演示工程來快速上手java呼叫HDFS的常見操作。接下來以建立檔案為例,通過閱讀HDFS的原始碼,一步步展開HDFS相關原理、理論知識的說明。 > 說明:本文件基於最新版本Hadoop3.2.1 # 目錄 ### 一、java呼叫HDFS的常見操作 #### 1.1、演示環境搭

常用批處理命令總結3之FindFindStr

ttr 計算 例如 ber 文件名 忽略大小寫 cap 應該 dst @echo offfindstr /V "[4-4]" all210.txt > no4.txtpause::註釋 /n是顯示出來第幾行!查不帶4的可註冊的域名 find 作