forked from cSploit/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystem.java
More file actions
1226 lines (977 loc) · 32.4 KB
/
Copy pathSystem.java
File metadata and controls
1226 lines (977 loc) · 32.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the dSploit.
*
* Copyleft of Simone Margaritelli aka evilsocket <evilsocket@gmail.com>
*
* dSploit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dSploit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dSploit. If not, see <http://www.gnu.org/licenses/>.
*/
package org.csploit.android.core;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.Build;
import android.os.Environment;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.preference.PreferenceManager;
import android.util.SparseIntArray;
import org.acra.ACRA;
import org.acra.ACRAConfiguration;
import org.apache.commons.compress.utils.IOUtils;
import org.csploit.android.R;
import org.csploit.android.WifiScannerActivity;
import org.csploit.android.gui.dialogs.FatalDialog;
import org.csploit.android.helpers.NetworkHelper;
import org.csploit.android.helpers.ThreadHelper;
import org.csploit.android.net.GitHubParser;
import org.csploit.android.net.Network;
import org.csploit.android.net.RemoteReader;
import org.csploit.android.net.Target;
import org.csploit.android.net.Target.Exploit;
import org.csploit.android.net.http.proxy.HTTPSRedirector;
import org.csploit.android.net.http.proxy.Proxy;
import org.csploit.android.net.http.server.Server;
import org.csploit.android.net.metasploit.MsfExploit;
import org.csploit.android.net.metasploit.Payload;
import org.csploit.android.net.metasploit.RPCClient;
import org.csploit.android.net.metasploit.Session;
import org.csploit.android.services.Services;
import org.csploit.android.tools.ToolBox;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NoRouteToHostException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Observer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class System
{
public static final String SESSION_MAGIC = "cSploitSession";
private static final String ERROR_LOG_FILENAME = "csploit-debug-error.log";
private static final Pattern SERVICE_PARSER = Pattern.compile("^([^\\s]+)\\s+(\\d+).*$", Pattern.CASE_INSENSITIVE);
public static int HTTP_PROXY_PORT = 8080;
public static int HTTP_SERVER_PORT = 8081;
public static int HTTPS_REDIR_PORT = 8082;
public static int MSF_RPC_PORT = 55553;
public static final String IPV4_FORWARD_FILEPATH = "/proc/sys/net/ipv4/ip_forward";
private static boolean mInitialized = false;
private static String mLastError = "";
private static Context mContext = null;
private static WifiLock mWifiLock = null;
private static WakeLock mWakeLock = null;
private static Network mNetwork = null;
private static final List<Target> mTargets = new ArrayList<>();
private static Target mCurrentTarget = null;
private static Map<String, String> mServices = null;
private static Map<String, String> mPorts = null;
private static Map<Integer, String> mVendors = null;
private static SparseIntArray mOpenPorts = null;
// registered plugins
private static ArrayList<Plugin> mPlugins = null;
private static Plugin mCurrentPlugin = null;
// toolbox singleton
private static ToolBox mTools = null;
private static HTTPSRedirector mRedirector = null;
private static Proxy mProxy = null;
private static Server mServer = null;
private static String mStoragePath = null;
private static String mSessionName = null;
private static String mApkVersion = null;
private static Object mCustomData = null;
private static RPCClient mMsfRpc = null;
private static Exploit mExploit = null;
private static Payload mPayload = null;
private static Session mMsfSession = null;
private static boolean mCoreInitialized = false;
private static KnownIssues mKnownIssues = null;
private static Observer targetListObserver = null;
private final static LinkedList<SettingReceiver> mSettingReceivers = new LinkedList<SettingReceiver>();
public static void init(Context context) throws Exception{
mContext = context;
try{
Logger.debug("initializing System...");
mStoragePath = getSettings().getString("PREF_SAVE_PATH", Environment.getExternalStorageDirectory().toString());
mSessionName = "csploit-session-" + java.lang.System.currentTimeMillis();
mKnownIssues = new KnownIssues();
mPlugins = new ArrayList<>();
mOpenPorts = new SparseIntArray(3);
mServices = new HashMap<>();
mPorts = new HashMap<>();
// if we are here, network initialization didn't throw any error, lock wifi
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
if(mWifiLock == null)
mWifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "wifiLock");
if(!mWifiLock.isHeld())
mWifiLock.acquire();
// wake lock if enabled
if(getSettings().getBoolean("PREF_WAKE_LOCK", true)){
PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
if(mWakeLock == null)
mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "wakeLock");
if(!mWakeLock.isHeld())
mWakeLock.acquire();
}
// set ports
try{
HTTP_PROXY_PORT = Integer.parseInt(getSettings().getString("PREF_HTTP_PROXY_PORT", "8080"));
HTTP_SERVER_PORT = Integer.parseInt(getSettings().getString("PREF_HTTP_SERVER_PORT", "8081"));
HTTPS_REDIR_PORT = Integer.parseInt(getSettings().getString("PREF_HTTPS_REDIRECTOR_PORT", "8082"));
MSF_RPC_PORT = Integer.parseInt(getSettings().getString("MSF_RPC_PORT", "55553"));
} catch(NumberFormatException e){
HTTP_PROXY_PORT = 8080;
HTTP_SERVER_PORT = 8081;
HTTPS_REDIR_PORT = 8082;
MSF_RPC_PORT = 55553;
}
uncaughtReloadNetworkMapping();
ThreadHelper.getSharedExecutor().execute(new Runnable() {
@Override
public void run() {
preloadServices();
preloadVendors();
}
});
}
catch(Exception e){
if(!(e instanceof NoRouteToHostException))
errorLogging(e);
throw e;
}
}
public static void reloadTools() {
getTools().reload();
}
public static class SuException extends Exception {
public SuException() {
super("super user access denied");
}
}
public static class DaemonException extends Exception {
public DaemonException(String message) {
super(message);
}
}
private static void startCoreDaemon() throws SuException, DaemonException {
boolean access_granted = false;
DataOutputStream writer = null;
BufferedReader reader = null;
String line;
int ret = -1;
try {
Process shell = Runtime.getRuntime().exec("su");
writer = new DataOutputStream(shell.getOutputStream());
String cmd;
cmd = String.format("{ echo 'ACCESS GRANTED' >&2; cd '%s' && exec ./start_daemon.sh ;} || exit 1\n",
System.getCorePath());
writer.write(cmd.getBytes());
writer.flush();
ret = shell.waitFor();
if(ret != 0) {
reader = new BufferedReader(new InputStreamReader(shell.getErrorStream()));
while((line = reader.readLine()) != null) {
if(line.equals("ACCESS GRANTED")) {
access_granted = true;
Logger.debug("'ACCESS GRANTED' found");
} else
Logger.warning("STDERR: " + line);
}
} else
access_granted = true;
} catch ( IOException e) {
// command "su" not found or cannot write to it's stdin
Logger.error(e.getMessage());
} catch (InterruptedException e) {
// interrupted while waiting for shell exit value
Logger.error(e.getMessage());
} finally {
if(writer != null)
try { writer.close(); } catch (IOException ignored) {}
if(reader != null)
try { reader.close(); } catch (IOException ignored) {}
}
mKnownIssues.fromFile(String.format("%s/issues", getCorePath()));
if(!access_granted)
throw new SuException();
if(ret != 0) {
File log = new File(System.getCorePath(), "cSploitd.log");
DaemonException daemonException = new DaemonException("core daemon returned " + ret);
if(log.exists() && log.canRead()) {
ACRAConfiguration conf = ACRA.getConfig();
conf.setApplicationLogFile(log.getAbsolutePath());
ACRA.setConfig(conf);
ACRA.getErrorReporter().handleException(daemonException, false);
}
throw daemonException;
}
}
/**
* shutdown the core daemon
*/
public static void shutdownCoreDaemon(){
if(!Client.isConnected() && !Client.Connect(getCorePath() + "/cSploitd.sock")) {
return; // daemon is not running
}
if(!Client.isAuthenticated() && !Client.Login("android", "DEADBEEF")) {
Logger.error("cannot login to daemon");
}
Client.Shutdown();
Client.Disconnect();
mCoreInitialized = false;
Services.getNetworkRadar().onAutoScanChanged();
}
public static void initCore() throws DaemonException, SuException {
if(mCoreInitialized)
return;
String socket_path = getCorePath() + "/cSploitd.sock";
if(!Client.isConnected()) {
if(!Client.Connect(socket_path)) {
startCoreDaemon();
if (!Client.Connect(socket_path))
throw new DaemonException("cannot connect to core daemon");
}
}
if (!Client.isAuthenticated() && !Client.Login("android", "DEADBEEF")) {
throw new DaemonException("cannot login to core daemon");
}
if (!Client.LoadHandlers()) {
throw new DaemonException("cannot load handlers");
}
ChildManager.storeHandlers();
reloadTools();
mCoreInitialized = true;
Services.getNetworkRadar().onAutoScanChanged();
}
public static void reloadNetworkMapping(){
try{
uncaughtReloadNetworkMapping();
}
catch(NoRouteToHostException nrthe){
// swallow bitch
}
catch(Exception e){
errorLogging(e);
}
}
private static void uncaughtReloadNetworkMapping() throws UnknownHostException, SocketException {
mNetwork = new Network(mContext);
reset();
mInitialized = true;
}
public static boolean checkNetworking(final Activity current){
if(!Network.isWifiConnected(mContext)){
Intent intent = new Intent();
intent.putExtra(WifiScannerActivity.CONNECTED, false);
current.setResult(Activity.RESULT_OK, intent);
String title = current.getString(R.string.error);
String message = current.getString(R.string.wifi_went_down);
new FatalDialog(title, message, current).show();
return false;
}
return true;
}
public synchronized static void setTargetListObserver(Observer targetListObserver) {
System.targetListObserver = targetListObserver;
}
/**
* notify that a specific target of the list has been changed
* @param target the changed target
*/
public static void notifyTargetListChanged(Target target) {
Observer o;
synchronized (System.class) {
o = targetListObserver;
}
if(o==null)
return;
o.update(null, target);
}
/**
* notify that the targets list has been changed
*/
public static void notifyTargetListChanged() {
notifyTargetListChanged(null);
}
public static void setLastError(String error){
mLastError = error;
}
public static String getLastError(){
return mLastError;
}
public static synchronized void errorLogging(Throwable e){
String message = "Unknown error.",
trace = "Unknown trace.",
filename = (new File(Environment.getExternalStorageDirectory().toString(), ERROR_LOG_FILENAME)).getAbsolutePath();
if(e != null){
if(e.getMessage() != null && !e.getMessage().isEmpty())
message = e.getMessage();
else if(e.toString() != null)
message = e.toString();
if(message.equals(mLastError))
return;
Writer sWriter = new StringWriter();
PrintWriter pWriter = new PrintWriter(sWriter);
e.printStackTrace(pWriter);
trace = sWriter.toString();
if(mContext != null && getSettings().getBoolean("PREF_DEBUG_ERROR_LOGGING", false)){
try{
FileWriter fWriter = new FileWriter(filename, true);
BufferedWriter bWriter = new BufferedWriter(fWriter);
bWriter.write(trace);
bWriter.close();
}
catch(IOException ioe){
Logger.error(ioe.toString());
}
}
}
setLastError(message);
Logger.error(message);
Logger.error(trace);
}
public static String getPlatform()
{
int api = Build.VERSION.SDK_INT;
String abi = Build.CPU_ABI;
return String.format("android%d.%s", api, abi);
}
public static String getCompatiblePlatform()
{
int api = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ?
Build.VERSION_CODES.JELLY_BEAN : Build.VERSION_CODES.GINGERBREAD);
String abi = Build.CPU_ABI;
return String.format("android%d.%s", api, abi);
}
public static boolean isARM(){
String abi = Build.CPU_ABI;
Logger.debug("Build.CPU_ABI = " + abi);
return Build.CPU_ABI.toLowerCase().startsWith("armeabi");
}
public static synchronized void setCustomData(Object data){
mCustomData = data;
}
public static Object getCustomData(){
return mCustomData;
}
public static InputStream getRawResource(int id){
return mContext.getResources().openRawResource(id);
}
public static String getDefaultRubyPath() {
return mContext.getFilesDir().getAbsolutePath() + "/ruby";
}
public static String getRubyPath() {
return getSettings().getString("RUBY_DIR", getDefaultRubyPath());
}
public static String getDefaultMsfPath() {
return mContext.getFilesDir().getAbsolutePath() + "/msf";
}
public static String getMsfPath() {
return getSettings().getString("MSF_DIR", getDefaultMsfPath());
}
public static String getToolsPath() {
return mContext.getFilesDir().getAbsolutePath() + "/tools/";
}
public static String getCorePath() {
return mContext.getFilesDir().getAbsolutePath();
}
public static void registerSettingListener(SettingReceiver receiver) {
if (mSettingReceivers != null) {
synchronized (mSettingReceivers) {
if (!mSettingReceivers.contains(receiver)) {
mSettingReceivers.add(receiver);
}
}
}
}
public static void onSettingChanged(String key) {
synchronized (mSettingReceivers) {
for(SettingReceiver r : mSettingReceivers) {
if(r.getFilter().contains(key))
r.onSettingChanged(key);
}
}
}
public static void unregisterSettingListener(SettingReceiver receiver) {
synchronized (mSettingReceivers) {
mSettingReceivers.remove(receiver);
}
}
public static void preloadServices(){
if (!mServices.isEmpty())
return;
FileReader fr = null;
BufferedReader reader = null;
try{
// preload network service and ports map
fr = new FileReader(mContext.getFilesDir().getAbsolutePath() + "/tools/nmap/nmap-services");
reader = new BufferedReader(fr);
String line;
Matcher matcher;
String port, proto;
while ((line = reader.readLine()) != null) {
if ((matcher = SERVICE_PARSER.matcher(line)) != null && matcher.find()) {
proto = matcher.group(1);
port = matcher.group(2);
mServices.put(proto, port);
mPorts.put(port, proto);
}
}
} catch (Exception e) {
mServices.clear();
mPorts.clear();
errorLogging(e);
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(fr);
}
}
private static void preloadVendors(){
if(mVendors == null){
try{
mVendors = new HashMap<>();
@SuppressWarnings("ConstantConditions")
FileInputStream fstream = new FileInputStream(mContext.getFilesDir().getAbsolutePath() + "/tools/nmap/nmap-mac-prefixes");
DataInputStream in = new DataInputStream(fstream);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while((line = reader.readLine()) != null){
line = line.trim();
if(!line.startsWith("#") && !line.isEmpty()){
String[] tokens = line.split(" ", 2);
if(tokens.length == 2)
mVendors.put(NetworkHelper.getOUICode(tokens[0]), tokens[1]);
}
}
in.close();
}
catch(Exception e){
errorLogging(e);
}
}
}
public static String getSessionName(){
return mSessionName;
}
public static String getStoragePath(){
return mStoragePath;
}
public static SharedPreferences getSettings(){
return PreferenceManager.getDefaultSharedPreferences(mContext);
}
public static String getAppVersionName(){
if(mApkVersion !=null)
return mApkVersion;
try{
PackageManager manager = mContext.getPackageManager();
PackageInfo info = manager != null ? manager.getPackageInfo(mContext.getPackageName(), 0) : null;
if(info != null)
return (mApkVersion = info.versionName);
}
catch(NameNotFoundException e){
errorLogging(e);
}
return "0.0.1";
}
/**
* reade the first line of a file
* @param filePath path of the file to read from
* @return the first line of the file or {@code null} if an error occurs
*/
private static String readFirstLine(String filePath) {
BufferedReader reader = null;
if(filePath==null)
return null;
try {
reader = new BufferedReader(new FileReader(filePath));
return reader.readLine().trim();
} catch (IOException e) {
Logger.debug(e.getMessage());
} finally {
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
//ignored
}
}
return null;
}
/**
* get currently installed core version
* @return the version of the core, null if not present.
*/
public static String getCoreVersion() {
return readFirstLine(getCorePath() + "/VERSION");
}
/**
* get version of installed ruby
* @return the installed version of ruby
*/
public static String getLocalRubyVersion() {
return readFirstLine(getRubyPath() + "/VERSION");
}
/**
* get version of installed MetaSploit Framework
* @return the version of installed MetaSploit Framework
*/
public static String getLocalMsfVersion() {
return readFirstLine(getMsfPath() + "/VERSION");
}
public static boolean isServiceRunning(String name){
ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
//noinspection ConstantConditions
for(RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)){
if(name.equals(service.service.getClassName()))
return true;
}
return false;
}
public static boolean isPortAvailable(int port){
boolean available = true;
int available_code = mOpenPorts.get(port);
if(available_code != 0)
return available_code != 1;
try{
// attempt 3 times since proxy and server could be still releasing
// their ports
for(int i = 0; i < 3; i++){
Socket channel = new Socket();
InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(mNetwork.getLocalAddressAsString()), port);
channel.connect(address, 200);
available = !channel.isConnected();
channel.close();
if(available)
break;
Thread.sleep(200);
}
}
catch(Exception e){
available = true;
}
mOpenPorts.put(port, available ? 2 : 1);
return available;
}
public static ArrayList<String> getAvailableSessionFiles(){
ArrayList<String> files = new ArrayList<String>();
File storage = new File(mStoragePath);
if(storage.exists()){
String[] children = storage.list();
if(children != null && children.length > 0){
for(String child : children){
if(child.endsWith(".dss"))
files.add(child);
}
}
}
return files;
}
public static String saveSession(String sessionName) throws IOException{
StringBuilder builder = new StringBuilder();
String filename = mStoragePath + '/' + sessionName + ".dss",
session;
builder.append(SESSION_MAGIC + "\n");
// skip the network target
synchronized (mTargets) {
builder.append(mTargets.size() - 1).append("\n");
for (Target target : mTargets) {
if (target.getType() != Target.Type.NETWORK)
target.serialize(builder);
}
}
session = builder.toString();
FileOutputStream ostream = new FileOutputStream(filename);
GZIPOutputStream gzip = new GZIPOutputStream(ostream);
gzip.write(session.getBytes());
gzip.close();
mSessionName = sessionName;
return filename;
}
public static ArrayList<String> getAvailableHijackerSessionFiles(){
ArrayList<String> files = new ArrayList<String>();
File storage = new File(mStoragePath);
if(storage.exists()){
String[] children = storage.list();
if(children != null && children.length > 0){
for(String child : children){
if(child.endsWith(".dhs"))
files.add(child);
}
}
}
return files;
}
public static void loadSession(String filename) throws Exception{
File file = new File(mStoragePath + '/' + filename);
if(file.exists() && file.length() > 0){
BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file))));
String line;
// begin decoding procedure
try{
line = reader.readLine();
if(line == null || !line.equals(SESSION_MAGIC))
throw new Exception("Not a cSploit session file.");
reset();
// read targets
int targets = Integer.parseInt(reader.readLine());
for(int i = 0; i < targets; i++){
Target target = new Target(reader);
if(!hasTarget(target)){
System.addOrderedTarget(target);
} else{
synchronized (mTargets) {
for (int j = 0; j < mTargets.size(); j++) {
if (mTargets.get(j) != null && mTargets.get(j).equals(target)) {
mTargets.set(j, target);
break;
}
}
}
}
}
reader.close();
} catch(Exception e){
reset();
reader.close();
throw e;
}
} else
throw new Exception(filename + " does not exists or is empty.");
}
public synchronized static ToolBox getTools() {
if(mTools == null) {
mTools = new ToolBox();
mTools.reload();
}
return mTools;
}
public static RPCClient getMsfRpc() {
return mMsfRpc;
}
public static void setMsfRpc(RPCClient value){
if(value==mMsfRpc)
return;
mMsfRpc = value;
// refresh all exploits
// NOTE: this method is usually called by the RPCServer Thread, which will not block the UI
for (Target t : getTargets()) { // use a copy of the targets to avoid deadlocks.
for (Exploit e : t.getExploits()) {
if (e instanceof MsfExploit) {
((MsfExploit) e).refresh();
}
}
}
for( Plugin plugin : getPluginsForTarget() ) {
plugin.onRpcChange(value);
}
}
public static Proxy getProxy(){
try{
if(mProxy == null)
mProxy = new Proxy(getNetwork().getLocalAddress(), HTTP_PROXY_PORT);
}
catch(Exception e){
errorLogging(e);
}
return mProxy;
}
public static HTTPSRedirector getHttpsRedirector(){
try{
if(mRedirector == null)
mRedirector = new HTTPSRedirector(mContext, getNetwork().getLocalAddress(), HTTPS_REDIR_PORT);
}
catch(Exception e){
errorLogging(e);
}
return mRedirector;
}
public static Server getServer(){
try{
if(mServer == null)
mServer = new Server(getNetwork().getLocalAddress(), HTTP_SERVER_PORT);
}
catch(Exception e){
errorLogging(e);
}
return mServer;
}
public static String getImageMimeType(String fileName){
String type = "image/jpeg",
name = fileName.toLowerCase();
if(name.endsWith(".jpeg") || name.endsWith(".jpg"))
type = "image/jpeg";
else if(name.endsWith(".png"))
type = "image/png";
else if(name.endsWith(".gif"))
type = "image/gif";
else if(name.endsWith(".tiff"))
type = "image/tiff";
return type;
}
public static void reset() {
mCurrentTarget = null;
synchronized (mTargets) {
mTargets.clear();
Target network = new Target(mNetwork),
gateway = new Target(mNetwork.getGatewayAddress(), mNetwork.getGatewayHardware()),
device = new Target(mNetwork.getLocalAddress(), mNetwork.getLocalHardware());
gateway.setAlias(mNetwork.getSSID());
device.setAlias(android.os.Build.MODEL);
mTargets.add(network);
mTargets.add(gateway);
mTargets.add(device);
scanThemAll();
}
}
public static void scanThemAll() {
if(!Services.getNetworkRadar().isAutoScanEnabled()) {
return;
}
synchronized (mTargets) {
for(Target t : mTargets) {
Services.getNetworkRadar().onNewTargetFound(t);
}
}
}
public static boolean isInitialized(){
return mInitialized;
}
public static boolean isCoreInstalled() {
return new File(getCorePath() + "/VERSION").exists();
}
public static boolean isCoreInitialized() {
return mCoreInitialized;
}
public static KnownIssues getKnownIssues() {
return mKnownIssues;
}
public static String getMacVendor(byte[] mac){
if(mac != null && mVendors != null && mac.length >= 3)
return mVendors.get(NetworkHelper.getOUICode(mac));
else
return null;
}
public static String getProtocolByPort(String port){
return mPorts.containsKey(port) ? mPorts.get(port) : null;
}
public static String getProtocolByPort(int port) {
return getProtocolByPort(Integer.toString(port));
}
public static int getPortByProtocol(String protocol){
return mServices.containsKey(protocol) ? Integer.parseInt(mServices.get(protocol)) : 0;
}