Skip to content

Commit

Permalink
Fixed SensorProfile logging issues+Added MQTTConfig class which enabl…
Browse files Browse the repository at this point in the history
…es the ip and port modification.
  • Loading branch information
irinil committed Jul 12, 2020
1 parent 3178707 commit 8a6c3a2
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 123 deletions.
20 changes: 13 additions & 7 deletions build.gradle
Expand Up @@ -4,7 +4,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add plugin
classpath 'com.google.gms:google-services:4.3.3' //added gms support
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0'
Expand Down Expand Up @@ -58,20 +58,21 @@ dependencies {
implementation 'org.snmp4j:snmp4j:1.10.1'
implementation files('libs/snmp4j-agent-2.3.3.jar')
implementation files('libs/VirusTotalAPI.jar')

//MQTT Broker Cassandana
//implementation files('libs/cassandana-jar-with-dependencies.jar')
//materialDesign
implementation 'com.google.android.material:material:1.3.0-alpha01'

//leakcanary
//debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'


// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
//testImplementation 'androidx.de.tudarmstadt.informatik.hostage.test:core:1.2.0'

// Optional -- Mockito framework
testImplementation "org.mockito:mockito-core:2.+"
//testImplementation "org.mockito:mockito-core:2.+"
implementation 'com.android.support:support-annotations:28.0.0'
androidTestImplementation 'androidx.test:rules:1.3.0-rc01'
androidTestImplementation 'androidx.test:runner:1.3.0-rc01'
Expand All @@ -93,19 +94,24 @@ greendao{
}
//this is for the conflict of common apache libraries between moquette and VirusTotalApi
configurations {
all*.exclude group: 'commons-codec', module: 'commons-codec'}
all*.exclude group: 'commons-codec', module: 'commons-codec'
}

android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
buildToolsVersion '29.0.3'
defaultConfig {
versionCode 7
versionName "2.0.0"
minSdkVersion 24
targetSdkVersion 28
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
useLibrary 'org.apache.http.legacy'
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath true
}
}

// signingConfig signingConfigs.config

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Thu May 21 21:14:40 EEST 2020
#Sat Jul 11 01:58:35 EEST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=hostage.wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=hostage.wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
1 change: 1 addition & 0 deletions greenDaoGenerator/.gitignore
@@ -0,0 +1 @@
/build
9 changes: 9 additions & 0 deletions greenDaoGenerator/build.gradle
@@ -0,0 +1,9 @@
apply plugin: 'java-library'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

}

