forked from cSploit/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.java
More file actions
268 lines (218 loc) · 7.29 KB
/
Copy pathNetwork.java
File metadata and controls
268 lines (218 loc) · 7.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* This file is part of the dSploit.
*
* Copyleft of Simone Margaritelli aka evilsocket <evilsocket@gmail.com>
*
* dSploit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dSploit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dSploit. If not, see <http://www.gnu.org/licenses/>.
*/
package org.csploit.android.net;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import org.apache.commons.net.util.SubnetUtils;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.NoRouteToHostException;
import java.net.SocketException;
import java.net.UnknownHostException;
import org.csploit.android.core.Logger;
import org.csploit.android.core.System;
public class Network
{
public enum Protocol{
TCP,
UDP,
ICMP,
IGMP,
UNKNOWN;
public static Protocol fromString(String proto){
if(proto != null){
proto = proto.toLowerCase();
if(proto.equals("tcp"))
return TCP;
else if(proto.equals("udp"))
return UDP;
else if(proto.equals("icmp"))
return ICMP;
else if(proto.equals("igmp"))
return IGMP;
}
return UNKNOWN;
}
public String toString()
{
switch(this)
{
case ICMP:
return "icmp";
case IGMP:
return "igmp";
case TCP:
return "tcp";
case UDP:
return "udp";
default:
return "unknown";
}
}
}
private ConnectivityManager mConnectivityManager = null;
private WifiManager mWifiManager = null;
private DhcpInfo mInfo = null;
private WifiInfo mWifiInfo = null;
private NetworkInterface mInterface = null;
private IP4Address mGateway = null;
private IP4Address mNetmask = null;
private IP4Address mLocal = null;
private IP4Address mBase = null;
/** see http://en.wikipedia.org/wiki/Reserved_IP_addresses
*/
private static final String[] PRIVATE_NETWORKS = {
"10.0.0.0/8",
"100.64.0.0/10",
"172.16.0.0/12",
"192.168.0.0/16"
};
public Network(Context context) throws SocketException, UnknownHostException{
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
mInfo = mWifiManager.getDhcpInfo();
mWifiInfo = mWifiManager.getConnectionInfo();
mLocal = new IP4Address(mInfo.ipAddress);
mGateway = new IP4Address(mInfo.gateway);
mNetmask = getNetmask();
mBase = new IP4Address(mInfo.netmask & mInfo.gateway);
if(isConnected() == false)
throw new NoRouteToHostException("Not connected to any WiFi access point.");
else{
try{
mInterface = NetworkInterface.getByInetAddress(getLocalAddress());
if(mInterface == null)
throw new IllegalStateException("Error retrieving network interface.");
}
catch(SocketException e){
System.errorLogging(e);
/*
* Issue #26: Initialization error in ColdFusionX ROM
*
* It seems it's a ROM issue which doesn't correctly populate device descriptors.
* This rom maps the default wifi interface to a generic usb device
* ( maybe it's missing the specific interface driver ), which is obviously not, and
* it all goes shit, use an alternative method to obtain the interface object.
*/
mInterface = NetworkInterface.getByName(java.lang.System.getProperty("wifi.interface", "wlan0"));
if(mInterface == null)
throw e;
}
}
}
private IP4Address getNetmask() throws UnknownHostException {
IP4Address result = new IP4Address(mInfo.netmask);
if(System.getSettings().getBoolean("WIDE_SCAN", false)) {
SubnetUtils privateNetwork;
for(String cidr_notation : PRIVATE_NETWORKS) {
privateNetwork = new SubnetUtils(cidr_notation);
if(privateNetwork.getInfo().isInRange(mLocal.toString())) {
result = new IP4Address(privateNetwork.getInfo().getNetmask());
break;
}
}
}
return result;
}
public boolean equals(Network network){
return mInfo.equals(network.getInfo());
}
public boolean isInternal(byte[] address) {
byte[] gateway = mGateway.toByteArray();
byte[] mask = mNetmask.toByteArray();
for(int i = 0; i < gateway.length; i++)
if((gateway[i] & mask[i]) != (address[i] & mask[i]))
return false;
return true;
}
public boolean isInternal(String ip){
try {
return isInternal(InetAddress.getByName(ip).getAddress());
} catch (UnknownHostException e) {
Logger.error(e.getMessage());
}
return false;
}
public boolean isInternal(InetAddress address) {
return isInternal(address.getAddress());
}
public static boolean isWifiConnected(Context context){
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
return info != null && info.isConnected() && info.isAvailable();
}
public static boolean isConnectivityAvailable(Context context){
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
return info != null && info.isConnected();
}
public boolean isConnected(){
return mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
}
public String getSSID(){
return mWifiInfo.getSSID();
}
public int getNumberOfAddresses(){
return IP4Address.ntohl(~mNetmask.toInteger());
}
public IP4Address getStartAddress(){
return mBase;
}
public String getNetworkMasked(){
int network = mBase.toInteger();
return (network & 0xFF) + "." + ((network >> 8) & 0xFF) + "." + ((network >> 16) & 0xFF) + "." + ((network >> 24) & 0xFF);
}
public String getNetworkRepresentation(){
return getNetworkMasked() + "/" + mNetmask.getPrefixLength();
}
public DhcpInfo getInfo(){
return mInfo;
}
public InetAddress getNetmaskAddress(){
return mNetmask.toInetAddress();
}
public InetAddress getGatewayAddress(){
return mGateway.toInetAddress();
}
public byte[] getGatewayHardware(){
return Endpoint.parseMacAddress(mWifiInfo.getBSSID());
}
public byte[] getLocalHardware(){
try{
return mInterface.getHardwareAddress();
}
catch(SocketException e){
System.errorLogging(e);
}
return null;
}
public String getLocalAddressAsString(){
return mLocal.toString();
}
public InetAddress getLocalAddress(){
return mLocal.toInetAddress();
}
public NetworkInterface getInterface(){
return mInterface;
}
}