Posted by Security is just an illusion at Friday, January 25, 2013
Read our previous post
Big thanks to Alexis for the source Code.
I have the source code changed a bit
Knockd Android Client Apk
Knockd Android Client Source
DBAdapter.java
package com.alexis.portknocking; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBAdapter { private Context ctx; private DBOpenHelper helper; private SQLiteDatabase db; public static final String KEY_ID = "_id"; public static final String KEY_HOST = "host"; public static final String KEY_PORT = "port"; public static final String KEY_STARTSTOP = "startstop"; public static final int START_MODE = 0; public static final int STOP_MODE = 1; private static class DBOpenHelper extends SQLiteOpenHelper { public DBOpenHelper(Context context) { super(context, "hostlist.db", null, 3); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE hosts (_id INTEGER PRIMARY KEY, " + "host STRING, " + "port INTEGER, " + "startstop INTEGER DEFAULT 0," + "position INTEGER DEFAULT 0);"); /* Triggers for computing position after DELETE/UPDATE-ing an host. */ db.execSQL("CREATE TRIGGER host_delete_recalcpos AFTER DELETE ON " + "hosts FOR EACH ROW BEGIN UPDATE hosts " + "SET position=position-1 WHERE position>old.position; " + "END;"); db.execSQL("CREATE TRIGGER host_update_recalcpos BEFORE UPDATE OF position " + "ON hosts FOR EACH ROW BEGIN UPDATE hosts " + "SET position=old.position WHERE position=new.position; " + "END;"); // I love involutive bijections! } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //db.execSQL("DROP TABLE IF EXISTS hosts"); //onCreate(db); } } public DBAdapter(Context ctx) { this.ctx = ctx; } public void open() { helper = new DBOpenHelper(this.ctx); db = helper.getWritableDatabase(); } public void close() { db.close(); } public int[] getHosts(long startstop) { Cursor c = db.query("hosts", new String[]{"_id"}, "startstop="+startstop, null, null, null, "position"); int[] data = new int[c.getCount()]; c.moveToFirst(); for (int i = 0; i < c.getCount(); i++) { data[i] = c.getInt(0); c.moveToNext(); } c.close(); return data; } public String getHost(int id) { Cursor c = db.query("hosts", new String[]{"host"}, "_id ="+id, null, null, null, null); String host; c.moveToFirst(); host = c.getString(0); c.close(); return host; } public int getPort(int id) { // TODO: WARNING SQL INJECTION! Cursor c = db.query("hosts", new String[]{"port"}, "_id = "+id, null, null, null, null); c.moveToFirst(); int result = c.getInt(0); c.close(); return result; } public Cursor getRawHosts(int startstop) { return db.query("hosts", null, "startstop="+startstop, null, null, null, "position"); } public Cursor getRawHost(long id) { Cursor c = db.query("hosts", null, "_id = "+String.valueOf(id), null, null, null, null); if (c != null) c.moveToFirst(); return c; } public void createHost(String host, Long port, int startstop) { db.execSQL("INSERT INTO hosts (host,port,position,startstop) VALUES (?,?,(SELECT " + "IFNULL(MAX(position)+1,0) " + "FROM hosts WHERE startstop=?),?)", new Object[]{host, port, startstop, startstop}); } public void updateHost(long id, String host, Long port) { ContentValues args = new ContentValues(); args.put(KEY_HOST, host); args.put(KEY_PORT, port); db.update("hosts", args, KEY_ID+"="+id, null); } public void deleteHost(long id) { db.delete("hosts", KEY_ID+"="+id, null); } public void upHost(long id) { db.execSQL("UPDATE hosts SET position = position-1 WHERE _id=?", new Object[]{id}); } public void downHost(long id) { db.execSQL("UPDATE hosts SET position = position+1 WHERE _id=?", new Object[]{id}); } }
HostEdit.java
package com.alexis.portknocking; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; // TODO: Use pause/resume/saved states for automatic DB saving public class HostEdit extends Activity { private Long rowid; private DBAdapter dbadapter; private EditText wHost; private EditText wPort; private Button wCancelButton; private int activity_mode = DBAdapter.START_MODE; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.hostedit); this.setTitle("Add a host"); dbadapter = new DBAdapter(this); dbadapter.open(); wHost = (EditText)findViewById(R.id.EditText01); wPort = (EditText)findViewById(R.id.EditText02); wCancelButton = (Button)findViewById(R.id.Button02); wCancelButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { setResult(RESULT_CANCELED); finish(); } }); Button savebutton = (Button)findViewById(R.id.Button01); savebutton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { commit(); setResult(RESULT_OK); finish(); } }); Bundle extras = getIntent().getExtras(); if (extras != null) { activity_mode = extras.getInt(DBAdapter.KEY_STARTSTOP); if (extras.containsKey(DBAdapter.KEY_ID)) { rowid = extras.getLong(DBAdapter.KEY_ID); populateFields(); } } } private void populateFields() { if (rowid != null) { Cursor host = dbadapter.getRawHost(rowid); startManagingCursor(host); wPort.setText(String.valueOf(host.getInt(host.getColumnIndexOrThrow(DBAdapter.KEY_PORT)))); wHost.setText(host.getString(host.getColumnIndexOrThrow(DBAdapter.KEY_HOST))); } } private void commit() { if (rowid == null) { dbadapter.createHost(wHost.getText().toString(), Long.valueOf(wPort.getText().toString()), activity_mode); } else { dbadapter.updateHost(rowid, wHost.getText().toString(), Long.valueOf(wPort.getText().toString())); } } }
HostList.java
package com.alexis.portknocking; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.AdapterView.AdapterContextMenuInfo; public class HostList extends ListActivity { private static final int ACTIVITY_ADD = 0; private static final int ACTIVITY_EDIT = 0; private int activity_mode = DBAdapter.START_MODE; private DBAdapter dbadapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setTitle("Port knocking settings"); Bundle extras = getIntent().getExtras(); if (extras != null) { activity_mode = extras.getInt(DBAdapter.KEY_STARTSTOP); } registerForContextMenu(this.getListView()); dbadapter = new DBAdapter(this.getApplicationContext()); dbadapter.open(); updateList(); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuItem menuit = menu.add(0, Menu.FIRST, 0, "Add"); menuit.setIcon(android.R.drawable.ic_menu_add); return true; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, Menu.FIRST+1, 0, "Edit"); menu.add(1, Menu.FIRST+2, 0, "Up"); menu.add(2, Menu.FIRST+3, 0, "Down"); menu.add(3, Menu.FIRST+4, 0, "Delete"); } @Override public boolean onContextItemSelected(MenuItem item) { super.onContextItemSelected(item); AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); switch(item.getItemId()) { case Menu.FIRST+1: onListItemClick(this.getListView(), null, info.position, info.id); return true; case Menu.FIRST+2: dbadapter.upHost(info.id); updateList(); return true; case Menu.FIRST+3: dbadapter.downHost(info.id); updateList(); return true; case Menu.FIRST+4: dbadapter.deleteHost(info.id); updateList(); return true; } return false; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { switch(item.getItemId()) { case Menu.FIRST: Intent i = new Intent(this, HostEdit.class); i.putExtra(DBAdapter.KEY_STARTSTOP, activity_mode); startActivityForResult(i, ACTIVITY_ADD); return true; } return super.onMenuItemSelected(featureId, item); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Intent i = new Intent(this, HostEdit.class); i.putExtra(DBAdapter.KEY_ID, id); startActivityForResult(i, ACTIVITY_EDIT); } public void updateList() { Cursor hostsCursor = dbadapter.getRawHosts(activity_mode); startManagingCursor(hostsCursor); String[] from = new String[]{"host", "port"}; int[] to = new int[]{R.id.TextView01, R.id.TextView02}; SimpleCursorAdapter hosts = new SimpleCursorAdapter(this, R.layout.hostitem, hostsCursor, from, to); setListAdapter(hosts); } }
Knock.java
package com.alexis.portknocking; import java.io.IOException; import java.net.ConnectException; import java.net.Socket; import java.net.UnknownHostException; public class Knock { public static boolean doKnock(String host, int port) { Socket s; try { s = new Socket(host, port); if (s.isConnected()) s.close(); } catch (UnknownHostException e) { e.printStackTrace(); return false; } catch (ConnectException e) { // We dismiss "connection refused" as knockd operates at link-layer } catch (IOException e) { e.printStackTrace(); return false; } return true; } }
PortKnocking.java
package com.alexis.portknocking; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class PortKnocking extends Activity { private static final int ACTIVITY_SETTINGS=0; private DBAdapter dbadapter; private Handler mHandler = new Handler(); private ProgressDialog d; private boolean success = true; private void launchKnocking(View view, int startstop) { d = ProgressDialog.show(view.getContext(), "Knock Knock Knock ...", "Please wait ...", true); final int startstop_final = startstop; // TODO: Learn how to use threads. new Thread(new Runnable() { public void run() { int[] hosts = dbadapter.getHosts(startstop_final); for (int i = 0; i < hosts.length; i++) { int id = hosts[i]; final String host = dbadapter.getHost(id); final int port = dbadapter.getPort(id); mHandler.post(new Runnable() { public void run() { d.setMessage(String.format("Pinging %s:%d ...", host, port)); Log.i("PortKnocker", String.format("Pinging %s:%d ...", host, port)); }; }); success = true; if (!Knock.doKnock(host, port)) success = false; } mHandler.post(new Runnable() { public void run() { d.dismiss(); endKnocking(); } }); } }).start(); final TextView text = (TextView) findViewById(R.id.texttimer); text.setText(""); new CountDownTimer(30000, 1000) { @Override public void onTick(long millisUntilFinished) { text.setText("Your Door is Now Open for : " + millisUntilFinished / 1000 + "Seconds"); ImageView img = (ImageView) findViewById(R.id.imageView1); img.setImageResource(R.drawable.opendoor); TextView text = (TextView) findViewById(R.id.texttimer); text.setTextColor(Color.parseColor("#7CFC00")); } @Override public void onFinish() { text.setText("Your Door are Closed Now !!!"); ImageView img = (ImageView) findViewById(R.id.imageView1); img.setImageResource(R.drawable.armitagelogo); TextView text = (TextView) findViewById(R.id.texttimer); text.setTextColor(Color.parseColor("#FF0000")); } }.start(); } public void endKnocking() { //AlertDialog.Builder alert = new AlertDialog.Builder(this); //alert.setTitle("Port knocking"); if (success) { //alert.setMessage("Knocking complete !"); } else { //alert.setMessage("An error occured during knocking !"); } //alert.show(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dbadapter = new DBAdapter(this.getApplicationContext()); dbadapter.open(); Button startButton; Button stopButton; startButton = (Button)findViewById(R.id.Button01); startButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { launchKnocking(view, DBAdapter.START_MODE); } }); stopButton = (Button)findViewById(R.id.Button02); stopButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { launchKnocking(view, DBAdapter.STOP_MODE); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuItem menuit = menu.add(0, Menu.FIRST, 0, "Settings"); menuit.setIcon(android.R.drawable.ic_menu_preferences); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { switch(item.getItemId()) { case Menu.FIRST: Intent i = new Intent(this, Settings.class); startActivityForResult(i, ACTIVITY_SETTINGS); return true; } return super.onMenuItemSelected(featureId, item); } }
Settings.java
package com.alexis.portknocking; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TabHost; public class Settings extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost thost = getTabHost(); thost.setup(); TabHost.TabSpec startspec = thost.newTabSpec("start"); Intent startintent = new Intent(this, HostList.class); startintent.putExtra(DBAdapter.KEY_STARTSTOP, DBAdapter.START_MODE); startspec.setContent(startintent); startspec.setIndicator("HOST", getResources().getDrawable(R.drawable.start)); thost.addTab(startspec); //TabHost.TabSpec stopspec = thost.newTabSpec("stop"); //Intent stopintent = new Intent(this, HostList.class); //stopintent.putExtra(DBAdapter.KEY_STARTSTOP, DBAdapter.STOP_MODE); //stopspec.setContent(stopintent); //stopspec.setIndicator("Stop", getResources().getDrawable(R.drawable.stop)); //thost.addTab(stopspec); } }
Quantum Binary Signals
ReplyDeleteProfessional trading signals delivered to your cell phone daily.
Start following our trades NOW and gain up to 270% a day.