Quick start with SCAPY and WIRESHARK (Including drivers) (Custom WIFI Packets) [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Scapy with WIFI - From setup to use
This tutorial is supposed to help you through the setup and installation of scapy and the wifi dongle used in this tutorial.

WIRESHARK, PYTHON AND SCAPY
I spent some time with scapy and want to share my knowledge since there are lots of spots where things can go wrong. I am using the TP-Link wifi dongle TL-WN722N V2.
Operating System:
I found that first of all you need to install your own driver to be able to use frame injection and monitor mode. Windows is not an option here because monitor mode was depreciated in earlier versions. Next I tried linux.
As I am quiet new to linux myself I played around a little and found out that most tutorials on WIFI monitor mode are only working for kali linux, which is fine if you want to use it for hacking. Another operating system for which I can confirm the drivers working is Ubuntu (version 20.04.2 currently). I also tried installing the drivers on raspbian. It does not work, ubuntu server version can be installed on raspberry pi, though. I hope this saves you some trouble.
=> Use Kali or Ubuntu (desktop and server version both work for ubuntu)
Installing The Driver
After reading the above paragraph this should be quiet easy as all the tutorials made for kali linux also work for ubuntu. Below are the steps that I took to install everything:
sudo apt update
sudo apt install bc make gcc
sudo rmmod r8188eu.ko
git clone -b v5.2.20 https://github.com/aircrack-ng/rtl8812au.git
cd rtl8812au
sudo -i
echo "blacklist r8188eu" > "/etc/modprobe.d/realtek.conf"
exit
make
sudo make install
sudo modprobe 8188eu
The most common error for me appeared after calling make. This is often due to wrong kernels and can be fixed by switching to ubuntu or kali as this has to do with the operating system.
Do not forget to reboot before the next steps.
Turn On Monitor Mode
This is also somewhat difficult because, as for me even though using the same operating system on rpi and my desktop computer, errors appeared at different spots. My solution was to simply fiddle around with the code and just trying random combination, leading me to success.
It is important that you use iwconfig to determine the name of your wifi dongle
Use these commands
ifconfig wlan0 down
airmon-ng check kill //Only useful in some situations
usermod -a -G netdev USERNAME //In case the operation is not permitted even though you are root
iwconfig wlan0 mode monitor //On RPi simply use this command without turning wlan0 down
ifconfig wlan0 up
iwconfig //Check out whether you have been successful and the mode says 'monitor' now
No idea why it does not always work the same way but your are very likely to succeed with the above commands.
Using Scapy
Finally, the fun stuff. Get ready to use pip to install scapy (python3 -m pip install scapy). Sadly, scapy only supports Python up to version 3.8, so make sure to have the correct version installed and activated as your default python. You also might need to run the script as root.
The code for sending packets is very straight forward:
from scapy.all import *
conf.use_pcap = True //Not quiet sure if this is optional
send(IP(dst="0.0.0.0")/UDP(dport=123, sport=200)/Raw(load="I am WIFI"), iface="wlan0", loop=1, inter=0.2)
I am not really trying to make a tutorial on how scapy itself works, only the big picture and how to set it up. It is a very interesting library and you should definitely check it out.
Most errors will arise from the import as it is crucial to use the proper python version! Also, the similar methods send() and sendp() troubled me a lot. I was unable to pick up anything with wireshark useing the sendp() method using the same parameters as above.
The iface="wlan0" is responsible for selecting the interface via which the packets are send. It should be matching with the interface you found earlier with the iwconfig command and set to monitor mode.
Using Wireshark
Wireshark is an awesome tool for prototyping whatever you want to do. If something is not working you should start looking there first. You can use it to identify what you are sending by running Wireshark on the transmitting wifi dongle or use another wifi dongle to pick up your packets. If you have done everything correctly and run the python script, while recording with Wireshark, you should be picking up many of the "I am WIFI" messages.
Thank you for going thorugh all of this. I hope I saved some people from all-nighters trying to figure out kernels, drivers or version mismatching.
Do ask me questions.

Related

Running Ruby, Node, Python and Docker on the new Apple Silicon architecture? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How do you get a Ruby, Python and Node.js development environments running on Apple Silicon architecture. What about virtualization software e.g. Docker?
Programming languages such as Ruby, Node and Python should run on Apple M1 Chip but Docker is not supported as of now (They are working on it)
Docker for Mac Issue
https://github.com/docker/for-mac/issues/4733
Docker team might be working on fixing the issue as per this:
https://github.com/docker/roadmap/issues/142
My Recommendation:
Get it now only if you want to build iOS application. Since most of the people don't have the laptop right now, we might end up with a lot of other issues.
UPDATE:
This one is more appropriate answer now:
https://stackoverflow.com/a/65253659/8216911
I've tried many things and had some real trouble getting things working, but in the end, here is the simplest way I've found to get Docker running on a new Mac Silicon M1 chip.
Docker does not natively work, VirtualBox doesn't work, Parallels doesn't work, ... in the end, it goes down to using UTM to create a Virtual Machine, install Ubuntu server on it. Once you have that, you can install whatever you want on it (Docker, Node JS, Apache, PHP, MariaDB, ...).
Then you set eveything up so you can use all your favorite Mac OS tools ( Terminal, Transmit, VS Code, Safari ... ) to work, just as if you had all that on your local file system.
1 - Download Linux installation disk
Grab an ARM linux distribution. I took Ubuntu server 20.04 LTS:
https://cdimage.ubuntu.com/releases/20.04/release/ubuntu-20.04.1-live-server-arm64.iso
It's 922 MB.
I got it from here: https://ubuntu.com/download/server/arm
You can chose any linux distribution, but just make sure you get the ARM version (some distributions don't have one).
2 - Download UTM
UTM is a virtualisation software that is mainly aimed at IOs devices, but it works on Mac OS too.
https://github.com/utmapp/UTM/releases/download/v2.0.14/UTM.dmg
That one is 255 MB.
Future versions will be available from here: https://github.com/utmapp/UTM/releases/
Simply download the package, open it, and launch the application that is inside.
3 - Create your VM
Create your new VM, attach the linux installation disk to it and launch the VM following these steps here:
https://github.com/utmapp/UTM/wiki/Install-Ubuntu-ARM64-on-Apple-M1
Basically:
click Create a New VM
in the Information tab: choose a name and an icon for your VM
in the System tab:
in Hardware choose ARM64 (aarch64) architecture
give it some memory (how about 4 GB ...)
in the Drives tab:
create your main drive with New Drive, interface VirtIO and choose the size you want (I chose 20 480 MB), then click Create.
create the CD drive with New Drive, check Removable, interface USB, click Create
Save the VM
Select your shiny new VM in the sidebar and in the bottom right corner, click Browse and select your Linux installation ISO virtual disk.
You can now launch the VM, it will boot on the Linux installation CD: install Linux.
During this classic installation process, you will be asked to create a user account on the linux system (let's call it bob). When the installation is finished, shut down the VM and extract the installation disk before rebooting.
4 - Working inside your VM
When you restart the VM, you get a terminal asking you to log into Linux, using the username and password you created during installation.
You can now install Docker, openSSH-server, nodeJS, using classic apt-get commands.
5 - Working in your VM from MacOS
If you failed like to me chose the right keyboard, you might have trouble typing some special characters. The best way to work with your VM is now to work from outside of it.
Stop the VM (sudo shutdown -h now if you are already inside the shell) and go back to UTM:
Select your VM in the left side panel and click the top right button to edit the VM again :
Go to the Network tab and in front of Port Forward, click New.
You need to manually add a new port forwarding directive for each port in your VM you want to access from your Mac OS Host.
For example for SSH: in the new port forward form, simply write 22 in Guest Port and what you want in Host Port (let's say 3022).
Now you can restart your VM and in a normal Mac OS Terminal, you can log into your VM with
ssh -p 3022 bob#localhost
If you don't want to type your password each time, copy the content of ~/.ssh/id_rsa.pub (from Mac OS) inside a newly created /home/bob/.ssh/authorized_keys text file, inside the VM.
6 - copying files via sftp
Sadly, I did not manage to access the content of the VM directly with the finder. I had to use the famous FTP client Transmit.
Create a new connection with:
Protocol: SFTP
Host: localhost
User: bob
Password: [your password]
Port: 3022
(yes, the port is the same as SSH)
You can now freely explore and copy files to and from your VM.
Oh but wait ... there is more!
7 - working with VS Code on your VM
Now you can also work on your VM, from your Mac OS VS Code, installing the Remote Development extention:
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack
Once the extention is installed, click on the green >< sign at the bottom left of your VS Code window and choose Remote-SSH: connect to host ...
Choose add new SSH host and type ssh -p 3022 bob#localhost
Now you can work in VS Code on a project inside your VM just as if it was in your local file system.
I do some Nuxt.js development that calls an API powered with Apache / PHP / MySQL (had to switch to MariaDB because I could not find an ARM version of MySQL that was working) that is all running in different Docker containers inside the VM using docker-compose.
Having Port Forwarded the guest 3000 port to the host 3000 port, I can browser the front end with Safari just as if it was all running natively on Mac OS.
I hope this all saves you some time.
For docker there is a technical preview out https://docs.docker.com/docker-for-mac/apple-m1/.
You can run ruby, python etc. directly on a Mac M1 by setting up a terminal that runs under Rosetta mode. Then run home brew and you can use the existing x86_64 architecture brew taps. I'm using /bin/bash as my Mac shell rather than zsh but you could adapt the below for zsh if you prefer.
Log in to your normal shell and install ARM homebrew to /opt/homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Now set up a new "Rosetta shell" terminal profile with the Shell tab Run command "env /usr/bin/arch -x86_64 /bin/bash --login".
Login again under rosetta shell and install x86_64 homebrew to /usr/local/homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Edit your ~/.bash_profile and add some code to detect if your shell is under Rosetta and if so use the /usr/local/homebrew instead of the usual /opt/homebrew
if [ "$(sysctl -n sysctl.proc_translated)" = "1" ]; then
# run under rosetta 2 with
# env /usr/bin/arch -x86_64 /bin/bash --login
#local brew_path="/usr/local/homebrew/bin"
eval $(/usr/local/bin/brew shellenv)
export PS1="i \D{%I:%M %p}:\w $ "
else
#local brew_path="/opt/homebrew/bin"
eval $(/opt/homebrew/bin/brew shellenv)
fi
Now login to your Rosetta shell and do commands like
$ brew install ruby
then you can run ruby.
I have also managed to get a vagrant virtual machine Fedora 33 for ARM running on Mac M1 under Parallels virtualisation beta. That might help with apache and php. Instructions here
https://github.com/peterdragon/packer-M1-parallels-fedora33
Seems everything will work as is...
From the event presentation they said "Existing Mac apps that have not been updated to Universal will run seamlessly with Apple’s Rosetta 2 technology."

How to setup the enviroment that bluepy can scan without sudo?

I wrote some Python3 script, that scans for devices. If they match a "name" I am connecting to them and do some ble stuff. The script is build on top of the bluepy module.
One think I don't like, is that I need to run the device scanning as sudo(like sudo python3 getDev.py). Any ideas how to make a user being able to scan without root rights?
Guess I need to add the local user to a group etc. Any ideas are welcome
On linux Bluetooth protocol stack need special privileges to interact with.
These privileges are implemented through properties called capabilities, see man 7 capability for details.
The tool to assign capabilities is the program setcap.
In case of bluepy it is the binary bluepy-helper that interact with the bluetooth protocol stack, so locate where it is installed the package bluepy and run:
sudo setcap 'cap_net_raw,cap_net_admin+eip' ${PY_SITE_PACKAGES_DIR}/bluepy/bluepy-helper
See also here

unable to flash micropython to esp 32: open serial error

issue: unable to flash micropython to esp 32: open serial error, please try again. hope to connect internet and try again.
pretty new to micropython - want to flash micropython to esp 32 ( resp 8266) board.
cf this docs: https://maker.pro/esp8266/tutorial/using-micropython-on-an-esp8266-with-upycraft
getting this errors all the time.
open serial error, please try again.
hope to connect internet and try again.
current version only open py txt json ini file.
hope to connect internet and try again.
hope to connect internet and try again.
i run Win 7
the hardware:
a. Joy-IT-Node MCU ESP8266
.. and besides that i also tried it with the following
b. ESP32-T Development Board CP2102 with Espressif ESP-WROOM-32 IoT WLAN & BLE Modul
yes: i have installed the > CP210x_Universal_Windows_Driver.zip
by the way: i could try to do all that on a linux machine. - on a MX-Linux-Sytem
any idea;
many thanks for any and all help in advance.
As of my opinion, You don't necessarily depend on the uPyCraft IDE. So there are several other ways to develop scripts and uploading them to Your board.
Some days ago I was facing problems with Python on the NodeMcu as well. Since I didn't like the tutorials around the web, I've set up one on GitHub, covering the end-to-end process (required software, drivers, firmware images, developing, flashing, uploading, etc.): python2nodemcu.
It depends on Mac and the NodeMcu board, but some of the topics may apply to other operating systems and boards as well.
And I think it covers the most relevant parts of the whole process, using the easiest tools around the web. Please have a look at it, but all in all it goes like this (short version of the linked documentation above):
Install Python 3
Verify with python --version
Or python3 --version
Install the correct SiLabs driver to enable serial USB-to-UART communication
See here
Find the device file (representing the interface to the physical device)
Try it using ls -la /dev/tty.*
Download the recent MicroPython firmware
You need the right one for Your board
Install the two Python-based libraries EspTool and PySerial
Try to erase the flash of Your device
Run python3 esptool.py --port /dev/tty.{device-file} erase_flash
Try to flash the original image or the MicroPython firmware to the board
Run python3 esptool.py --port /dev/tty.{device-file} write_flash 0x00000 {micropython-image-or-original-firmware}.bin
Try to connect to the board via the REPL prompt
Run screen /dev/tty.{device-file} 115200
You maybe need to try different baud rates (depending on Your driver, cable, etc.)
Connect to the board using Ampy
For example, try to list the files on Your device executing python3 ampy/cli.py --port /dev/tty.{device-file} --baud 115200 ls
Again, try other baud rates
Anyway, I've noticed that You do not necessarily need to push the physical flash button on the device.

Running an opencv based python script on a remote server with ssh forwarding from my macbook gives me an error

I am trying to run a python script on a remote server, which includes displaying images. The image does not get displayed and I get an error Gtk-WARNING **: cannot open display:
I have checked posts where they suggest editing the flags in sshd_config and also setting the DISPLAY variable manually. But, none of that seems to be working for me.
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost no
XAuthLocation /usr/X11/bin/xauth
Running xclock or xeyes also gives me errors.
Edit: I used ssh -X and ssh -Y to ssh into the server, neither worked
Solution: Restart after installing XQuartz
After looking through multiple posts and trying to make it work, I realised that after installing XQuartz, the user is required to restart the machine. It allows to set the correct environment variables (like DISPLAY). It works for me now after having restarted.
Alternative
However, in case you face a similar problem, not stemming due to the restart issue, I found an alternate way as suggested in the following link:
https://uisapp2.iu.edu/confluence-prd/pages/viewpage.action?pageId=280461906

Getting Notified on "Ethernet cable plugged in" events in linux

I am writing a Python Application running on linux. I want to be able to register and be notified by the system if a network cable is plugged in/ out.
I am already using pyUdev (python bindings for libUdev) in order to get notified for USB plug in events. However, because the network eth0 module (for example) always remains loaded (regardless of the cable being plugged in) , I dont get the information I require. It only works for if up/down events.
I have read a lot of posts on Windows WMI providing this functionality but none on linux. Am I looking in the right direction?
A python way of doing this (a python library) would be ideal as I want to integrate this with my program.
There are many ways to get the "UP" status of a network interface via polling, ranging from simply parsing ifconfig output, to reading the status of /proc/net/... pseudo-files.
However, for a way to be notified (i.e. not poll yourself) for such events, your best option on Ubuntu is to use the netplug package. It's not installed by default, you need to:
sudo apt-get install netplug
Some documentation is here: http://manpages.ubuntu.com/manpages/lucid/man8/netplugd.8.html
To set it up you need to tweak /etc/netplug/netplugd.conf and /etc/netplug/netplug scripts. For an example of those config files check out the section "Configuring netplug" at http://natisbad.org/dyn-net/index.html
With /etc/netplug/netplug you can have your own python script called when the network goes down/up, so you can generate your kind of event for your application.
Have you looked into netplugd command, I am not sure if a python wrapper exists but it should not be hard to parse the output

Categories