-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
151 lines (120 loc) · 5.43 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
import logging
import cymru.ip2asn.dns
import cymru.ip2asn.whois
import cymru.bogon.dns
import cymru.mhr.dns
import cymru.mhr.whois
from cymru import ip_expand
import unittest
class TestDNS(unittest.TestCase):
def setUp(self):
self.c = cymru.ip2asn.dns.DNSClient()
def testASN(self):
asns = ['1515','5005','64496']
datas = self.c.lookupmany_dict(asns,qType='ASN')
for attr in ['asn','cc','lir','owner','date']:
self.assertIsNotNone( getattr(datas[asns[0]], attr) )
if attr != 'date': # date can be null
self.assertIsNotNone( getattr(datas[asns[1]], attr) )
self.assertIsNone( getattr(datas[asns[2]], attr) )
def testIP4(self):
ips = ['88.198.224.117','127.0.0.1']
datas = self.c.lookupmany_dict(ips)
for attr in ['asn','prefix','cc','lir','date']:
self.assertIsNotNone( getattr(datas[ips[0]], attr) )
self.assertIsNone( getattr(datas[ips[1]], attr) )
def testIP6(self):
ips = ['2001:4860:8010::68','2001:7a8:1:1::76','3ffe:1:1::1']
datas = self.c.lookupmany_dict(ips,qType='IP6')
# getip - need ip6 full address
for attr in ['asn','prefix','cc','lir','date']:
self.assertIsNotNone( getattr(datas[ip_expand(ips[0])], attr) )
self.assertIsNotNone( getattr(datas[ip_expand(ips[1])], attr) )
self.assertIsNone( getattr(datas[ip_expand(ips[2])], attr) )
def testPeer(self):
ips = ['91.121.224.117','10.10.11.12']
datas = self.c.lookupmany_dict(ips,qType='PEER')
for attr in ['asn','prefix','cc','lir','date']:
self.assertIsNotNone( getattr(datas[ips[0]], attr) )
self.assertIsNone( getattr(datas[ips[1]], attr) )
class TestWhois(unittest.TestCase):
def setUp(self):
self.c = cymru.ip2asn.whois.WhoisClient()
def testASN(self):
asns = ['1515','5005','64496']
datas = [x for x in self.c.lookupmany(asns,qType='ASN')]
#'asn', is not null in lookup many
for attr in ['cc','lir','owner','date']:
self.assertIsNotNone( getattr(datas[0], attr) )
if attr != 'date': # date can be null
self.assertIsNotNone( getattr(datas[1], attr) )
self.assertIsNone( getattr(datas[2], attr) )
def testIP4(self):
ips = ['88.198.224.117','127.0.0.1']
datas = [x for x in self.c.lookupmany(ips)]
for attr in ['asn','prefix','cc','lir','date']:
self.assertIsNotNone( getattr(datas[0], attr) )
# lir can be 'other' ?
if attr != 'lir':
self.assertIsNone( getattr(datas[1], attr) )
def testIP6(self):
ips = ['2001:4860:8010::68','2001:7a8:1:1::76','3ffe:1:1::1']
datas = [x for x in self.c.lookupmany(ips,qType='IP6')]
for attr in ['asn','prefix','cc','lir','date']:
self.assertIsNotNone( getattr(datas[0], attr) )
self.assertIsNotNone( getattr(datas[1], attr) )
self.assertIsNone( getattr(datas[2], attr) )
class TestBogon(unittest.TestCase):
def setUp(self):
self.c = cymru.bogon.dns.DNSClient()
def testBogon(self):
ips = ['192.168.0.244','198.51.100.0','202.42.42.42']
datas = self.c.lookupmany_dict(ips,'IP')
self.assertTrue( datas[ips[0]] )
self.assertTrue( datas[ips[1]] )
self.assertFalse( datas[ips[2]] )
def testFull6Bogon(self):
ips = ['fe80::4','3ffe:5678:987::3','2001:678:67::01']
datas = self.c.lookupmany_dict(ips,'FULLIP6')
self.assertTrue( datas[ips[0]] )
self.assertFalse( datas[ips[1]] )
self.assertFalse( datas[ips[2]] )
def testFull6BogonRange(self):
ips = ['fe80::4','3ffe:5678:987::3','2001:678:67::01']
datas = self.c.lookupmany_dict(ips,'FULLIP6RANGE')
self.assertEqual( datas[ips[0]], '8000::/1', )
self.assertFalse( datas[ips[1]] )
self.assertFalse( datas[ips[2]] )
def testFullBogon(self):
ips = ['192.168.0.244','198.51.100.0','202.42.42.42']
datas = self.c.lookupmany_dict(ips,'FULLIP')
self.assertTrue( datas[ips[0]] )
self.assertTrue( datas[ips[1]] )
self.assertFalse( datas[ips[2]] )
def testFullBogonRange(self):
ips = ['192.168.0.244','198.51.100.0','202.42.42.42']
datas = self.c.lookupmany_dict(ips,'FULLIPRANGE')
self.assertEqual( datas[ips[0]], '192.168.0.0/16', )
self.assertEqual( datas[ips[1]], '198.51.100.0/24', )
self.assertFalse( datas[ips[2]] )
class TestMhrDNS(unittest.TestCase):
def setUp(self):
self.c = cymru.mhr.dns.DNSClient()
def testHash(self):
hashes = ['0fd453efa2320350f2b08fbfe194b39aab5f798d','733a48a9cb49651d72fe824ca91e8d00']
#'733a48a9cb49651d72fe824ca91e8d00' is malware
res = [x for x in self.c.lookupmany(hashes)]
self.assertIsNone( res[0].detection )
self.assertIsNotNone( res[1].detection )
class TestMhrWhois(unittest.TestCase):
def setUp(self):
self.c = cymru.mhr.whois.WhoisClient()
def testHash(self):
hashes = ['0fd453efa2320350f2b08fbfe194b39aab5f798d','733a48a9cb49651d72fe824ca91e8d00']
#'733a48a9cb49651d72fe824ca91e8d00' is malware
res = [x for x in self.c.lookupmany(hashes)]
self.assertIsNone( res[0].detection )
self.assertIsNotNone( res[1].detection )
if __name__ == '__main__':
unittest.main()