Lab 1: Linux Setup

When you're done with this lab you will have done the following things:

  1. Install Git on WSL2
  2. Set up your Linux for running ARM assembly.
  3. Practice basic Linux command lines.
  4. Fix a broken image by hacking the raw binary file

The very last task does not need to do on Linux, so it can be done even if you are having issues that delay the setup of your WSL2.

Part 1: Git on Linux

Goals of this section:

If you haven't yet, please complete the prelab.

Launch your Linux in WSL2, and it's time to play around Linux with a terminal. For the rest of this lab, you're going to use a number of linux commands. Here's a quick reference for some of them:

Also, in order to run things as an administrator (for example, to install programs), you need to use a command called sudo. This temporarily turns you into an admin called root. Try it!

user@hostname ~ $ whoami
username
user@hostname ~ $ sudo whoami
root

Stage 2: Install a command-line web browser

Since your sole access to the Linux is via command line, lets install a browser that works in a text-only environment! To do this, we will use apt.

user@hostname: ~ $ sudo apt update
user@hostname: ~ $ sudo apt install links

It will ask you if it's ok to install stuff. Type "y" and hit enter.

Once it's installed, launch it and see if you can view the course website! (Hint: hit the "ESC" key to show menus and "q" to quit).

user@hostname ~ $ links http://www.rose-hulman.edu/class/csse/csse132/

SHOW YOUR INSTRUCTOR

Now is a great time to print out the instructor verification sheet and show your instructor.

Stage 3: Install Git

Next, you need to install the git client so you can get your lab and homework materials. Use apt to install it!

user@hostname ~ $ sudo apt install git

NOTE: your Linux may already have Git installed. If it does, running this command will result in a message such as:

git is already the newest version

Stage 4: Use Git

Clone your class repository

Clone your repository using a command like this, replacing <username> with your RHIT username. NOTE: <username> appears twice; replace it both times. Use the git clone command:

git clone ssh://<username>@gitter.csse.rose-hulman.edu:/srv/git_repos/2223b-csse132-<username>

Use your RHIT password when asked for a password.
NOTE: GIT may ask you if you want to accept a key. Choose "yes".

Once it's checked out, use the cd command to go into the directory, and the ls command to show your repository's contents. HINT: you can type part of the command (like "cd 1819" and then hit tab to auto-complete the directory).

user@hostname ~ $ cd 2223b-csse132-username/
user@hostname ~/2223b-csse132-username $ ls
lab1
user@hostname ~/2223b-csse132-username $

Configure Git

Next, you will have to tell Git your username and email address. Use the git config command to do this.

git config --global user.name "YOUR NAME"

(replace "YOUR NAME" with your name)

git config --global user.email youremail@rose-hulman.edu

(replace "youremail@rose-hulman.edu" with your Rose email address).

Create and push a new file.

Use the cd command to go into the lab1 folder. Once you're in there, create a new file called "itworks.txt":

  1. type nano itworks.txt to create the file and edit it using nano.
  2. Put your name in the file, and write a fact about yourself (for example, "I like cats").
  3. Save the file in nano (hold the Ctrl key and type the letter O)
  4. Exit nano (hold The Ctrl key and type the letter X)

Now that you've got the file, commit it to your git repository. First, pull any change from the server:

git pull

Then, you need to tell git about the file:

git add itworks.txt

Commit your changes:

git commit -m "added new file in lab 1"

Once it verifies your commit, it's time to upload your new file to the server.

git push

You've committed your first git file! Good job!

SHOW YOUR INSTRUCTOR

Check this item off the Instructor Verification Sheet.

Part 2: Get Linux Ready for ARM Assembly

Install Toolchain and QEMU

Run the following command in order to install necessary libs and packages.

  sudo apt update -y && sudo apt upgrade -y
  sudo apt install gcc-arm-linux-gnueabihf binutils-arm-linux-gnueabihf binutils-arm-linux-gnueabihf-dbg
  sudo apt install qemu-user qemu-user-static gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu binutils-aarch64-linux-gnu-dbg build-essential

Compile and Run an ARM program

In Linux, navigate to the hello_ARM folder under lab1 folder under your git repo. This can be done with the following command:

 username@hostname ~ $ cd  2223b-csse132-username/lab1/hello_ARM

Using ls command, you can check the files under this directory:

