Skip to content

Commit 4bdf79d

Browse files
committed
Speed up port protocol resolution.
Workaround for #332. Instead of loading services when we find a new open port, do it when we enter in the prrt scanner, and in a thread. So when a new open port is discovered, it has already loaded ~2000 ports. Also avoid to create new objects on every iteration of the loops.
1 parent c93c08f commit 4bdf79d

2 files changed

Lines changed: 69 additions & 32 deletions

File tree

cSploit/src/org/csploit/android/core/System.java

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import android.app.Activity;
2222
import android.app.ActivityManager;
2323
import android.app.ActivityManager.RunningServiceInfo;
24-
import android.app.AlertDialog;
2524
import android.content.Context;
26-
import android.content.DialogInterface;
2725
import android.content.Intent;
2826
import android.content.SharedPreferences;
2927
import android.content.pm.PackageInfo;
@@ -32,7 +30,6 @@
3230
import android.net.wifi.WifiManager;
3331
import android.net.wifi.WifiManager.WifiLock;
3432
import android.os.Build;
35-
import android.os.Bundle;
3633
import android.os.Environment;
3734
import android.os.PowerManager;
3835
import android.os.PowerManager.WakeLock;
@@ -156,6 +153,8 @@ public static void init(Context context) throws Exception{
156153
mKnownIssues = new KnownIssues();
157154
mPlugins = new ArrayList<Plugin>();
158155
mOpenPorts = new SparseIntArray(3);
156+
mServices = new HashMap<String, String>();
157+
mPorts = new HashMap<String, String>();
159158

160159
// if we are here, network initialization didn't throw any error, lock wifi
161160
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
@@ -520,38 +519,46 @@ public static void unregisterSettingListener(SettingReceiver receiver) {
520519
}
521520
}
522521

523-
private static void preloadServices(){
524-
if(mServices == null || mPorts == null){
525-
try{
526-
// preload network service map and mac vendors
527-
mServices = new HashMap<String, String>();
528-
mPorts = new HashMap<String, String>();
529-
530-
@SuppressWarnings("ConstantConditions")
531-
FileInputStream fstream = new FileInputStream(mContext.getFilesDir().getAbsolutePath() + "/tools/nmap/nmap-services");
522+
public static void preloadServices(){
523+
if (mServices.size() > 0 && mPorts.size() > 0)
524+
return;
532525

533-
DataInputStream in = new DataInputStream(fstream);
534-
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
535-
String line;
536-
Matcher matcher;
526+
FileReader fr = null;
527+
BufferedReader reader = null;
528+
try{
529+
// preload network service and ports map
537530

538-
while((line = reader.readLine()) != null){
539-
line = line.trim();
531+
//@SuppressWarnings("ConstantConditions")
532+
fr = new FileReader(mContext.getFilesDir().getAbsolutePath() + "/tools/nmap/nmap-services");
533+
reader = new BufferedReader(fr);
534+
String line;
535+
Matcher matcher;
536+
String port, proto;
540537

541-
if((matcher = SERVICE_PARSER.matcher(line)) != null && matcher.find()){
542-
String proto = matcher.group(1),
543-
port = matcher.group(2);
538+
while ((line = reader.readLine()) != null) {
539+
if ((matcher = SERVICE_PARSER.matcher(line)) != null && matcher.find()) {
540+
proto = matcher.group(1);
541+
port = matcher.group(2);
544542

545-
mServices.put(proto, port);
546-
mPorts.put(port, proto);
547-
}
543+
mServices.put(proto, port);
544+
mPorts.put(port, proto);
548545
}
549-
550-
in.close();
551546
}
552-
catch(Exception e){
553-
errorLogging(e);
547+
548+
} catch (Exception e) {
549+
mServices.clear();
550+
mPorts.clear();
551+
552+
errorLogging(e);
553+
}
554+
finally {
555+
try {
556+
if (fr != null)
557+
fr.close();
558+
if (reader != null)
559+
reader.close();
554560
}
561+
catch (Exception e){}
555562
}
556563
}
557564

@@ -946,7 +953,6 @@ public static String getMacVendor(byte[] mac){
946953
}
947954

948955
public static String getProtocolByPort(String port){
949-
preloadServices();
950956

951957
return mPorts.containsKey(port) ? mPorts.get(port) : null;
952958
}
@@ -956,7 +962,6 @@ public static String getProtocolByPort(int port) {
956962
}
957963

958964
public static int getPortByProtocol(String protocol){
959-
preloadServices();
960965

961966
return mServices.containsKey(protocol) ? Integer.parseInt(mServices.get(protocol)) : 0;
962967
}

cSploit/src/org/csploit/android/plugins/PortScanner.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import android.content.SharedPreferences;
2424
import android.net.Uri;
2525
import android.os.Bundle;
26-
import android.preference.PreferenceManager;
2726
import android.view.Menu;
2827
import android.view.MenuInflater;
2928
import android.view.MenuItem;
@@ -70,6 +69,7 @@ public class PortScanner extends Plugin {
7069
private static final String CUSTOM_PARAMETERS = "PortScanner.Prefs.CustomParameters";
7170
private static final String CUSTOM_PARAMETERS_TEXT = "PortScanner.Prefs.CustomParameters.Text";
7271
private SharedPreferences mPreferences = null;
72+
private boolean unresolvedPorts = false;
7373

7474
public PortScanner() {
7575
super(R.string.port_scanner, R.string.port_scanner_desc,
@@ -187,6 +187,13 @@ public void onCreate(Bundle savedInstanceState) {
187187
mScanToggleButton = (ToggleButton) findViewById(R.id.scanToggleButton);
188188
mScanProgress = (ProgressBar) findViewById(R.id.scanActivity);
189189

190+
new Thread(new Runnable() {
191+
@Override
192+
public void run() {
193+
System.preloadServices();
194+
}
195+
}).start();
196+
190197
if (mPreferences.getBoolean(CUSTOM_PARAMETERS, false))
191198
displayParametersField();
192199

@@ -383,6 +390,29 @@ public void run() {
383390
public void onEnd(int exitCode) {
384391
super.onEnd(exitCode);
385392

393+
// last chance to resolve ports protocols
394+
if (unresolvedPorts) {
395+
String temp_port, port, protocol;
396+
for (int i = 0; i < mPortList.size(); i++) {
397+
if (mPortList.get(i).startsWith("tcp : ") || mPortList.get(i).startsWith("udp : ")) {
398+
temp_port = mPortList.get(i);
399+
port = temp_port.split("\\s")[2];
400+
protocol = System.getProtocolByPort(port);
401+
402+
if (protocol != null)
403+
mPortList.set(i, port + " ( " + protocol + " )");
404+
405+
Logger.debug("unresolved port: " + port + ":" + protocol);
406+
}
407+
}
408+
PortScanner.this.runOnUiThread(new Runnable() {
409+
@Override
410+
public void run() {
411+
mListAdapter.notifyDataSetChanged();
412+
}
413+
});
414+
}
415+
386416
PortScanner.this.runOnUiThread(new Runnable() {
387417
@Override
388418
public void run() {
@@ -406,8 +436,10 @@ public void run() {
406436
if (resolvedProtocol != null)
407437
entry = port + " ( " + resolvedProtocol + " )";
408438

409-
else
439+
else {
440+
unresolvedPorts = true;
410441
entry = portProtocol + " : " + port;
442+
}
411443

412444
// add open port to the listview and notify the environment
413445
// about the event

0 commit comments

Comments
 (0)