Programming Language/Python

scapy

GGkeeper 2022. 1. 13. 21:34

###########
## scapy ##
###########

>>> conf
>>> ls()

>>> a = 1
>>> print(a)
1
>>> type(a)
int

Ethernet 헤더를 확인한다.
>>> ls(Ether)
dst        : DestMACField                        = (None)
src        : SourceMACField                      = (None)
type       : XShortEnumField                     = (36864)

ARP 헤더를 확인한다.
>>> ls(ARP)
hwtype     : XShortField                         = (1)
ptype      : XShortEnumField                     = (2048)
hwlen      : FieldLenField                       = (None)
plen       : FieldLenField                       = (None)
op         : ShortEnumField                      = (1)
hwsrc      : MultipleTypeField                   = (None)
psrc       : MultipleTypeField                   = (None)
hwdst      : MultipleTypeField                   = (None)
pdst       : MultipleTypeField                   = (None)

IP 헤더를 확인한다.
>>> ls(IP)
version    : BitField  (4 bits)                  = (4)
ihl        : BitField  (4 bits)                  = (None)
tos        : XByteField                          = (0)
len        : ShortField                          = (None)
id         : ShortField                          = (1)
flags      : FlagsField  (3 bits)                = (<Flag 0 ()>)
frag       : BitField  (13 bits)                 = (0)
ttl        : ByteField                           = (64)
proto      : ByteEnumField                       = (0)
chksum     : XShortField                         = (None)
src        : SourceIPField                       = (None)
dst        : DestIPField                         = (None)
options    : PacketListField                     = ([])

ICMP 헤더를 확인한다.
>>> ls(ICMP)
type       : ByteEnumField                       = (8)
code       : MultiEnumField (Depends on type)    = (0)
chksum     : XShortField                         = (None)
id         : XShortField (Cond)                  = (0)
seq        : XShortField (Cond)                  = (0)
ts_ori     : ICMPTimeStampField (Cond)           = (36575483)
ts_rx      : ICMPTimeStampField (Cond)           = (36575483)
ts_tx      : ICMPTimeStampField (Cond)           = (36575483)
gw         : IPField (Cond)                      = ('0.0.0.0')
ptr        : ByteField (Cond)                    = (0)
reserved   : ByteField (Cond)                    = (0)
length     : ByteField (Cond)                    = (0)
addr_mask  : IPField (Cond)                      = ('0.0.0.0')
nexthopmtu : ShortField (Cond)                   = (0)
unused     : ShortField (Cond)                   = (0)
unused     : IntField (Cond)                     = (0)

TCP 헤더를 확인한다.
>>> ls(TCP)
sport      : ShortEnumField                      = (20)
dport      : ShortEnumField                      = (80)
seq        : IntField                            = (0)
ack        : IntField                            = (0)
dataofs    : BitField  (4 bits)                  = (None)
reserved   : BitField  (3 bits)                  = (0)
flags      : FlagsField  (9 bits)                = (<Flag 2 (S)>)
window     : ShortField                          = (8192)
chksum     : XShortField                         = (None)
urgptr     : ShortField                          = (0)
options    : TCPOptionsField                     = (b'')



>>> a = Ether(src="11:22:33:44:55:66")
>>> a
<Ether  src=11:22:33:44:55:66 |>
>>> a = Ether(src="11:22:33:44:55:66")/IP()
>>> a
<Ether  src=11:22:33:44:55:66 type=0x800 |<IP  |>>
>>> a = Ether(src="11:22:33:44:55:66")/ARP()
>>> a
<Ether  src=11:22:33:44:55:66 type=0x806 |<ARP  |>>


type: 0x0800 IP 
type: 0x0806 ARP
>>> a = Ether(src="11:22:33:44:55:66")/ARP()
>>> a
<Ether  src=11:22:33:44:55:66 type=ARP |<ARP  |>>
>>> print(a.type)
2054
>>> a.type
2054

hex() 
16진수로 변환해주는 리턴하는 함수
형태 : hex(정수)
>>> hex(a.type)
'0x806'  
>>> a = Ether(src="11:22:33:44:55:66")/IP()
>>> a
<Ether  src=11:22:33:44:55:66 type=IPv4 |<IP  |>>
>>> a.type
2048
>>> hex(a.type)
'0x800'


RandIP()
IP주소를 랜덤하게 생성하는 함수

>>> print(RandIP())
61.241.75.148
>>> print(RandIP())
110.123.30.126
>>> print(RandIP())
236.249.225.69
>>> print(RandIP())
122.163.234.154

>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(list(range(1, 11)))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> 

for문을 이용해서 RandIP() 함수를 호출해서 변수 ip에 랜덤 IP주소를 생성한 후 print() 함수로 확인한다.
>>> for i in range(10):
...:     ip = RandIP()
...:     print(ip)
...: 
61.90.31.21
168.63.4.159
64.112.10.160
121.62.170.54
169.44.229.115
118.198.220.51
172.196.204.104
121.115.188.163
206.100.1.204
98.64.5.56

>>> quit()

'Programming Language > Python' 카테고리의 다른 글

for 문 테스트 (IP 주소 생성기)  (0) 2022.01.13
Python : 모듈  (0) 2022.01.13
Python : 함수  (0) 2022.01.13
[기초] 6-2. 전달값과 반환값  (0) 2021.11.09
[기초] 6-1. 함수  (0) 2021.11.09