forked from milo2012/ipv4Bypass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbypass.py
More file actions
213 lines (190 loc) · 6.29 KB
/
bypass.py
File metadata and controls
213 lines (190 loc) · 6.29 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
import nmap
import sys
import subprocess
import socket
import fcntl
import struct
import optparse
from termcolor import colored, cprint
nm = nmap.PortScanner()
interfaceNo=""
bold=True
def diff(li1, li2):
return (list(set(li1) - set(li2)))
def setColor(message, bold=False, color=None, onColor=None):
retVal = colored(message, color=color, on_color=onColor, attrs=("bold",))
return retVal
def get_hw_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
def get_ip_addressv6(ifname):
cmd = "ifconfig "+ifname
p1 = subprocess.Popen(["ifconfig",ifname],stdout=subprocess.PIPE)
cmd = "grep -i inet6"
p2 = subprocess.Popen(["grep","-i","inet6"],stdin=p1.stdout,stdout=subprocess.PIPE)
cmd = "awk '{print $2}'"
p3 = subprocess.Popen(["awk","{print $2}"],stdin=p2.stdout,stdout=subprocess.PIPE)
stdout,stderr = p3.communicate()
return (stdout).strip()
def ipv62mac(ipv6):
ipv6=ipv6.split("%")[0]
# remove subnet info if given
subnetIndex = ipv6.find("/")
if subnetIndex != -1:
ipv6 = ipv6[:subnetIndex]
ipv6Parts = ipv6.split(":")
macParts = []
for ipv6Part in ipv6Parts[-4:]:
while len(ipv6Part) < 4:
ipv6Part = "0" + ipv6Part
macParts.append(ipv6Part[:2])
macParts.append(ipv6Part[-2:])
# modify parts to match MAC value
macParts[0] = "%02x" % (int(macParts[0], 16) ^ 2)
del macParts[4]
del macParts[3]
return ":".join(macParts)
def runCommand(cmd):
cmdList=cmd.split(" ")
out = subprocess.Popen(cmdList,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
stdout,stderr = out.communicate()
return stdout
def getRemoteMac(ip):
tmpMacAddr=""
a=nm.scan(hosts=ip, arguments='-sP -6')
for k,v in a['scan'].iteritems():
x=str(v['vendor']).split("': '")[0]
x=x.replace("{'","")
return x
def scanTarget(ipv4,ipv6):
tmpIPv4pPortList=[]
tmpIPv6pPortList=[]
a=nm.scan(hosts=ipv6, arguments='-sT -6 -T4 --top-ports 65535')
for host in nm.all_hosts():
for proto in nm[host].all_protocols():
lport = nm[host][proto].keys()
for port in lport:
tmpIPv6pPortList.append(port)
a=nm.scan(hosts=ipv4, arguments='-sT -T4 --top-ports 65535')
for host in nm.all_hosts():
for proto in nm[host].all_protocols():
lport = nm[host][proto].keys()
for port in lport:
tmpIPv4pPortList.append(port)
return tmpIPv4pPortList,tmpIPv6pPortList
parser = optparse.OptionParser()
parser.add_option('-i', action="store", dest="interfaceNo", help="Network interface (e.g. eth0)")
parser.add_option('-r', action="store", dest="ipRange", help="Local network IP range (e.g. 192.168.0.1/24)")
options, remainder = parser.parse_args()
if not options.interfaceNo or not options.ipRange:
print "[*] Please provide the -i and -r options"
sys.exit()
interfaceNo=options.interfaceNo
myMac=get_hw_address(interfaceNo)
myIP=get_ip_address(interfaceNo)
myIPv6=get_ip_addressv6(interfaceNo)
targetIP=options.ipRange
cmd=""
if myIPv6.startswith("2620:"):
cmd='ping6 -I '+myIPv6+' -c 2 ff02::1%'+interfaceNo
else:
cmd='ping6 -c 2 ff02::1%'+interfaceNo
cmdList=cmd.split(" ")
out = subprocess.Popen(cmdList,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
ipv6List=[]
stdout,stderr = out.communicate()
tmpResultList=stdout.split("\n")
for x in tmpResultList:
if " bytes from " in x:
tmpIP=x.split(" ")[3]
if tmpIP not in ipv6List:
ipv6List.append(tmpIP)
nm.scan(targetIP,arguments='-sP -T4')
ipv4List=[]
for x in nm.all_hosts():
if x not in ipv4List:
ipv4List.append(x)
print "\n[*] Found the below IPv4 addresses"
tmpIPv4List=[]
for x in ipv4List:
#cmd="/usr/sbin/arp -a "+x
cmd="arp -a "+x
tmpResults=runCommand(cmd)
tmpMacAddr=tmpResults.split(" at ")
if "no match found" not in str(tmpMacAddr):
tmpMacAddr=tmpMacAddr[1]
tmpMacAddr=tmpMacAddr.split(" [ether] ")[0]
tmpIPv4List.append([x,str(tmpMacAddr)])
print x+"\t"+str(tmpMacAddr)
print "\n[*] Found the below IPv6 addresses"
tmpIPv6List=[]
for x in ipv6List:
tmpMacAddr=getRemoteMac(x)
if tmpMacAddr!="{}":
tmpIPv6List.append([x,tmpMacAddr])
print x+"\t"+tmpMacAddr
else:
tmpMacAddr=ipv62mac(x)
tmpIPv6List.append([x,tmpMacAddr])
if tmpMacAddr==myMac:
print x+"\t"+tmpMacAddr+" [This Computer]"
else:
print x+"\t"+tmpMacAddr
print "\n[*] Matching IPv4 and IPv6 addresses"
tmpResultList=[]
for y in tmpIPv6List:
tmpFound=False
for x in tmpIPv4List:
if y[1].lower()==x[1].lower():
print y[0]+"\t"+y[1]+"\t"+x[0]
tmpResultList.append([y[0],y[1],x[0]])
tmpFound=True
if tmpFound==False:
if [y[0],"",""] not in tmpResultList:
tmpMac=ipv62mac(y[0])
if tmpMac==myMac:
print y[0]+"\t"+tmpMac+"\t"+myIP+" [This Computer]"
tmpResultList.append([y[0],tmpMac,myIP])
#tmpResultList.append([y[0],tmpMac,"[This Computer]"])
else:
print y[0]+"\t"+tmpMac
tmpResultList.append([y[0],tmpMac,""])
print "\n[*] Comparing ports on IPv4 and IPv6 interfaces on hosts"
for x in tmpResultList:
if x[2]!=myIP:
#if x[2]!="[This Computer]":
tmpIPv4pPortList,tmpIPv6pPortList=scanTarget(x[2],x[0])
if len(tmpIPv4pPortList)!=len(tmpIPv6pPortList):
if len(tmpIPv6pPortList)>len(tmpIPv4pPortList):
if len(x[2])>0:
diffList=diff(tmpIPv6pPortList,tmpIPv4pPortList)
tmpResultList=[]
for y in diffList:
if y in tmpIPv6pPortList:
tmpResultList.append(str(y))
if len(tmpResultList)>0:
print x[2]+"\t["+x[0]+"] - Additional ports on IPv6: "+setColor(", ".join(tmpResultList), bold, color="red")
else:
print x[2]+"\t["+x[0]+"]"
else:
#print "["+x[0]+"]"
diffList=diff(tmpIPv6pPortList,tmpIPv4pPortList)
tmpResultList=[]
for y in diffList:
if y in tmpIPv6pPortList:
tmpResultList.append(str(y))
if len(tmpResultList)>0:
print "["+x[0]+"] - Additional ports on IPv6: "+setColor(", ".join(tmpResultList), bold, color="red")
else:
print "["+x[0]+"]"
else:
print x[2]+"\t["+x[0]+"] - No additional ports on IPv6"