123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- package de.tudarmstadt.informatik.hostage.sync.android;
- import android.accounts.Account;
- import android.accounts.AccountManager;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.util.Log;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpVersion;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.conn.ClientConnectionManager;
- import org.apache.http.conn.scheme.PlainSocketFactory;
- import org.apache.http.conn.scheme.Scheme;
- import org.apache.http.conn.scheme.SchemeRegistry;
- import org.apache.http.conn.ssl.SSLSocketFactory;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
- import org.apache.http.params.BasicHttpParams;
- import org.apache.http.params.HttpParams;
- import org.apache.http.params.HttpProtocolParams;
- import org.apache.http.protocol.HTTP;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.Writer;
- import java.security.KeyStore;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import de.tudarmstadt.informatik.hostage.logging.Record;
- import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
- /**
- * Created by abrakowski
- */
- public class SyncUtils {
- private static final long SYNC_FREQUENCY = 60 * 60; // 1 hour (in seconds)
- public static final String CONTENT_AUTHORITY = "de.tudarmstadt.informatik.hostage.androidsync";
- private static final String PREF_SETUP_COMPLETE = "sync_setup_complete";
- private static final Map<String, Integer> protocolsTypeMap;
- static {
- protocolsTypeMap = new HashMap<String, Integer>();
- protocolsTypeMap.put("ECHO", 10);
- protocolsTypeMap.put("FTP", 0);
- protocolsTypeMap.put("GHOST", 0);
- protocolsTypeMap.put("HTTP", 0);
- protocolsTypeMap.put("HTTPS", 0);
- protocolsTypeMap.put("MySQL", 31);
- protocolsTypeMap.put("SIP", 50);
- protocolsTypeMap.put("SMB", 40);
- protocolsTypeMap.put("TELNET", 0);
- }
- /**
- * Create an entry for this application in the system account list, if it isn't already there.
- *
- * @param context Context
- */
- public static void CreateSyncAccount(Context context) {
- boolean newAccount = false;
- boolean setupComplete = PreferenceManager
- .getDefaultSharedPreferences(context).getBoolean(PREF_SETUP_COMPLETE, false);
- // Create account, if it's missing. (Either first run, or user has deleted account.)
- Account account = HostageAccountService.GetAccount();
- AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
- if (accountManager.addAccountExplicitly(account, null, null)) {
- // Inform the system that this account supports sync
- ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
- // Inform the system that this account is eligible for auto sync when the network is up
- ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
- // Recommend a schedule for automatic synchronization. The system may modify this based
- // on other scheduled syncs and network utilization.
- ContentResolver.addPeriodicSync(
- account, CONTENT_AUTHORITY, new Bundle(),SYNC_FREQUENCY);
- newAccount = true;
- }
- // Schedule an initial sync if we detect problems with either our account or our local
- // data has been deleted. (Note that it's possible to clear app data WITHOUT affecting
- // the account list, so wee need to check both.)
- if (newAccount || !setupComplete) {
- TriggerRefresh();
- PreferenceManager.getDefaultSharedPreferences(context).edit()
- .putBoolean(PREF_SETUP_COMPLETE, true).commit();
- }
- }
- public static void TriggerRefresh() {
- Bundle b = new Bundle();
- // Disable sync backoff and ignore sync preferences. In other words...perform sync NOW!
- b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
- b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
- ContentResolver.requestSync(
- HostageAccountService.GetAccount(), // Sync account
- CONTENT_AUTHORITY, // Content authority
- b); // Extras
- }
- public static void appendRecordToStringWriter(Record record, Writer stream){
- try {
- stream.append(
- "{" +
- "\"sensor\":{" +
- "\"name\":\"HosTaGe\"," +
- "\"type\":\"Honeypot\"" +
- "}," +
- "\"src\":{" +
- "\"ip\":\"" + record.getRemoteIP() + "\"," +
- "\"port\":" + record.getRemotePort() +
- "}," +
- "\"dst\":{" +
- "\"ip\":\"" + record.getExternalIP() /*record.getLocalIP()*/ + "\"," +
- "\"port\":" + record.getLocalPort() +
- "}," +
- "\"type\":" + (protocolsTypeMap.containsKey(record.getProtocol()) ? protocolsTypeMap.get(record.getProtocol()) : 0) + "," +
- "\"log\":\"" + record.getProtocol() + "\"," +
- "\"md5sum\":\"\"," +
- "\"date\":" + (int)(record.getTimestamp() / 1000) +
- "}\n"
- );
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static boolean uploadRecordsToServer(String entity, String serverAddress){
- HttpPost httppost;
- try {
- HttpClient httpClient = createHttpClient();
- // Create HttpPost
- httppost = new HttpPost(serverAddress);
- StringEntity se = new StringEntity(entity);
- httppost.addHeader("content-type", "application/json+newline");
- httppost.setEntity(se);
- // Execute HttpPost
- HttpResponse response = httpClient.execute(httppost);
- if(response.getStatusLine().getStatusCode() >= 400 && response.getStatusLine().getStatusCode() < 600){
- return false;
- }
- Log.i("TracingSyncService", "Status Code: " + response.getStatusLine().getStatusCode());
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public static List<String[]> getCountriesFromServer(String serverAddress){
- HttpGet httpget;
- List<String[]> ret = new ArrayList<String[]>();
- try {
- HttpClient httpClient = createHttpClient();
- // Create HttpPost
- httpget = new HttpGet(serverAddress + "/get_countries");
- httpget.addHeader("content-type", "application/json+newline");
- // Execute HttpPost
- HttpResponse response = httpClient.execute(httpget);
- if(response.getStatusLine().getStatusCode() >= 400 && response.getStatusLine().getStatusCode() < 600){
- return null;
- }
- BufferedReader bReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- String line;
- StringBuilder builder = new StringBuilder();
- while ((line = bReader.readLine()) != null) {
- builder.append(line);
- }
- JSONArray array = new JSONArray(builder.toString());
- for(int i = 0; i < array.length(); i++){
- JSONObject ob = array.getJSONObject(i);
- ret.add(new String[]{ob.getString("cc"), ob.getString("country")});
- }
- Log.i("TracingSyncService", "Status Code: " + response.getStatusLine().getStatusCode());
- return ret;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- public static HttpClient createHttpClient() {
- try {
- KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
- trustStore.load(null, null);
- SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
- sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- HttpParams params = new BasicHttpParams();
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
- HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
- registry.register(new Scheme("https", sf, 443));
- ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
- return new DefaultHttpClient(ccm, params);
- } catch (Exception e) {
- e.printStackTrace();
- return new DefaultHttpClient();
- }
- }
- }
|