Skip to content

Commit 1b4a0f0

Browse files
committed
can now run on interfaces without a default gateway.
Initial support for #436
1 parent a61b547 commit 1b4a0f0

5 files changed

Lines changed: 143 additions & 67 deletions

File tree

cSploit/src/main/java/org/csploit/android/core/System.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -954,15 +954,22 @@ public static void reset() {
954954
synchronized (mTargets) {
955955
mTargets.clear();
956956

957-
Target network = new Target(mNetwork),
958-
gateway = new Target(mNetwork.getGatewayAddress(), mNetwork.getGatewayHardware()),
959-
device = new Target(mNetwork.getLocalAddress(), mNetwork.getLocalHardware());
957+
Target network, gateway, device;
958+
959+
network = new Target(mNetwork);
960+
device = new Target(mNetwork.getLocalAddress(), mNetwork.getLocalHardware());
961+
960962

961-
gateway.setAlias(mNetwork.getSSID());
962963
device.setAlias(android.os.Build.MODEL);
963964

964965
mTargets.add(network);
965-
mTargets.add(gateway);
966+
967+
if(mNetwork.haveGateway()) {
968+
gateway = new Target(mNetwork.getGatewayAddress(), mNetwork.getGatewayHardware());
969+
gateway.setAlias(mNetwork.getSSID());
970+
mTargets.add(gateway);
971+
}
972+
966973
mTargets.add(device);
967974

968975
scanThemAll();
@@ -1004,7 +1011,9 @@ public static void markNetworkAsDisconnected() {
10041011

10051012
public static void markInitialNetworkTargetsAsConnected() {
10061013
InetAddress localAddress = mNetwork.getLocalAddress();
1014+
boolean haveGateway = mNetwork.haveGateway();
10071015
InetAddress gatewayAddress = mNetwork.getGatewayAddress();
1016+
10081017
synchronized (mTargets) {
10091018
for (Target t : mTargets) {
10101019
switch (t.getType()) {
@@ -1014,7 +1023,7 @@ public static void markInitialNetworkTargetsAsConnected() {
10141023
}
10151024
default:
10161025
if (localAddress.equals(t.getAddress()) ||
1017-
gatewayAddress.equals(t.getAddress())) {
1026+
(haveGateway && gatewayAddress.equals(t.getAddress()))) {
10181027
t.setConneced(true);
10191028
}
10201029
break;
@@ -1221,10 +1230,6 @@ public static Collection<Exploit> getCurrentExploits() {
12211230
return getCurrentTarget().getExploits();
12221231
}
12231232

1224-
public static String getGatewayAddress() {
1225-
return mNetwork.getGatewayAddress().getHostAddress();
1226-
}
1227-
12281233
public static boolean isForwardingEnabled() {
12291234
boolean forwarding = false;
12301235
BufferedReader reader;

cSploit/src/main/java/org/csploit/android/net/Network.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.FileInputStream;
3535
import java.io.IOException;
3636
import java.io.InputStreamReader;
37+
import java.lang.reflect.Method;
3738
import java.net.InetAddress;
3839
import java.net.InterfaceAddress;
3940
import java.net.NetworkInterface;
@@ -101,6 +102,7 @@ public String toString() {
101102
private IP4Address mNetmask = null;
102103
private IP4Address mLocal = null;
103104
private IP4Address mBase = null;
105+
private Method mTetheredIfacesMethod = null;
104106

105107
/**
106108
* see http://en.wikipedia.org/wiki/Reserved_IP_addresses
@@ -121,6 +123,7 @@ public Network(Context context, String iface) throws SocketException, UnknownHos
121123
mGateway = new IP4Address(mInfo.gateway);
122124
mNetmask = getNetmask();
123125
mBase = new IP4Address(mInfo.netmask & mInfo.gateway);
126+
mTetheredIfacesMethod = getTetheredIfacesMethod(mConnectivityManager);
124127

125128
if (iface != null) {
126129
if (initNetworkInterface(iface))
@@ -136,6 +139,15 @@ public Network(Context context, String iface) throws SocketException, UnknownHos
136139
throw new NoRouteToHostException("Not connected to any network.");
137140
}
138141

142+
private static Method getTetheredIfacesMethod(ConnectivityManager connectivityManager) {
143+
try {
144+
return connectivityManager.getClass().getDeclaredMethod("getTetheredIfaces");
145+
} catch (NoSuchMethodException e) {
146+
Logger.warning("unable to get 'ConnectivityManager#getTetheredIfaces()': " + e.getMessage());
147+
return null;
148+
}
149+
}
150+
139151
public boolean initNetworkInterface(String iface) {
140152

141153
try {
@@ -158,10 +170,17 @@ public boolean initNetworkInterface(String iface) {
158170
Short.toString(ifaceAddress.getNetworkPrefixLength()));
159171

160172
mLocal = new IP4Address(su.getInfo().getAddress());
161-
mGateway = new IP4Address(getSystemGateway(mInterface.getDisplayName()));
162173
mNetmask = new IP4Address(su.getInfo().getNetmask());
163174
mBase = new IP4Address(su.getInfo().getNetworkAddress());
164175

176+
String gateway = getSystemGateway(mInterface.getDisplayName());
177+
178+
if(gateway == null) {
179+
mGateway = null;
180+
} else {
181+
mGateway = new IP4Address(gateway);
182+
}
183+
165184
return true;
166185
} catch (Exception e) {
167186
Logger.error("Error: " + e.getLocalizedMessage());
@@ -194,11 +213,11 @@ public boolean equals(Network network) {
194213
}
195214

196215
public boolean isInternal(byte[] address) {
197-
byte[] gateway = mGateway.toByteArray();
216+
byte[] local = mLocal.toByteArray();
198217
byte[] mask = mNetmask.toByteArray();
199218

200-
for (int i = 0; i < gateway.length; i++)
201-
if ((gateway[i] & mask[i]) != (address[i] & mask[i]))
219+
for (int i = 0; i < local.length; i++)
220+
if ((local[i] & mask[i]) != (address[i] & mask[i]))
202221
return false;
203222

204223
return true;
@@ -265,8 +284,25 @@ public InetAddress getNetmaskAddress() {
265284
return mNetmask.toInetAddress();
266285
}
267286

287+
public boolean haveGateway() {
288+
return mGateway != null;
289+
}
290+
291+
public boolean isTetheringEnabled() {
292+
if(mTetheredIfacesMethod == null) {
293+
return false;
294+
}
295+
try {
296+
String[] ifaces = (String[]) mTetheredIfacesMethod.invoke(mConnectivityManager);
297+
return ifaces.length > 0;
298+
} catch (Exception e) {
299+
Logger.error("unable to retrieve tethered ifaces: " + e.getMessage());
300+
return false;
301+
}
302+
}
303+
268304
public InetAddress getGatewayAddress() {
269-
return mGateway.toInetAddress();
305+
return mGateway == null ? null : mGateway.toInetAddress();
270306
}
271307

272308
public byte[] getGatewayHardware() {

cSploit/src/main/java/org/csploit/android/plugins/mitm/MITM.java

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,22 @@ static class Action{
9595
public String name;
9696
public String description;
9797
public OnClickListener listener;
98+
public ActionEnabler enabler;
9899

99-
public Action(String name, String description, int resourceId, OnClickListener listener){
100+
public Action(String name, String description, int resourceId, OnClickListener listener, ActionEnabler enabler){
100101
this.resourceId = resourceId;
101102
this.name = name;
102103
this.description = description;
103104
this.listener = listener;
105+
this.enabler = enabler;
104106
}
105107

106108
public Action(String name, String description, OnClickListener listener){
107-
this(name, description, R.drawable.action_plugin, listener);
109+
this(name, description, R.drawable.action_plugin, listener, null);
110+
}
111+
112+
public interface ActionEnabler {
113+
boolean isEnabled();
108114
}
109115
}
110116

@@ -138,11 +144,11 @@ public View getView(int position, View convertView, ViewGroup parent){
138144
if (getSharedPreferences("THEME", 0).getBoolean("isDark", false))
139145
row.setBackgroundResource(R.drawable.card_background_dark);
140146
holder = new ActionHolder();
141-
holder.icon = (ImageView) (row != null ? row.findViewById(R.id.actionIcon) : null);
142-
holder.name = (TextView) (row != null ? row.findViewById(R.id.itemName) : null);
143-
holder.description = (TextView) (row != null ? row.findViewById(R.id.itemDescription) : null);
144-
holder.activity = (ProgressBar) (row != null ? row.findViewById(R.id.itemActivity) : null);
145-
if(row != null) row.setTag(holder);
147+
holder.icon = (ImageView) row.findViewById(R.id.actionIcon);
148+
holder.name = (TextView) row.findViewById(R.id.itemName);
149+
holder.description = (TextView) row.findViewById(R.id.itemDescription);
150+
holder.activity = (ProgressBar) row.findViewById(R.id.itemActivity);
151+
row.setTag(holder);
146152

147153
} else holder = (ActionHolder) row.getTag();
148154

@@ -152,7 +158,8 @@ public View getView(int position, View convertView, ViewGroup parent){
152158
holder.name.setText(action.name);
153159
holder.description.setText(action.description);
154160

155-
if(row != null) row.setOnClickListener(action.listener);
161+
row.setOnClickListener(action.listener);
162+
row.setEnabled(action.enabler == null || action.enabler.isEnabled());
156163

157164
return row;
158165
}
@@ -442,7 +449,7 @@ public void onClick(View v) {
442449
);
443450
overridePendingTransition(R.anim.fadeout, R.anim.fadein);
444451
}
445-
}));
452+
}, null));
446453

447454
mActions.add(new Action
448455
(
@@ -452,7 +459,7 @@ public void onClick(View v) {
452459
new OnClickListener(){
453460
@Override
454461
public void onClick(View v){
455-
if(System.checkNetworking(MITM.this) == false)
462+
if(!System.checkNetworking(MITM.this))
456463
return;
457464

458465
setStoppedState();
@@ -467,7 +474,7 @@ public void onClick(View v){
467474
);
468475
overridePendingTransition(R.anim.fadeout, R.anim.fadein);
469476
}
470-
}));
477+
}, null));
471478

472479

473480
mActions.add(new Action
@@ -478,7 +485,7 @@ public void onClick(View v){
478485
new OnClickListener() {
479486
@Override
480487
public void onClick(View v) {
481-
if (System.checkNetworking(MITM.this) == false)
488+
if (!System.checkNetworking(MITM.this))
482489
return;
483490

484491
setStoppedState();
@@ -493,7 +500,7 @@ public void onClick(View v) {
493500
);
494501
overridePendingTransition(R.anim.fadeout, R.anim.fadein);
495502
}
496-
}));
503+
}, null));
497504

498505
mActions.add(new Action
499506
(
@@ -503,7 +510,7 @@ public void onClick(View v) {
503510
new OnClickListener(){
504511
@Override
505512
public void onClick(View v){
506-
if(System.checkNetworking(MITM.this) == false)
513+
if (!System.checkNetworking(MITM.this))
507514
return;
508515

509516
setStoppedState();
@@ -518,7 +525,7 @@ public void onClick(View v){
518525
);
519526
overridePendingTransition(R.anim.fadeout, R.anim.fadein);
520527
}
521-
}));
528+
}, null));
522529

523530
mActions.add(new Action
524531
(
@@ -528,19 +535,21 @@ public void onClick(View v){
528535
new OnClickListener(){
529536
@Override
530537
public void onClick(View v){
531-
if(System.checkNetworking(MITM.this) == false)
538+
if (!System.checkNetworking(MITM.this))
532539
return;
533540

534541
final ProgressBar activity = (ProgressBar) v.findViewById(R.id.itemActivity);
535542

536543
if(activity.getVisibility() == View.INVISIBLE){
537-
if(System.getCurrentTarget().getType() != Target.Type.ENDPOINT)
544+
if (System.getCurrentTarget().getType() != Target.Type.ENDPOINT) {
538545
new ErrorDialog(getString(R.string.error), getString(R.string.mitm_connection_kill_error), MITM.this).show();
539-
540-
else{
546+
} else if(!System.getNetwork().haveGateway() && !System.getNetwork().isTetheringEnabled()) {
547+
new ErrorDialog(getString(R.string.error), "Connection killer requires a gateway or active Tethering", MITM.this).show();
548+
} else {
541549
setStoppedState();
542550

543551
try {
552+
if(System.getNetwork().haveGateway()) {
544553
mConnectionKillerProcess = System.getTools().arpSpoof.spoof(System.getCurrentTarget(), new ArpSpoof.ArpSpoofReceiver() {
545554

546555
@Override
@@ -560,6 +569,10 @@ public void run() {
560569
});
561570
}
562571
});
572+
} else {
573+
mConnectionKillerProcess = null;
574+
System.setForwarding(false);
575+
}
563576

564577
activity.setVisibility(View.VISIBLE);
565578

@@ -575,10 +588,19 @@ public void run() {
575588
mConnectionKillerProcess = null;
576589
}
577590

591+
if(!System.getNetwork().haveGateway() && System.getNetwork().isTetheringEnabled()) {
592+
System.setForwarding(true);
593+
}
594+
578595
activity.setVisibility(View.INVISIBLE);
579596
}
580597
}
581-
}));
598+
}, new Action.ActionEnabler() {
599+
@Override
600+
public boolean isEnabled() {
601+
return System.getNetwork().haveGateway() || System.getNetwork().isTetheringEnabled();
602+
}
603+
}));
582604

583605
mActions.add(new Action
584606
(
@@ -643,7 +665,7 @@ public void onError(String error, int resId){
643665
} else
644666
setStoppedState();
645667
}
646-
}));
668+
}, null));
647669

648670
mActions.add(new Action
649671
(
@@ -745,7 +767,7 @@ public void onError(String error, int resId){
745767
setStoppedState();
746768
}
747769
}
748-
}));
770+
}, null));
749771

750772
mActions.add(new Action
751773
(
@@ -825,7 +847,7 @@ public void onError(String error, int resId){
825847
} else
826848
setStoppedState();
827849
}
828-
}));
850+
}, null));
829851

830852
mActions.add(new Action
831853
(
@@ -835,7 +857,7 @@ public void onError(String error, int resId){
835857
new OnClickListener(){
836858
@Override
837859
public void onClick(View v){
838-
if(System.checkNetworking(MITM.this) == false)
860+
if(!System.checkNetworking(MITM.this))
839861
return;
840862

841863
final ProgressBar activity = (ProgressBar) v.findViewById(R.id.itemActivity);
@@ -912,7 +934,7 @@ public void onError(String error, int resId){
912934
setStoppedState();
913935
}
914936
}
915-
}));
937+
}, null));
916938

917939
mActions.add(new Action
918940
(
@@ -921,7 +943,7 @@ public void onError(String error, int resId){
921943
new OnClickListener(){
922944
@Override
923945
public void onClick(View v){
924-
if(System.checkNetworking(MITM.this) == false)
946+
if(!System.checkNetworking(MITM.this))
925947
return;
926948

927949
final ProgressBar activity = (ProgressBar) v.findViewById(R.id.itemActivity);

0 commit comments

Comments
 (0)