Skip to content

Commit

Permalink
hidfuzzer: modernization 3/N
Browse files Browse the repository at this point in the history
- improved error handling
- move all UI code into Activity
- favor synchronous SU with no synchronization when possible
- remove back references in favor of callbacks
- simplify lua interface
- refactor code style
  • Loading branch information
Netdex committed Nov 13, 2020
1 parent 516b37e commit 73dd138
Show file tree
Hide file tree
Showing 23 changed files with 634 additions and 789 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ android {
compileSdkVersion 30
defaultConfig {
applicationId "org.netdex.hidfuzzer"
minSdkVersion 25
minSdkVersion 26
targetSdkVersion 30
versionCode 100
versionName '1.0.0'
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/assets/scripts/a.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
---

while not cancelled() do
progress("idle")
log("idle")

-- poll until /dev/hidg0 is writable
while not cancelled() and not test() do delay(1000) end
if cancelled() then break end

progress("running")
log("running")
delay(1000)

send_string("test")

progress("done")
log("done")
while not cancelled() and test() do
delay(1000)
end
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/assets/scripts/downloadrun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ local file = ask("File to download?", "https://github.com/Netdex/FlyingCursors/r
local runAs = should("Task UAC", "Launch exe as admin?");

while not cancelled() do
progress("idle")
log("idle")

-- poll until /dev/hidg0 is writable
while not cancelled() and not test() do delay(1000) end
if cancelled() then break end

progress("running")
log("running")
delay(1000)

log("opening powershell, runAs=" .. tostring(runAs))
Expand Down Expand Up @@ -49,7 +49,7 @@ while not cancelled() do
"exit;\n"
)

progress("done")
log("done")
while not cancelled() and test() do
delay(1000)
end
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/assets/scripts/fuzzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
math.randomseed(os.time())

while not cancelled() do
progress("idle")
log("idle")

-- poll until /dev/hidg0 is writable
while not cancelled() and not test() do delay(1000) end
if cancelled() then break end

progress("running")
log("running")
delay(1000)

while test() and not isCancelled() do
kbuf = {}
mbuf = {}
for i=1,7 do kbuf[i] = math.random(0, )end
for i=1,4 do end
end
--for i=1,7 do kbuf[i] = math.random(0, )end
--for i=1,4 do end
end
kbuf = {}
mbuf = {}
progress("done")
log("done")
while not cancelled() and test() do
delay(1000)
end
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/assets/scripts/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
---

while not cancelled() do
progress("idle")
log("idle")

-- poll until /dev/hidg0 is writable
while not cancelled() and not test() do delay(1000) end
if cancelled() then break end

progress("running")
log("running")
delay(1000)

progress("done")
log("done")
while not cancelled() and test() do
delay(1000)
end
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/assets/scripts/wallpaper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
--- wallpaper.lua: Changes a Windows 10 desktop wallpaper
---
---
local file = ask("Wallpaper to download?", "https://i.imgur.com/UhjsQ3x.jpg")
local file = ask("Wallpaper to download?", "https://i.redd.it/ur1mqcbpxou51.png")

while not cancelled() do
progress("idle")
log("idle")

-- poll until /dev/hidg0 is writable
while not cancelled() and not test() do delay(1000) end
if cancelled() then break end

progress("running")
log("running")
delay(1000)

press_keys(kb.LSUPER, kb.R)
Expand All @@ -34,7 +34,7 @@ while not cancelled() do
"[W.S]::SW(\"$Env:Temp\\b.jpg\")\n" ..
"exit\n")

progress("done")
log("done")
while not cancelled() and test() do
delay(1000)
end
Expand Down
80 changes: 72 additions & 8 deletions app/src/main/java/org/netdex/hidfuzzer/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package org.netdex.hidfuzzer;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Html;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.os.HandlerCompat;

import org.netdex.hidfuzzer.ltask.HIDTask;
import org.netdex.hidfuzzer.ltask.LuaTaskLoader;
import org.netdex.hidfuzzer.gui.ConfirmDialog;
import org.netdex.hidfuzzer.gui.PromptDialog;
import org.netdex.hidfuzzer.ltask.AsyncIOBridge;
import org.netdex.hidfuzzer.ltask.LuaHIDTask;
import org.netdex.hidfuzzer.ltask.LuaHIDTaskFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class MainActivity extends AppCompatActivity {
Expand All @@ -25,7 +37,7 @@ public class MainActivity extends AppCompatActivity {
static final HashMap<String, String> fTaskMap = new HashMap<>();
static final ArrayList<String> fTaskSpinnerItems = new ArrayList<>();

private HIDTask mRunningTask;
private LuaHIDTask mRunningTask;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -46,25 +58,77 @@ protected void onCreate(Bundle savedInstanceState) {
e.printStackTrace();
}

Handler handler = HandlerCompat.createAsync(Looper.getMainLooper());

// sort out the UI
final ToggleButton btnPoll = findViewById(R.id.btnPoll);
final Spinner spnTask = findViewById(R.id.spnTask);
ToggleButton btnPoll = findViewById(R.id.btnPoll);
Spinner spnTask = findViewById(R.id.spnTask);
TextView logView = findViewById(R.id.txtLog);
ScrollView scrollView = findViewById(R.id.scrollview);

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

AsyncIOBridge dialogIO = new AsyncIOBridge() {
@Override
public void onLogMessage(String s) {
handler.post(() -> {
logView.append(Html.fromHtml(s));
logView.append("\n");
scrollView.fullScroll(View.FOCUS_DOWN);
});
}

@Override
public void onLogClear() {
handler.post(() -> {
logView.setText("");
});
}

@Override
public void onSignal(Signal signal) {
handler.post(()->{
switch(signal){
case DONE:
btnPoll.setChecked(false);
btnPoll.setEnabled(true);
break;
default:
throw new IllegalStateException("Unexpected value: " + signal);
}
});
}

@Override
public boolean onConfirm(String title, String prompt) {
ConfirmDialog dialog = new ConfirmDialog(MainActivity.this, title, prompt);
return dialog.show();
}

@Override
public String onPrompt(String title, String def) {
PromptDialog dialog = new PromptDialog(MainActivity.this, title, def);
return dialog.show();
}
};

LuaHIDTaskFactory luaHIDTaskFactory = new LuaHIDTaskFactory(dialogIO);

ExecutorService executorService = Executors.newCachedThreadPool();

btnPoll.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
String selectedTask = (String) spnTask.getSelectedItem();
String taskSrcFilePath = fTaskMap.get(selectedTask);
mRunningTask = LuaTaskLoader.createTaskFromLuaFile(this, selectedTask, taskSrcFilePath);
mRunningTask.execute();
mRunningTask = luaHIDTaskFactory.createTaskFromLuaFile(this, selectedTask, taskSrcFilePath);
executorService.execute(mRunningTask);
} else {
if (mRunningTask != null) {
btnPoll.setEnabled(false);
mRunningTask.cancel(false);
mRunningTask.setCancelled(true);
}
}
});
Expand Down
Loading

0 comments on commit 73dd138

Please sign in to comment.