username@hostname ~/2223b-csse132-username/lab1/hello_ARM $ ls
Makefile hello.s

Then type make to compile the ARM source code. Usually, you will see output like this

username@hostname ~/2223b-csse132-username/lab1/hello_ARM $ make
arm-linux-gnueabihf-as  hello.s -o hello.o
arm-linux-gnueabihf-ld -static hello.o -o hello

Now you can run the executable file by typing make run. Your setup is successful if you can see the message below:

Hello, ARM32!
If you see this message, your ARM env setup is successful!!

SHOW YOUR INSTRUCTOR

Check this item off the Instructor Verification Sheet.

Part 3: Learning and Practicing Linux Command lines

Open a web browser and go to the bandit instruction page using the link below:

http://overthewire.org/wargames/bandit/

Follow the instructions and get into level 5. After finishing each level, save the password on your computer (maybe in notepad). You will need this info later.

SHOW YOUR INSTRUCTOR

Check this item off the Instructor Verification Sheet.

Part 4: Fix an Image

Finding the image: Open File Explorer and type \\wsl$ then enter. You should be able to see "Ubuntu" (If you don't, launch Ubuntu in Windows Terminal first). Keep navigate to the lab1 folder with this path: "Ubuntu" -> "/home//2223b-csse132-/lab1". There you can find the lab1_broken_pic.png To make this image easy to access, copy it to the Desktop or somewhere you can find.

In this part, you will need to tweak the data of an image file to help fix a broken image. To modify the raw data of a file, you need special tools to do that. Frhed is a free binary file editor available on Windows. Follow the link below to download the tool:

Frhed Download Link

Usage:

Wrong Format

PNG defines a certain format that a picture file has to follow. If a picture does not strictly follow this format, computers can not recognize it correctly, and thus fail to open it as a picture. This is what happened for this picture.

To fix it, you need to first understand the format specs of PNG. Check the PNG Format Specs page and particularly focus on the File Header section.

Now, open the lab1_broken_pic.png, and inspect the header (the few bytes at the beginning) to see if it matches the format specs in the Wiki page. Fix the incorrect byte.

If you successfully correct the header, the picture can be opened as a picture to view.

Wrong Size

When opening the picture, you will see extra white space below the picture. That's because the size specified in the file is not correct, specifically the height is too big. (You can find the size information by right clicking the file -> Properties -> Details Tab.)

Check again the PNG Format Specs to find the bytes that define the height.

Hint: After the header, comes a series of chunks, each of which conveys certain information about the image. The height is defined in the IHDR chunk, the very first chunk of the file.

Once you find the bytes that define the height, change it from 666 to 410. First, convert these two numbers to hex and then edit the bytes accordingly.

If you did that, your image file should be broken again. If it is not broken, it is just because the software ignores the error, which is exactly Windows tends to do.

Fix the CRC (Optional)

The reason it should break is because the software should notice we edited (corrupted) the file so it should consider the file is not safe. The way the computer detects this problem is to use certain bytes in the file to calculate a validation code, called CRC (cyclic redundancy checksum). It will compare the computed result with the checksum stored in the file. If the result doesn't match, it knows the file is corrupted. In our case, the CRC for the IHDR chunk is not correct given the modified height info. To find the the CRC, pay attention to the "offset" shown at the bottom left corner of the app: the CRC's first byte is at the offset/address 0x1D.

We need to re-calculate the CRC. Once we know which bytes we need to use to generate the new CRC, we can simply use this website CRC calculator to calculate new CRC. Based on the specs, we know the CRC for this chunk is calculated over the chunk type and chunk data (Hint: from offset 0xc to 0x1c). So, type those bytes into the website and choose CRC32 to calculate the new CRC. Then change the CRC in this chunk.

Once you have done that, you should be able to open the file correctly with the proper size.

Finishing the Lab

  1. Submit any electronic files you create to git (using git add <file> for each file, then git commit and git push.). (Probably you already did that for this lab)

  2. To make sure your git submission has successfully reached the server, a quick way to check is using the following command: git log --stat -1
    Make sure you see your latest commit (e.g., adding test.txt) appears in the output. Also, double-check if the first line of the log message includes origin/master in the parentheses. If not, try to git push again.

  3. Submit the instructor verification sheet in hard copy.