sourceCompatibility = "7"
targetCompatibility = "7"
4 changes: 2 additions & 2 deletions src/main/java/de/tudarmstadt/informatik/hostage/Handler.java
Expand Up @@ -176,7 +176,7 @@ private void uploadTracing(){
public void run() {
service.notifyUI(this.getClass().getName(),
new String[] { service.getString(R.string.broadcast_started), protocol.toString(), Integer.toString(listener.getPort()) });
if(MQTTHandler.isAnAttackOngoing()){
if(protocol.toString().equals("MQTT")){
try {
handleMQTTPackets();
} catch (UnknownHostException e) {
Expand Down Expand Up @@ -216,7 +216,7 @@ private synchronized void getAndIncrementAttackID(SharedPreferences pref) {
Editor editor = pref.edit();
attack_id = pref.getLong("ATTACK_ID_COUNTER", 0);
editor.putLong("ATTACK_ID_COUNTER", attack_id + 1);
editor.commit();
editor.apply();
}

/**
Expand Down
59 changes: 24 additions & 35 deletions src/main/java/de/tudarmstadt/informatik/hostage/Hostage.java
Expand Up @@ -39,12 +39,9 @@
import android.util.Log;
import android.widget.Toast;


import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
import de.tudarmstadt.informatik.hostage.location.MyLocationManager;

import de.tudarmstadt.informatik.hostage.logging.DaoSession;
import de.tudarmstadt.informatik.hostage.persistence.DAO.AttackRecordDAO;
import de.tudarmstadt.informatik.hostage.protocol.Protocol;
Expand Down Expand Up @@ -72,6 +69,9 @@ public class Hostage extends Service {
MultiStageAlarm alarm = new MultiStageAlarm();
private DaoSession dbSession;
public static int prefix;
boolean activeHandlers = false;
boolean bssidSeen = false;
boolean listening = false;

public class LocalBinder extends Binder {
public Hostage getService() {
Expand Down Expand Up @@ -310,13 +310,10 @@ public void onCreate() {

createNotification();
registerNetReceiver();
updateConnectionInfo();
Device.checkCapabilities();

updateConnectionInfo();
}



@Override
public void onDestroy() {
cancelNotification();
Expand All @@ -325,8 +322,6 @@ public void onDestroy() {
super.onDestroy();
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
Expand Down Expand Up @@ -544,25 +539,9 @@ private synchronized void createNotification() {
return; // prevent NullPointerException
}

dbSession = HostageApplication.getInstances().getDaoSession();

AttackRecordDAO attackRecordDAO = new AttackRecordDAO(dbSession);
boolean activeHandlers = false;
boolean bssidSeen = false;
boolean listening = false;
for (Listener listener : listeners) {
if (listener.isRunning())
listening = true;
if (listener.getHandlerCount() > 0) {
activeHandlers = true;
}
if (attackRecordDAO.bssidSeen(listener.getProtocolName(), getBSSID(getApplicationContext()))) {
bssidSeen = true;
}
}

checkNetworkPreviousInfection();

PendingIntent resultPendingIntent = intentNotificationGenerator();
PendingIntent resultPendingIntent = intentNotificationGenerator();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Under Attack";
Expand All @@ -574,11 +553,8 @@ private synchronized void createNotification() {

mNotificationManager.createNotificationChannel(channel);
Notification.Builder notificationBuilder = new Notification.Builder(this,"42");

notificationBuilder.setContentTitle(getString(R.string.app_name)).setWhen(System.currentTimeMillis());

notificationIconBuilder( listening, activeHandlers, bssidSeen, notificationBuilder);

notificationIconBuilder(listening, activeHandlers, bssidSeen, notificationBuilder);
notificationBuilder.setContentIntent(resultPendingIntent);

Notification notification = notificationBuilder.setOngoing(true)
Expand All @@ -589,14 +565,30 @@ private synchronized void createNotification() {
}
}

private void checkNetworkPreviousInfection(){
dbSession = HostageApplication.getInstances().getDaoSession();
AttackRecordDAO attackRecordDAO = new AttackRecordDAO(dbSession);

for (Listener listener : listeners) {
if (listener.isRunning())
listening = true;
if (listener.getHandlerCount() > 0) {
activeHandlers = true;
}
if (attackRecordDAO.bssidSeen(listener.getProtocolName(), getBSSID(getApplicationContext()))) {
bssidSeen = true;
}
}

}

/**
* Selects the appropriate icon for the notification.
* @param listening listener for protocols
* @param activeHandlers active Handlers
* @param bssidSeen checks if this bssid is already seen
* @param notificationBuilder builds the notification
*/

private void notificationIconBuilder(boolean listening,boolean activeHandlers,boolean bssidSeen,Notification.Builder notificationBuilder){
if (!listening) {
notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
Expand All @@ -617,9 +609,7 @@ private void notificationIconBuilder(boolean listening,boolean activeHandlers,bo
* Generates the intent for the notification.
* @return the pending Intent
*/

private PendingIntent intentNotificationGenerator(){

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);

Expand Down Expand Up @@ -788,7 +778,6 @@ private void findExternalIp(){
*
* @see MainActivity #CONNECTION_INFO
*/

public void updateEditor(String ssid, String bssid, int ipAddress, int netmask){
SharedPreferences pref = context.getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);

Expand Down
13 changes: 0 additions & 13 deletions src/main/java/de/tudarmstadt/informatik/hostage/Listener.java
Expand Up @@ -216,8 +216,6 @@ private boolean notifyUI(boolean running){
* running in and notifies the background service.
*/
public void stop() {
if(stopMqttBroker())
return;
try {
server.close();
thread.interrupt();
Expand All @@ -227,16 +225,6 @@ public void stop() {
}
}

public boolean stopMqttBroker(){
if(port == mqttport) {
MQTT.brokerStop();
notifyUI(false);
return true;
}
return false;
}


/**
* Waits for an incoming connection, accepts it and starts a {@link Handler}
*/
Expand All @@ -253,7 +241,6 @@ private void fullHandler() throws IOException {

private Thread socketsThread(Socket client){
socketsThread = new Thread(new Runnable() {

@Override
public void run() {
try {
Expand Down
42 changes: 31 additions & 11 deletions src/main/java/de/tudarmstadt/informatik/hostage/MQTTListener.java
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
Expand Down Expand Up @@ -106,27 +108,25 @@ private boolean notifyUI(boolean running){
@Override
public void stop() { stopMqttBroker();}

public boolean stopMqttBroker(){
public void stopMqttBroker(){
if(super.getPort() == mqttport) {
MQTT.brokerStop();
if(brokerThread!=null)
brokerThread.interrupt();
if(thread!=null)
thread.interrupt();
notifyUI(false);
return true;
}
return false;
}

private void fullHandler() throws IOException {
private void fullHandler() throws Exception {
if (conReg.isConnectionFree()) {
ExecutorService threadPool = Executors.newFixedThreadPool(1);

Thread brokerThread = brokerThread();
threadPool.submit(brokerThread);
startsMonitoringProfile();
threadPool.shutdown();

}
}

Expand All @@ -135,8 +135,6 @@ private Thread brokerThread(){
@Override
public void run() {
try {
monitorSensorProfile();

if (ConnectionGuard.portscanInProgress())
return;

Expand All @@ -150,7 +148,7 @@ public void run() {
if (ConnectionGuard.portscanInProgress())
return;

isTopicPublished(); //The record wont get updated.
isTopicPublished();
if(MQTTHandler.isAnAttackOngoing()) {
startHandler();
conReg.newOpenConnection();
Expand All @@ -165,13 +163,35 @@ public void run() {

}

private void monitorSensorProfile() throws Exception {
private void startsMonitoringProfile() throws Exception {
if(ProfileManager.getInstance().getCurrentActivatedProfile().mId == 14){
SensorProfile sensorProfile = new SensorProfile();
sensorProfile.startSensor();
Timer timer = scheduleMonitorSensorProfile();
TimeUnit.SECONDS.sleep(5); //this method is on the loop, so it is necessary to wait until it stops.
timer.cancel();
}
}

private Timer scheduleMonitorSensorProfile(){
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
monitorSensorProfile();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 3000, 4000 );//milliseconds

return timer;
}

private void monitorSensorProfile() throws Exception {
SensorProfile sensorProfile = new SensorProfile();
sensorProfile.startSensor();
}

private boolean checkPostScanInProgress() throws IOException {
if (ConnectionGuard.portscanInProgress()) {
mutex.release();
Expand Down
Expand Up @@ -11,7 +11,6 @@
import de.tudarmstadt.informatik.hostage.logging.MessageRecordDao;
import de.tudarmstadt.informatik.hostage.logging.Record;
import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
import io.moquette.logging.LoggingUtils;


public class MessageRecordDAO extends DAO {
Expand Down

0 comments on commit 8a6c3a2

Please sign in to comment.