|
@@ -1,16 +1,26 @@
|
|
package de.tudarmstadt.informatik.hostage.ui.fragment;
|
|
package de.tudarmstadt.informatik.hostage.ui.fragment;
|
|
|
|
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
+import java.util.regex.Matcher;
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
import android.app.Activity;
|
|
import android.app.Activity;
|
|
import android.app.AlertDialog;
|
|
import android.app.AlertDialog;
|
|
|
|
+import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.content.DialogInterface;
|
|
|
|
+import android.content.SharedPreferences;
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
|
+import android.os.Environment;
|
|
|
|
+import android.preference.PreferenceManager;
|
|
import android.text.format.DateFormat;
|
|
import android.text.format.DateFormat;
|
|
import android.view.LayoutInflater;
|
|
import android.view.LayoutInflater;
|
|
import android.view.Menu;
|
|
import android.view.Menu;
|
|
import android.view.MenuInflater;
|
|
import android.view.MenuInflater;
|
|
|
|
+import android.view.MenuItem;
|
|
import android.view.MotionEvent;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.ViewGroup;
|
|
@@ -19,6 +29,8 @@ import android.widget.ImageButton;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.ScrollView;
|
|
import android.widget.ScrollView;
|
|
import android.widget.TextView;
|
|
import android.widget.TextView;
|
|
|
|
+import android.widget.Toast;
|
|
|
|
+
|
|
import de.tudarmstadt.informatik.hostage.R;
|
|
import de.tudarmstadt.informatik.hostage.R;
|
|
import de.tudarmstadt.informatik.hostage.logging.Record;
|
|
import de.tudarmstadt.informatik.hostage.logging.Record;
|
|
import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
|
|
import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
|
|
@@ -43,7 +55,7 @@ public class RecordDetailFragment extends UpNavigatibleFragment {
|
|
/**
|
|
/**
|
|
* The database helper to retrieve data from the database
|
|
* The database helper to retrieve data from the database
|
|
*/
|
|
*/
|
|
- private HostageDBOpenHelper mDBOpenHelper;
|
|
|
|
|
|
+ public HostageDBOpenHelper mDBOpenHelper;
|
|
|
|
|
|
/**
|
|
/**
|
|
* The layout inflater
|
|
* The layout inflater
|
|
@@ -62,6 +74,10 @@ public class RecordDetailFragment extends UpNavigatibleFragment {
|
|
private TextView mRecordDetailsTextProtocol;
|
|
private TextView mRecordDetailsTextProtocol;
|
|
private ImageButton mRecordDeleteButton;
|
|
private ImageButton mRecordDeleteButton;
|
|
|
|
|
|
|
|
+ public SharedPreferences pref;
|
|
|
|
+ public int port;
|
|
|
|
+ public StringBuilder portArray;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Sets the record of which the details should be displayed
|
|
* Sets the record of which the details should be displayed
|
|
* @param rec the record to be used
|
|
* @param rec the record to be used
|
|
@@ -93,6 +109,7 @@ public class RecordDetailFragment extends UpNavigatibleFragment {
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
super.onCreate(savedInstanceState);
|
|
setHasOptionsMenu(true);
|
|
setHasOptionsMenu(true);
|
|
|
|
+ pref = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -213,7 +230,7 @@ public class RecordDetailFragment extends UpNavigatibleFragment {
|
|
.setPositiveButton(R.string.yes,
|
|
.setPositiveButton(R.string.yes,
|
|
new DialogInterface.OnClickListener() {
|
|
new DialogInterface.OnClickListener() {
|
|
public void onClick(DialogInterface dialog,
|
|
public void onClick(DialogInterface dialog,
|
|
- int which) {
|
|
|
|
|
|
+ int which) {
|
|
mDBOpenHelper.deleteByAttackID(mRecord.getAttack_id());
|
|
mDBOpenHelper.deleteByAttackID(mRecord.getAttack_id());
|
|
|
|
|
|
MainActivity.getInstance().navigateBack();
|
|
MainActivity.getInstance().navigateBack();
|
|
@@ -225,6 +242,235 @@ public class RecordDetailFragment extends UpNavigatibleFragment {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
+ // Inflate the menu items for use in the action bar
|
|
|
|
+ if (mRecord.getProtocol().contains("HTTP")){
|
|
|
|
+ inflater.inflate(R.menu.records_detail_actions, menu);
|
|
|
|
+ }
|
|
|
|
+ else if (mRecord.getProtocol().contains("MODBUS")){
|
|
|
|
+ inflater.inflate(R.menu.records_detail_actions, menu);
|
|
|
|
+ }
|
|
|
|
+ else if (mRecord.getProtocol().contains("MULTISTAGE")){
|
|
|
|
+ inflater.inflate(R.menu.records_detail_actions, menu);
|
|
|
|
+ }
|
|
|
|
+ else if (mRecord.getProtocol().contains("FILE INJECTION")){
|
|
|
|
+ inflater.inflate(R.menu.records_detail_actions, menu);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
+ switch (item.getItemId()) {
|
|
|
|
+ case R.id.bro_sig:
|
|
|
|
+ AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
|
|
|
|
+ builder.setTitle(MainActivity.getInstance().getString(R.string.bro_signature));
|
|
|
|
+ builder.setMessage(MainActivity.getInstance().getString(R.string.bro_message));
|
|
|
|
+
|
|
|
|
+ builder.setPositiveButton(R.string.generate,
|
|
|
|
+ new DialogInterface.OnClickListener() {
|
|
|
|
+ public void onClick(DialogInterface dialog,
|
|
|
|
+ int which) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ getConversation();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //mDBOpenHelper.deleteByAttackID(mRecord.getAttack_id());
|
|
|
|
+
|
|
|
|
+ MainActivity.getInstance().navigateBack();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ).setNegativeButton(R.string.cancel, null);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ builder.create();
|
|
|
|
+ builder.show();
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public int protocol2Port(String protocol){
|
|
|
|
+
|
|
|
|
+ if(protocol.contains("HTTP")){port=80;}
|
|
|
|
+ else if(protocol.contains("MODBUS")){port=502;}
|
|
|
|
+ else if(protocol.contains("TELNET")){port=23;}
|
|
|
|
+ else if(protocol.contains("SMB")){port=80;}
|
|
|
|
+ else if(protocol.contains("HTTPS")){port=443;}
|
|
|
|
+ else if(protocol.contains("ECHO")){port=7;}
|
|
|
|
+ else if(protocol.contains("FTP")){port=21;}
|
|
|
|
+ else if(protocol.contains("MySQL")){port=3306;}
|
|
|
|
+ else if(protocol.contains("S7COMM")){port=102;}
|
|
|
|
+ else if(protocol.contains("SIP")){port=1025;}
|
|
|
|
+ else if(protocol.contains("SMTP")){port=25;}
|
|
|
|
+ else if(protocol.contains("SNMP")){port=161;}
|
|
|
|
+ else if(protocol.contains("SSH")){port=22;}
|
|
|
|
+ return port;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void getConversation() throws IOException {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ ArrayList<Record> conversation = this.mDBOpenHelper.getConversationForAttackID(mRecord.getAttack_id());
|
|
|
|
+ for (Record r : conversation) {
|
|
|
|
+
|
|
|
|
+ String mydata = r.getPacket();
|
|
|
|
+ ArrayList<String> myTokensList = new ArrayList<String>();
|
|
|
|
+ String tokens[]=mydata.split("\n");
|
|
|
|
+ for (String tok : tokens) {
|
|
|
|
+ if (tok.contains("Protocol:")) {
|
|
|
|
+ myTokensList.add(tok.split(":")[1]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ ArrayList<Integer> myPortList = new ArrayList<Integer>();
|
|
|
|
+
|
|
|
|
+ //Disaplay the protocols/services
|
|
|
|
+ for (String tok : myTokensList) {
|
|
|
|
+ myPortList.add(protocol2Port(tok));
|
|
|
|
+ }
|
|
|
|
+ System.out.print(myPortList);
|
|
|
|
+ String signature = createSignature(r.getRemoteIP(),myPortList);
|
|
|
|
+ createSignatureFile(signature);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String createSignature(String ip,ArrayList portList) {
|
|
|
|
+
|
|
|
|
+ int portListSize=0;
|
|
|
|
+ StringBuilder portArray = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ for (Object tok : portList) {
|
|
|
|
+
|
|
|
|
+ portArray.append(tok+"/tcp");
|
|
|
|
+ portListSize++;
|
|
|
|
+ if(portListSize!=portList.size()){
|
|
|
|
+ portArray.append(",");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String defaultSignature = "@load base/frameworks/notice\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "export{\n" +
|
|
|
|
+ "\tredef enum Notice::Type += {\n" +
|
|
|
|
+ "\t\tMultistage\n" +
|
|
|
|
+ "\t};\n" +
|
|
|
|
+ "}\n" +
|
|
|
|
+ "global attack_ip ="+ ip+";\n" +
|
|
|
|
+ "global attack_port = set("+portArray+");\n" +
|
|
|
|
+ "global attack_count = 0;\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "event connection_established(c: connection)\n" +
|
|
|
|
+ "{\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "print fmt (\"Initiating.............\");\n" +
|
|
|
|
+ "print c$id$orig_h;\n" +
|
|
|
|
+ "print c$id$resp_p;\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "for (i in attack_port){\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "\tif(count==0){\n" +
|
|
|
|
+ "\t\t\n" +
|
|
|
|
+ "\tif ((c$id$orig_h==attack_ip) && (c$id$resp_p==attack_prot1))\n" +
|
|
|
|
+ " {\n" +
|
|
|
|
+ "\tprint fmt(\"Inside the loop\");\n" +
|
|
|
|
+ " ++attack_count;\n" +
|
|
|
|
+ "\tprint attack_count;\n" +
|
|
|
|
+ " }\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "\telse{break;}\n" +
|
|
|
|
+ "\t}\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ " \n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "}\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ " else {\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ " if ((c$id$orig_h==attack_ip) && (c$id$resp_p == attack_prot2)){\n" +
|
|
|
|
+ "\t\n" +
|
|
|
|
+ "\tprint fmt (\"MULTISTAGE ATTACK!!!\");\n" +
|
|
|
|
+ " NOTICE([$note = Multistage,\n" +
|
|
|
|
+ " $conn = c,\n" +
|
|
|
|
+ " $msg = fmt(\"Multistage Attack! from %s\",c$id$orig_h)]);\n" +
|
|
|
|
+ "\tattack_count = 0;\n" +
|
|
|
|
+ "\t\n" +
|
|
|
|
+ " }\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ " }\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "\n" +
|
|
|
|
+ "}\n";
|
|
|
|
+
|
|
|
|
+ return defaultSignature;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void createSignatureFile(String signature) throws IOException {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ FileOutputStream sig;
|
|
|
|
+ Long tsLong = System.currentTimeMillis() / 1000;
|
|
|
|
+ String ts = tsLong.toString();
|
|
|
|
+ String fileName = "Bro_sig"+ts+".bro";
|
|
|
|
+ String externalLocation = pref.getString("pref_external_location", "");
|
|
|
|
+ String root = Environment.getExternalStorageDirectory().toString();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (root != null && isExternalStorageWritable()) {
|
|
|
|
+ File dir = new File(root + externalLocation);
|
|
|
|
+ dir.mkdirs();
|
|
|
|
+ File file = new File(dir, fileName);
|
|
|
|
+ sig = new FileOutputStream(file);
|
|
|
|
+ sig.write(signature.getBytes());
|
|
|
|
+ sig.write(System.getProperty("line.separator").getBytes());
|
|
|
|
+ sig.flush();
|
|
|
|
+ sig.close();
|
|
|
|
+ Toast.makeText(this.getActivity().getApplicationContext(),"Signature file"+fileName +"created",Toast.LENGTH_LONG).show();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ Toast.makeText(this.getActivity(),"Could not write to SD Card",Toast.LENGTH_SHORT).show();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private boolean isExternalStorageWritable() {
|
|
|
|
+ String state = Environment.getExternalStorageState();
|
|
|
|
+ if (Environment.MEDIA_MOUNTED.equals(state)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
/*****************************
|
|
/*****************************
|
|
*
|
|
*
|
|
* Date Transform
|
|
* Date Transform
|