-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab7.py
More file actions
173 lines (123 loc) · 4.32 KB
/
lab7.py
File metadata and controls
173 lines (123 loc) · 4.32 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# author: TODO: Kenton Bell
from scapy.all import *
from socket import gethostbyname
from subprocess import getstatusoutput
from sys import argv
from scapy.layers.inet import TCP, ICMP
def main():
# these show helpful PRINT code for debugging purposes
# # # # to have nothing run in command line, set all to false
showTestCode = False
try:
ifShowTestCode = argv[3]
if ifShowTestCode == '-t':
showTestCode = True
except:
pass
# maxHops = 100
try:
maxHops = argv[2]
except:
print("ERROR: Invalid maximum hops. The next argument should be the maximum number of hops to allow.")
return
servername = "localhost"
targetport = 80
try:
url = argv[1]
except:
print("ERROR: Invalid url. The next argument should be the url.")
return
if not url.startswith("http://"):
url = "http://" + url
if not url.endswith("/"):
url += "/"
try:
serverIP = gethostbyname(url.split("//")[1].split("/")[0])
except Exception as e:
print(f"Couldnt resolve hostname! {e}")
return
print(f"route to {url.split('//')[1].split('/')[0]} ({serverIP}), {maxHops} hops max")
if showTestCode:
print("url: " + url)
asNumbers = []
reached_target = False
for ttl in range(1, maxHops + 1):
ip = IP(dst=serverIP, ttl=ttl)
syn = TCP(dport=80, flags='S')
packet = ip / syn
response = sr1(packet, verbose=0, timeout=3)
if response:
# if response.haslayer(ICMP) and response[ICMP].type == 11:
if response.haslayer(TCP) and response[TCP].flags == "SA":
if showTestCode:
print("we have got to the end!")
print(response.getlayer(TCP).getlayer(ICMP).payload)
break
hopIP = response.src
print(f"{ttl} - {hopIP}")
# WHOIS Lookup
whois_out = getstatusoutput(f"whois -h whois.cymru.com {hopIP}")
asNum = "***"
for line in whois_out[1].splitlines():
if line.strip().startswith("AS") or line.strip()[0].isdigit():
parts = line.split("|")
if len(parts) > 0 and parts[0].strip().isdigit():
asNum = f"AS{parts[0].strip()}"
break
if asNum not in asNumbers and asNum != "***":
asNumbers.append(asNum)
if hopIP == serverIP:
reached_target = True
break
else:
print(f"{ttl} - * * *")
# Print AS numbers as in the example format
print("Traversed AS numbers: ", end="")
for i, asNum in enumerate(asNumbers):
if i > 0:
print(" -> ", end="")
print(asNum, end="")
if (i + 1) % 3 == 0 and i + 1 < len(asNumbers):
print(" ->\n", end="")
"""
serverName = gethostbyname(url)
ip = IP()
ip.flags = 'S'
ip.dst = serverName
asNumumberlist = None
SourcePortlist = None
ReverseDNSLoopupList = None
firstpacket = ip
hopIndex = 0
try:
while hopIndex < maxHops:
curResponse = sr1(firstpacket, verbose=0, timeout=3)
if curResponse:
SourcePortlist += curResponse.src
curAddress = curResponse.src
if showTestCode:
print(f"curAddress::: {curAddress}")
whois = getstatusoutput(f"whois -h whois.cymru.com {curAddress}")
splitWhois = whois[1].split("\n")
for i in splitWhois:
if i.isdigit():
asNumumberlist += i.split(" ")[0]
nsLookup = getstatusoutput(f"nslookup {curAddress}")
ReverseDNSLoopupList += nsLookup[1].split("name")[1]
hopIndex += 1
ip.dst = serverName
ip.src = curAddress
else:
asNumumberlist += "***"
except:
print("ERROR: The sr1 hopper broke.")
return
if showTestCode:
print("We successfully sent the first packet")
index = 0
print("AS Numbers: ")
for asNumumber in asNumumberlist:
print(f"{index}: {asNumumber}")
"""
if __name__ == "__main__":
main()