256 lines
10 KiB
Text
256 lines
10 KiB
Text
|
# UTscapy syntax is explained here: http://www.secdev.org/projects/UTscapy/
|
||
|
|
||
|
# original author: patrick battistello
|
||
|
|
||
|
% Validation of Diameter layer
|
||
|
|
||
|
|
||
|
#######################################################################
|
||
|
+ Different ways of building basic AVPs
|
||
|
#######################################################################
|
||
|
|
||
|
= AVP identified by full name
|
||
|
a1 = AVP ('High-User-Priority', val=15)
|
||
|
a1.show()
|
||
|
raw(a1) == b'\x00\x00\x02/@\x00\x00\x0c\x00\x00\x00\x0f'
|
||
|
|
||
|
= Same AVP identified by the beginning of the name
|
||
|
a1b = AVP ('High-U', val=15)
|
||
|
a1b.show()
|
||
|
raw(a1b) == raw(a1)
|
||
|
|
||
|
= Same AVP identified by its code
|
||
|
a1c = AVP (559, val=15)
|
||
|
a1c.show()
|
||
|
raw(a1c) == raw(a1)
|
||
|
|
||
|
= The Session-Id AVP (with some padding added)
|
||
|
a2 = AVP ('Session-Id', val='aaa.test.orange.fr;1428128;644587')
|
||
|
a2.show()
|
||
|
raw(a2) == b'\x00\x00\x01\x07@\x00\x00)aaa.test.orange.fr;1428128;644587\x00\x00\x00'
|
||
|
|
||
|
= An enumerated AVP
|
||
|
a3 = AVP ('Auth-Session-State', val='NO_STATE_MAINTAINED')
|
||
|
a3.show()
|
||
|
raw(a3) == b'\x00\x00\x01\x15@\x00\x00\x0c\x00\x00\x00\x01'
|
||
|
|
||
|
= An address AVP
|
||
|
a4v4 = AVP("CG-Address", val='192.168.0.1')
|
||
|
a4v4.show()
|
||
|
raw(a4v4) == b'\x00\x00\x03N\xc0\x00\x00\x12\x00\x00(\xaf\x00\x01\xc0\xa8\x00\x01\x00\x00'
|
||
|
|
||
|
a4v6 = AVP("CG-Address", val='::1')
|
||
|
a4v6.show()
|
||
|
raw(a4v6) == b'\x00\x00\x03N\xc0\x00\x00\x1e\x00\x00(\xaf\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00'
|
||
|
|
||
|
a4error = AVP("CG-Address", val="unknown")
|
||
|
a4error.show()
|
||
|
assert raw(a4error) == raw(AVP("CG-Address"))
|
||
|
|
||
|
= A time AVP
|
||
|
a5 = AVP("Expiry-Time")
|
||
|
a5.show()
|
||
|
assert not a5.val
|
||
|
|
||
|
= An empty Auth App ID AVP
|
||
|
a6 = AVP("Auth-Application-Id")
|
||
|
a6.show()
|
||
|
raw(a6) == b'\x00\x00\x01\x02@\x00\x00\x0c\x00\x00\x00\x00'
|
||
|
|
||
|
= An ISDN AVP
|
||
|
a7 = AVP("MSISDN", val="101")
|
||
|
a7.show()
|
||
|
raw(a7) == b'\x00\x00\x02\xbd\xc0\x00\x00\x0e\x00\x00(\xaf\x01\xf1\x00\x00'
|
||
|
|
||
|
= Some OctetString AVPs
|
||
|
a8 = AVP("Authorization-Token", val="test")
|
||
|
a8.show()
|
||
|
assert raw(a8) == b'\x00\x00\x01\xfa\xc0\x00\x00\x10\x00\x00(\xaftest'
|
||
|
|
||
|
a8 = AVP("Authorization-Token", val=b"test\xc3\xa9")
|
||
|
a8.show()
|
||
|
assert a8.val == b"test\xc3\xa9"
|
||
|
assert raw(a8) == b'\x00\x00\x01\xfa\xc0\x00\x00\x12\x00\x00(\xaftest\xc3\xa9\x00\x00'
|
||
|
|
||
|
= Unknown AVP identifier
|
||
|
|
||
|
a9 = AVP("wrong")
|
||
|
assert not a9
|
||
|
|
||
|
|
||
|
#######################################################################
|
||
|
+ AVPs with vendor field
|
||
|
#######################################################################
|
||
|
|
||
|
= Vendor AVP identified by full name
|
||
|
a4 = AVP ('Feature-List-ID', val=1)
|
||
|
a4.show()
|
||
|
raw(a4) == b'\x00\x00\x02u\x80\x00\x00\x10\x00\x00(\xaf\x00\x00\x00\x01'
|
||
|
|
||
|
= Same AVP identified by its code and vendor ID
|
||
|
* This time a list is required as first argument
|
||
|
a4c = AVP ( [629, 10415], val=1)
|
||
|
raw(a4c) == raw(a4)
|
||
|
|
||
|
|
||
|
#######################################################################
|
||
|
+ Altering the AVPs default provided values
|
||
|
#######################################################################
|
||
|
|
||
|
= Altering the flags of the Origin-Host AVP
|
||
|
a5 = AVP ('Origin-Host', avpFlags=187, val='aaa.test.orange.fr')
|
||
|
a5.show()
|
||
|
raw(a5) == b'\x00\x00\x01\x08\xbb\x00\x00\x1aaaa.test.orange.fr\x00\x00'
|
||
|
|
||
|
= Altering the length of the Destination-Realm AVP
|
||
|
a6 = AVP (283, avpLen=33, val='foreign.realm1.fr')
|
||
|
a6.show()
|
||
|
raw(a6) == b'\x00\x00\x01\x1b@\x00\x00!foreign.realm1.fr\x00\x00\x00'
|
||
|
|
||
|
= Altering the vendor of the Public-Identity AVP, and hence the flags ...
|
||
|
a7 = AVP ( [601, 98765432], val = 'sip:+0123456789@aaa.test.orange.fr')
|
||
|
a7.show()
|
||
|
raw(a7) == b'\x00\x00\x02Y\x80\x00\x00.\x05\xe3\nxsip:+0123456789@aaa.test.orange.fr\x00\x00'
|
||
|
|
||
|
|
||
|
#######################################################################
|
||
|
+ Grouped AVPs
|
||
|
#######################################################################
|
||
|
|
||
|
= The Supported-Features AVP (with vendor)
|
||
|
a8 = AVP ('Supported-Features')
|
||
|
a8.val.append(a1)
|
||
|
a8.val.append(a5)
|
||
|
a8.show()
|
||
|
raw(a8) == b'\x00\x00\x02t\x80\x00\x004\x00\x00(\xaf\x00\x00\x02/@\x00\x00\x0c\x00\x00\x00\x0f\x00\x00\x01\x08\xbb\x00\x00\x1aaaa.test.orange.fr\x00\x00'
|
||
|
|
||
|
= The same AVP created more simply
|
||
|
a8b = AVP ('Supported-Features', val = [a1, a5])
|
||
|
raw(a8b) == raw(a8)
|
||
|
|
||
|
= (re)Building the previous AVP from scratch
|
||
|
a8c = AVP ('Supported-Features', val = [
|
||
|
AVP ('High-User-Priority', val=15),
|
||
|
AVP ('Origin-Host', avpFlags=187, val='aaa.test.orange.fr') ])
|
||
|
raw(a8c) == raw(a8)
|
||
|
|
||
|
= Another (dummy) grouped AVP
|
||
|
a9 = AVP (297, val = [a2, a4, a6])
|
||
|
a9.show()
|
||
|
raw(a9) == b'\x00\x00\x01)@\x00\x00`\x00\x00\x01\x07@\x00\x00)aaa.test.orange.fr;1428128;644587\x00\x00\x00\x00\x00\x02u\x80\x00\x00\x10\x00\x00(\xaf\x00\x00\x00\x01\x00\x00\x01\x1b@\x00\x00!foreign.realm1.fr\x00\x00\x00'
|
||
|
|
||
|
= A grouped AVP inside another grouped AVP
|
||
|
a10 = AVP ('Server-Cap', val = [a1, a9])
|
||
|
a10.show()
|
||
|
raw(a10) == b'\x00\x00\x02[\xc0\x00\x00x\x00\x00(\xaf\x00\x00\x02/@\x00\x00\x0c\x00\x00\x00\x0f\x00\x00\x01)@\x00\x00`\x00\x00\x01\x07@\x00\x00)aaa.test.orange.fr;1428128;644587\x00\x00\x00\x00\x00\x02u\x80\x00\x00\x10\x00\x00(\xaf\x00\x00\x00\x01\x00\x00\x01\x1b@\x00\x00!foreign.realm1.fr\x00\x00\x00'
|
||
|
|
||
|
= A big grouped AVP
|
||
|
a11 = AVP ('SIP-Auth', val = [a2, a4, a8, a10])
|
||
|
a11.show()
|
||
|
raw(a11) == b'\x00\x00\x01x@\x00\x00\xf0\x00\x00\x01\x07@\x00\x00)aaa.test.orange.fr;1428128;644587\x00\x00\x00\x00\x00\x02u\x80\x00\x00\x10\x00\x00(\xaf\x00\x00\x00\x01\x00\x00\x02t\x80\x00\x004\x00\x00(\xaf\x00\x00\x02/@\x00\x00\x0c\x00\x00\x00\x0f\x00\x00\x01\x08\xbb\x00\x00\x1aaaa.test.orange.fr\x00\x00\x00\x00\x02[\xc0\x00\x00x\x00\x00(\xaf\x00\x00\x02/@\x00\x00\x0c\x00\x00\x00\x0f\x00\x00\x01)@\x00\x00`\x00\x00\x01\x07@\x00\x00)aaa.test.orange.fr;1428128;644587\x00\x00\x00\x00\x00\x02u\x80\x00\x00\x10\x00\x00(\xaf\x00\x00\x00\x01\x00\x00\x01\x1b@\x00\x00!foreign.realm1.fr\x00\x00\x00'
|
||
|
|
||
|
= Dissect grouped AVP
|
||
|
|
||
|
a12 = DiamG(b'\x01\x00\x00!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\xbd\xc0\x00\x00\r\x00\x00(\xaf\x01')
|
||
|
assert isinstance(a12.avpList[0], AVP_10415_701)
|
||
|
assert "MSISDN" in a12.avpList[0].name
|
||
|
|
||
|
#######################################################################
|
||
|
+ Diameter Requests (without AVPs)
|
||
|
#######################################################################
|
||
|
|
||
|
= A simple request identified by its name
|
||
|
r1 = DiamReq ('Capabilities-Exchange', drHbHId=1234, drEtEId=5678)
|
||
|
r1.show()
|
||
|
raw(r1) == b'\x01\x00\x00\x14\x80\x00\x01\x01\x00\x00\x00\x00\x00\x00\x04\xd2\x00\x00\x16.'
|
||
|
|
||
|
= Unknown request by its name
|
||
|
ur = DiamReq ('Unknown')
|
||
|
raw(ur) == b'\x01\x00\x00\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
|
||
|
|
||
|
= The same one identified by its code
|
||
|
r1b = DiamReq (257, drHbHId=1234, drEtEId=5678)
|
||
|
raw(r1b) == raw(r1)
|
||
|
|
||
|
= Unknown request by its code
|
||
|
ur = DiamReq (0)
|
||
|
raw(ur) == b'\x01\x00\x00\x14\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
|
||
|
|
||
|
= The same one identified by its abbreviation
|
||
|
* Only the first 2 abbreviation letters are significant (although 3 are provided in this example)
|
||
|
r1c = DiamReq ('CER', drHbHId=1234, drEtEId=5678)
|
||
|
raw(r1c) == raw(r1)
|
||
|
|
||
|
= Altering the request default fields
|
||
|
r2 = DiamReq ('CER', drHbHId=1234, drEtEId=5678, drFlags=179, drAppId=978, drLen=12)
|
||
|
r2.show()
|
||
|
raw(r2) == b'\x01\x00\x00\x0c\xb3\x00\x01\x01\x00\x00\x03\xd2\x00\x00\x04\xd2\x00\x00\x16.'
|
||
|
|
||
|
= Altering the default request fields with string
|
||
|
r2b = DiamReq ('CER', drAppId="1")
|
||
|
r2b.show()
|
||
|
raw(r2b) == b'\x01\x00\x00\x14\x00\x00\x01\x01\x01\x00\x00$\x00\x00\x00\x00\x00\x00\x00\x00'
|
||
|
|
||
|
= Altering the default request fields with invalid string
|
||
|
r2be = DiamReq ('CER', drAppId="-1")
|
||
|
r2be.show()
|
||
|
raw(r2be) == b'\x01\x00\x00\x14\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
|
||
|
|
||
|
|
||
|
#######################################################################
|
||
|
+ Diameter Answers (without AVPs)
|
||
|
#######################################################################
|
||
|
|
||
|
= A simple answer identified by its name
|
||
|
ans1 = DiamAns ('Capabilities-Exchange', drHbHId=1234, drEtEId=5678)
|
||
|
ans1.show()
|
||
|
raw(ans1) == b'\x01\x00\x00\x14\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x04\xd2\x00\x00\x16.'
|
||
|
|
||
|
= Same answer identified by its code or abbreviation
|
||
|
ans1b = DiamAns (257, drHbHId=1234, drEtEId=5678)
|
||
|
ans1c = DiamAns ('CEA', drHbHId=1234, drEtEId=5678)
|
||
|
a = raw(ans1b) == raw(ans1)
|
||
|
b = raw(ans1c) == raw(ans1)
|
||
|
a, b
|
||
|
assert a and b
|
||
|
|
||
|
= Altering the answer default fields
|
||
|
ans2 = DiamAns ('CEA', drHbHId=1234, drEtEId=5678, drFlags=115, drAppId=1154, drLen=18)
|
||
|
ans2.show()
|
||
|
raw(ans2) == b'\x01\x00\x00\x12s\x00\x01\x01\x00\x00\x04\x82\x00\x00\x04\xd2\x00\x00\x16.'
|
||
|
|
||
|
|
||
|
#######################################################################
|
||
|
+ Full Diameter messages
|
||
|
#######################################################################
|
||
|
|
||
|
= A dummy Multimedia-Auth request (identified by only a portion of its name)
|
||
|
r3 = DiamReq ('Multimedia-Auth', drHbHId=0x5478, drEtEId=0x1234, avpList = [a11])
|
||
|
r3.show()
|
||
|
raw(r3) == b'\x01\x00\x01\x04\xc0\x00\x01\x1e\x00\x00\x00\x06\x00\x00Tx\x00\x00\x124\x00\x00\x01x@\x00\x00\xf0\x00\x00\x01\x07@\x00\x00)aaa.test.orange.fr;1428128;644587\x00\x00\x00\x00\x00\x02u\x80\x00\x00\x10\x00\x00(\xaf\x00\x00\x00\x01\x00\x00\x02t\x80\x00\x004\x00\x00(\xaf\x00\x00\x02/@\x00\x00\x0c\x00\x00\x00\x0f\x00\x00\x01\x08\xbb\x00\x00\x1aaaa.test.orange.fr\x00\x00\x00\x00\x02[\xc0\x00\x00x\x00\x00(\xaf\x00\x00\x02/@\x00\x00\x0c\x00\x00\x00\x0f\x00\x00\x01)@\x00\x00`\x00\x00\x01\x07@\x00\x00)aaa.test.orange.fr;1428128;644587\x00\x00\x00\x00\x00\x02u\x80\x00\x00\x10\x00\x00(\xaf\x00\x00\x00\x01\x00\x00\x01\x1b@\x00\x00!foreign.realm1.fr\x00\x00\x00'
|
||
|
|
||
|
|
||
|
= The same request built from scratch
|
||
|
r3b = DiamReq ('Multimedia-Auth', drHbHId=0x5478, drEtEId=0x1234,
|
||
|
avpList = [
|
||
|
AVP ('SIP-Auth', val = [
|
||
|
AVP ('Session-Id', val='aaa.test.orange.fr;1428128;644587'),
|
||
|
AVP ('Feature-List-ID', val=1),
|
||
|
AVP ('Supported-Features', val = [
|
||
|
AVP ('High-User-Priority', val=15),
|
||
|
AVP ('Origin-Host', avpFlags=187, val='aaa.test.orange.fr')
|
||
|
]),
|
||
|
AVP ('Server-Cap', val = [
|
||
|
AVP ('High-User-Priority', val=15),
|
||
|
AVP (297, val = [
|
||
|
AVP ('Session-Id', val='aaa.test.orange.fr;1428128;644587'),
|
||
|
AVP ('Feature-List-ID', val=1),
|
||
|
AVP (283, avpLen=33, val='foreign.realm1.fr')
|
||
|
])
|
||
|
])
|
||
|
])
|
||
|
])
|
||
|
|
||
|
raw(r3b) == raw(r3)
|
||
|
|