5454import org .csploit .android .tools .NMap ;
5555
5656import java .util .ArrayList ;
57+ import java .util .HashMap ;
58+ import java .util .Map ;
5759
5860public class PortScanner extends Plugin {
5961 private TextView mTextDoc = null ;
6062 private EditText mTextParameters = null ;
6163 private ToggleButton mScanToggleButton = null ;
6264 private ProgressBar mScanProgress = null ;
6365 private boolean mRunning = false ;
64- private ArrayList <String > mPortList = null ;
66+ private ArrayList <String > mPortList = new ArrayList <>() ;
6567 private ArrayAdapter <String > mListAdapter = null ;
6668 private Receiver mScanReceiver = null ;
6769 private String mCustomPorts = null ;
6870 private Menu mMenu = null ;
6971 private static final String CUSTOM_PARAMETERS = "PortScanner.Prefs.CustomParameters" ;
7072 private static final String CUSTOM_PARAMETERS_TEXT = "PortScanner.Prefs.CustomParameters.Text" ;
7173 private SharedPreferences mPreferences = null ;
74+ private Map <Integer , String > urlFormats = new HashMap <>();
7275
7376 public PortScanner () {
7477 super (R .string .port_scanner , R .string .port_scanner_desc ,
7578
7679 new Target .Type []{Target .Type .ENDPOINT , Target .Type .REMOTE },
7780 R .layout .plugin_portscanner , R .drawable .action_scanner );
78-
79- mPortList = new ArrayList <String >();
80- mScanReceiver = new Receiver ();
81+ urlFormats .put (80 , "http://%s/" );
82+ urlFormats .put (443 , "https://%s/" );
83+ urlFormats .put (21 , "ftp://%s" );
84+ urlFormats .put (22 , "ssh://%s" );
85+ urlFormats .put (23 , "telnet://%s/" );
86+ urlFormats .put (0 , "telnet://%s:%d/" ); ///< default
8187 }
8288
8389 /**
@@ -185,13 +191,6 @@ public void onCreate(Bundle savedInstanceState) {
185191 mScanToggleButton = (ToggleButton ) findViewById (R .id .scanToggleButton );
186192 mScanProgress = (ProgressBar ) findViewById (R .id .scanActivity );
187193
188- new Thread (new Runnable () {
189- @ Override
190- public void run () {
191- System .preloadServices ();
192- }
193- }).start ();
194-
195194 if (mPreferences .getBoolean (CUSTOM_PARAMETERS , false ))
196195 displayParametersField ();
197196
@@ -210,52 +209,32 @@ public void onClick(View v) {
210209
211210 createPortList ();
212211
213- mListAdapter = new ArrayAdapter <String >(this ,
212+ final Target target = System .getCurrentTarget ();
213+ final String cmdlineRep = target .getCommandLineRepresentation ();
214+
215+ mListAdapter = new ArrayAdapter <>(this ,
214216 android .R .layout .simple_list_item_1 , mPortList );
215217 mScanList .setAdapter (mListAdapter );
216218 mScanList .setOnItemLongClickListener (new OnItemLongClickListener () {
217219 @ Override
218220 public boolean onItemLongClick (AdapterView <?> parent , View view ,
219221 int position , long id ) {
220- int portNumber = System .getCurrentTarget ().getOpenPorts ()
221- .get (position ).getNumber ();
222- String url ;
223-
224- if (portNumber == 80 )
225- url = "http://"
226- + System .getCurrentTarget ()
227- .getCommandLineRepresentation () + "/" ;
228-
229- else if (portNumber == 443 )
230- url = "https://"
231- + System .getCurrentTarget ()
232- .getCommandLineRepresentation () + "/" ;
233-
234- else if (portNumber == 21 )
235- url = "ftp://"
236- + System .getCurrentTarget ()
237- .getCommandLineRepresentation ();
238-
239- else if (portNumber == 22 )
240- url = "ssh://"
241- + System .getCurrentTarget ()
242- .getCommandLineRepresentation ();
222+ int portNumber = target .getOpenPorts ().get (position ).getNumber ();
243223
244- else
245- url = "telnet://"
246- + System .getCurrentTarget ()
247- .getCommandLineRepresentation () + ":"
248- + portNumber ;
224+ if (!urlFormats .containsKey (portNumber )) {
225+ portNumber = 0 ;
226+ }
249227
250- final String furl = url ;
228+ final String url = String .format (urlFormats .get (portNumber ),
229+ cmdlineRep , portNumber );
251230
252231 new ConfirmDialog ("Open" , "Open " + url + " ?" ,
253232 PortScanner .this , new ConfirmDialogListener () {
254233 @ Override
255234 public void onConfirm () {
256235 try {
257236 Intent browser = new Intent (
258- Intent .ACTION_VIEW , Uri .parse (furl ));
237+ Intent .ACTION_VIEW , Uri .parse (url ));
259238
260239 PortScanner .this .startActivity (browser );
261240 } catch (ActivityNotFoundException e ) {
@@ -372,6 +351,12 @@ public void onBackPressed() {
372351
373352 private class Receiver extends NMap .SynScanReceiver {
374353
354+ private final Target target ;
355+
356+ public Receiver (Target target ) {
357+ this .target = target ;
358+ }
359+
375360 @ Override
376361 public void onStart (String commandLine ) {
377362 super .onStart (commandLine );
@@ -399,30 +384,24 @@ public void run() {
399384
400385 @ Override
401386 public void onPortFound (final int port , String protocol ) {
402- final String portProtocol = protocol ;
403- final String resolvedProtocol = System .getProtocolByPort (port );
387+ String resolvedProtocol = System .getProtocolByPort (port );
404388
405- System .addOpenPort (port , Network .Protocol .fromString (protocol ));
389+ target .addOpenPort (port , Network .Protocol .fromString (protocol ));
406390
407- PortScanner .this .runOnUiThread (new Runnable () {
408- @ Override
409- public void run () {
410- String entry ;
411-
412- if (resolvedProtocol != null ) {
413- entry = port + " ( " + resolvedProtocol + " )" ;
414- } else {
415- entry = portProtocol + " : " + port ;
416- }
391+ final String entry = (resolvedProtocol !=null ?
392+ port + " ( " + resolvedProtocol + " )" :
393+ protocol + " : " + port
394+ );
417395
418- // add open port to the listview and notify the environment
419- // about the event
420- if (!mPortList .contains (entry )) {
396+ if (!mPortList .contains (entry )) {
397+ PortScanner .this .runOnUiThread (new Runnable () {
398+ @ Override
399+ public void run () {
421400 mPortList .add (entry );
422401 mListAdapter .notifyDataSetChanged ();
423402 }
424- }
425- });
403+ });
404+ }
426405 }
427406 }
428407}
0 commit comments