Skip to content

Commit

Permalink
Return localhost as internal attack.
Browse files Browse the repository at this point in the history
  • Loading branch information
irinil committed Aug 1, 2020
1 parent e5d3a47 commit ade45bc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 139 deletions.
18 changes: 12 additions & 6 deletions src/main/java/de/tudarmstadt/informatik/hostage/Handler.java
Expand Up @@ -4,7 +4,6 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import java.util.UUID;

Expand All @@ -14,7 +13,6 @@
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
import de.tudarmstadt.informatik.hostage.commons.SubnetUtils;
import de.tudarmstadt.informatik.hostage.location.MyLocationManager;
Expand All @@ -26,7 +24,6 @@
import de.tudarmstadt.informatik.hostage.nio.Reader;
import de.tudarmstadt.informatik.hostage.nio.Writer;
import de.tudarmstadt.informatik.hostage.protocol.GHOST;
import de.tudarmstadt.informatik.hostage.protocol.MQTT;
import de.tudarmstadt.informatik.hostage.protocol.Protocol;
import de.tudarmstadt.informatik.hostage.protocol.coapUtils.COAPHandler;
import de.tudarmstadt.informatik.hostage.protocol.mqttUtils.MQTTHandler;
Expand Down Expand Up @@ -293,14 +290,23 @@ public AttackRecord createAttackRecord() {
private boolean checkIfIsInternalAttack(){
int prefix = Hostage.prefix;
String internalIp = HelperUtils.intToStringIp(internalIPAddress);
String remoteIP = formatRemoteIP();

if(remoteIP.equals("127.0.0.1"))
return true;

SubnetUtils utils = new SubnetUtils(internalIp+"/"+prefix);

return utils.getInfo().isInRange(remoteIP);
}

private String formatRemoteIP(){
String remoteIP =client.getRemoteSocketAddress().toString();
remoteIP= remoteIP.substring(0, remoteIP.indexOf(":"));
if(remoteIP.startsWith("/"))
remoteIP= remoteIP.substring(1);
remoteIP= remoteIP.substring(1);

return utils.getInfo().isInRange(remoteIP);
return remoteIP;
}

/**
Expand Down Expand Up @@ -365,7 +371,7 @@ public void logCOAP(MessageRecord.TYPE type){
* InputStream of the socket.
* @param out
* OutputStream of the socket.
* @throws IOException
* @throws IOException if the Input or Output stream fails.
*/

protected void talkToClient(InputStream in, OutputStream out) throws IOException {
Expand Down
@@ -1,19 +1,12 @@
package de.tudarmstadt.informatik.hostage.net;

import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketImpl;

import javax.net.ServerSocketFactory;


import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
import de.tudarmstadt.informatik.hostage.system.Device;
import de.tudarmstadt.informatik.hostage.system.PrivilegedPort;


public class MyServerSocketFactory extends ServerSocketFactory {
Expand All @@ -28,20 +21,7 @@ public ServerSocket createServerSocket(int port) throws IOException {
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(ipAddress,port));
} else if (Device.isRooted()) {
// if (Device.isPorthackInstalled()) {
// FileDescriptor fd = new PrivilegedPort(PrivilegedPort.TYPE.TCP, port).getFD();
// socket = new ServerSocket();
// try {
// SocketImpl impl = getImpl(socket);
// injectFD(fd, impl);
// setBound(socket);
// } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
// e.printStackTrace();
// }
// }
if (Device.isPortRedirectionAvailable()) { // use ip tables
//Device.executePortRedirectionScript();

int redirectedPort = HelperUtils.getRedirectedPort(port);
socket = new ServerSocket();
socket.setReuseAddress(true);
Expand All @@ -61,7 +41,6 @@ public ServerSocket createServerSocket(int port, int backlog, InetAddress iAddre
return createServerSocket(port);
}


public String getIpAddress() {
return ipAddress;
}
Expand All @@ -70,25 +49,4 @@ public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}

private SocketImpl getImpl(ServerSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
Field implField = socket.getClass().getDeclaredField("impl");
implField.setAccessible(true);
return (SocketImpl) implField.get(socket);
}

private void injectFD(FileDescriptor fd, SocketImpl impl) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
Class<?> plainServerSocketImplClazz = impl.getClass();
Class<?> plainSocketImplClazz = plainServerSocketImplClazz.getSuperclass();
Class<?> socketImplClazz = plainSocketImplClazz.getSuperclass();
Field fdField = socketImplClazz.getDeclaredField("fd");
fdField.setAccessible(true);
fdField.set(impl, fd);
}

private void setBound(ServerSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
Field boundField = socket.getClass().getDeclaredField("isBound");
boundField.setAccessible(true);
boundField.set(socket, true);
}

}
Expand Up @@ -6,7 +6,6 @@
import com.mbed.coap.server.CoapExchange;
import com.mbed.coap.utils.CoapResource;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
Expand Down Expand Up @@ -89,12 +88,12 @@ public static void removeCurrentConnected(){

/**
* Helper method for Handler, creates an attackRecord with the logs from the InterceptHandler.
* @param attack_id
* @param externalIP
* @param protocol
* @param subnetMask
* @param BSSID
* @param internalIPAddress
* @param attack_id the attack_id
* @param externalIP the externalIp
* @param protocol the protocol
* @param subnetMask the subnet mask
* @param BSSID the BSSID
* @param internalIPAddress the internal IpAddress
* @return
* @throws UnknownHostException
*/
Expand Down Expand Up @@ -124,10 +123,11 @@ public synchronized static AttackRecord createAttackRecord(Long attack_id, Strin
}

private synchronized static boolean checkIfIsInternalAttack(String remoteIPAddress,String internalIPAddress){
if(remoteIPAddress.equals("127.0.0.1"))
return true;
int prefix = Hostage.prefix;
SubnetUtils utils = new SubnetUtils(internalIPAddress+"/"+prefix);
System.out.println("RemoteIp: "+remoteIPAddress+" "+"InternalIp: "+internalIPAddress
+"/"+prefix +" "+"Check: "+utils.getInfo().isInRange(remoteIPAddress));

return utils.getInfo().isInRange(remoteIPAddress);
}

Expand Down
Expand Up @@ -334,6 +334,8 @@ public synchronized static AttackRecord createAttackRecord(Long attack_id, Strin
}

private synchronized static boolean checkIfIsInternalAttack(String remoteIPAddress,String internalIPAddress){
if(remoteIPAddress.equals("127.0.0.1"))
return true;
int prefix = Hostage.prefix;
SubnetUtils utils = new SubnetUtils(internalIPAddress+"/"+prefix);

Expand Down
Expand Up @@ -110,102 +110,97 @@ public View getView(final int position, View convertView, ViewGroup parent) {
}

holder.activated.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ServicesListItem item = (ServicesListItem) buttonView.getTag();
try {
mProfile = ProfileManager.getInstance().getCurrentActivatedProfile();
} catch (Exception e) {
e.printStackTrace();
}
//SK: Temp bugfix
//if (!HelperUtils.isNetworkAvailable(mActivity)) {
if (isChecked && !HelperUtils.isWifiConnected(mActivity)) {
if(!MainActivity.getInstance().getHostageService().hasRunningListeners()) {
new AlertDialog.Builder(mActivity)
.setTitle(R.string.information)
.setMessage(R.string.wifi_not_connected_msg)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}
)
.setIcon(android.R.drawable.ic_dialog_info).show();
if (buttonView.isChecked()) {
buttonView.setChecked(false);
}
(buttonView, isChecked) -> {
ServicesListItem item1 = (ServicesListItem) buttonView.getTag();
try {
mProfile = ProfileManager.getInstance().getCurrentActivatedProfile();
} catch (Exception e) {
e.printStackTrace();
}
//SK: Temp bugfix
//if (!HelperUtils.isNetworkAvailable(mActivity)) {
if (isChecked && !HelperUtils.isWifiConnected(mActivity)) {
if(!MainActivity.getInstance().getHostageService().hasRunningListeners()) {
new AlertDialog.Builder(mActivity)
.setTitle(R.string.information)
.setMessage(R.string.wifi_not_connected_msg)
.setPositiveButton(android.R.string.ok,
(dialog, which) -> {
}
)
.setIcon(android.R.drawable.ic_dialog_info).show();
if (buttonView.isChecked()) {
buttonView.setChecked(false);
}
} else {
//check if switch is set to ON and start the concrete listener for the protocol
if (isChecked) {
if(item.protocol.equals("GHOST")){
if(mProfile.mGhostActive){
mGhostPorts = mProfile.getGhostPorts();

if(mGhostPorts.length != 0) {
for(Integer port: mGhostPorts){
if(!MainActivity.getInstance().getHostageService().isRunning(item.protocol, port)) {
MainActivity.getInstance().getHostageService().startListener(item.protocol, port);
}
}
} else {
//check if switch is set to ON and start the concrete listener for the protocol
if (isChecked) {
if(item1.protocol.equals("GHOST")){
if(mProfile.mGhostActive){
mGhostPorts = mProfile.getGhostPorts();

if(mGhostPorts.length != 0) {
for(Integer port: mGhostPorts){
if(!MainActivity.getInstance().getHostageService().isRunning(item1.protocol, port)) {
MainActivity.getInstance().getHostageService().startListener(item1.protocol, port);
}
//set the main switch to null, so that he won't react and starts all protocols
mServicesSwitch.setOnCheckedChangeListener(null);
mServicesSwitch.setChecked(true);
mServicesSwitch.setOnCheckedChangeListener(mListener);
}
//set the main switch to null, so that he won't react and starts all protocols
mServicesSwitch.setOnCheckedChangeListener(null);
mServicesSwitch.setChecked(true);
mServicesSwitch.setOnCheckedChangeListener(mListener);

if(!buttonView.isChecked()) {
buttonView.setChecked(true);
}
if(!buttonView.isChecked()) {
buttonView.setChecked(true);
}
}
else {
if(buttonView.isChecked()) {
buttonView.setChecked(false);
}
}
else {
if(buttonView.isChecked()) {
buttonView.setChecked(false);
}
}
else if (!MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
boolean success = MainActivity.getInstance().getHostageService().startListener(item.protocol);

if (success) {
//set the main switch to null, so that he won't react and starts all protocols
mServicesSwitch.setOnCheckedChangeListener(null);
mServicesSwitch.setChecked(true);
mServicesSwitch.setOnCheckedChangeListener(mListener);
if (!buttonView.isChecked()) {
buttonView.setChecked(true);
}
} else {
buttonView.setChecked(false);
}
} else {
if(!buttonView.isChecked()) {
}
else if (!MainActivity.getInstance().getHostageService().isRunning(item1.protocol)) {
boolean success = MainActivity.getInstance().getHostageService().startListener(item1.protocol);

if (success) {
//set the main switch to null, so that he won't react and starts all protocols
mServicesSwitch.setOnCheckedChangeListener(null);
mServicesSwitch.setChecked(true);
mServicesSwitch.setOnCheckedChangeListener(mListener);
if (!buttonView.isChecked()) {
buttonView.setChecked(true);
}
} else {
buttonView.setChecked(false);
}
} else {
if(item.protocol.equals("GHOST")) {
mGhostPorts = mProfile.getGhostPorts();
for(Integer port: mGhostPorts){
if(port != null) {
if(MainActivity.getInstance().getHostageService().isRunning("GHOST",port)){
MainActivity.getInstance().getHostageService().stopListener("GHOST", port);
}
if(!buttonView.isChecked()) {
buttonView.setChecked(true);
}
}
} else {
if(item1.protocol.equals("GHOST")) {
mGhostPorts = mProfile.getGhostPorts();
for(Integer port: mGhostPorts){
if(port != null) {
if(MainActivity.getInstance().getHostageService().isRunning("GHOST",port)){
MainActivity.getInstance().getHostageService().stopListener("GHOST", port);
}
}
if(buttonView.isChecked()) {
buttonView.setChecked(false);
}
}
else if (MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
MainActivity.getInstance().getHostageService().stopListener(item.protocol);
}
if(buttonView.isChecked()) {
buttonView.setChecked(false);
}
}
if(buttonView.isChecked()) {
buttonView.setChecked(false);
}
}
else if (MainActivity.getInstance().getHostageService().isRunning(item1.protocol)) {
MainActivity.getInstance().getHostageService().stopListener(item1.protocol);
}
if(buttonView.isChecked()) {
buttonView.setChecked(false);
}
}
}
}
Expand Down

0 comments on commit ade45bc

Please sign in to comment.