Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Overview

Lab Wanderings

You need to complete the following 8 experiments within one semester:

Data Lab - Code using very limited operators (^, |, &, etc.).

If you wonder what to code...
/*
 * bitXnor - ~(x^y) using only ~ and |
 *   Example: bitXnor(6, -5) = 2
 *   Legal ops: ~ |
 *   Max ops: 7
 *   Rating: 1
 */
int bitXnor(int x, int y) {
    // Replace 0 to your implementation of bitXnor.
    return 0;
}

Tip

You may want to check your answer:
int bitXnor(int x, int y) {
    return  ~(x | y) | ~(~x | ~y);
}

Note

Puzzles you actually meet would be more complicated.

Bomb Lab - Read assembly codes (x86-64) and solve puzzles.

If you wonder what the puzzles look like...

Your task is to defuse Phase 1.

The phase reads two signed decimal integers separated by whitespace. Analyze the assembly code and determine the only input that allows the function to return normally.

If the input is malformed or does not satisfy the required condition, the function calls explode_bomb, then you would permanently lose some points.

    .section .rodata
.Lformat:
    .string "%d %d"

    .text
    .globl phase_1
    .type phase_1, @function
    .extern explode_bomb
    .extern __isoc99_sscanf

phase_1:
    subq    $24, %rsp              

    leaq    8(%rsp), %rdx         
    leaq    12(%rsp), %rcx        
    leaq    .Lformat(%rip), %rsi  
    xorl    %eax, %eax            
    call    __isoc99_sscanf@PLT

    cmpl    $2, %eax               
    jne     .Lexplode

    cmpl    $6, 8(%rsp)            
    jne     .Lexplode

    movl    8(%rsp), %eax          
    addl    %eax, %eax             
    addl    $1, %eax               

    cmpl    %eax, 12(%rsp)         
    jne     .Lexplode

    addq    $24, %rsp              
    ret

.Lexplode:
    call    explode_bomb           
    addq    $24, %rsp
    ret

    .size phase_1, .-phase_1
    .section .note.GNU-stack,"",@progbits

Tip

You may want to check your answer:
6 13

Note

Puzzles you actually meet would be more complicated.

Attack Lab - Read vulnerable assembly codes and hack them.

If you wonder what 'hack' means...

Your task is to construct an input file that causes the program to execute touch1.

    .section .rodata
success_msg:
    .string "Attack successful: touch1 reached."

normal_msg:
    .string "Returned normally."

    .text

    .globl main
    .type main, @function

main:
    pushq   %rbp
    movq    %rsp, %rbp

    call    getbuf                  

    leaq    normal_msg(%rip), %rdi
    call    puts@PLT                # Printed only if getbuf returns normally

    xorl    %eax, %eax
    popq    %rbp
    ret


    .globl getbuf
    .type getbuf, @function

getbuf:
    subq    $40, %rsp               

    movq    %rsp, %rsi              
    xorl    %edi, %edi              
    movl    $128, %edx              
    call    read@PLT                

    movl    $1, %eax
    addq    $40, %rsp
    ret                             


    .globl touch1
    .type touch1, @function

touch1:
    leaq    success_msg(%rip), %rdi
    call    puts@PLT

    xorl    %edi, %edi
    call    exit@PLT                # Terminate after successful exploitation

    ud2                             # Should never be reached


    .size main, .-main
    .size getbuf, .-getbuf
    .size touch1, .-touch1

    .section .note.GNU-stack,"",@progbits

Note

If you want to execute the codes above, you need some magic. Try to compile with:

gcc -fno-stack-protector -no-pie -o your-file-name your-file-name.s

Note

Puzzles you actually meet would be more complicated.

Arch Lab - Write assembly codes that leverage pipeline to gain extremely high (time) efficiency.

Cache Lab - Write C codes that leverage cache (a.k.a. minimize times of cache misses).

Shell Lab - Write a tiny OS-level CLI shell.

Malloc Lab - Write a C-style memory allocator with high memory efficiency.

Proxy Lab - Write an I/O proxy of a server.

Happy coding~

Use Linux via CLI

Just like Windows, Linux is an operating system. Unlike Windows, Linux is distributed under an open source license, and it is extremely lightweight. For most common computer users, Windows (or MacOS) could perfectly meet all their requirements and there’s no reason for them to learn Linux. But for programmers or CS researchers like us, Linux would be our main operating system.

Tip

Want to learn a little more about Linux? Click ME!

All the labs of our course are running on Ubuntu 22.04 LTS. Ubuntu is one of the Linux distributions (or, in short form, “distros”). You may wonder what a Linux distribution is. In short, Linux just provide the “kernel” of an operator systems, with little user interfaces, which makes it hard to “use”. A Linux distribution provides a “shell” for Linux and through this users could use Linux more happily.

Tip

Want to install a Ubuntu virtual machine on your local (Windows or MacOS) machine? You could visit the official website of Ubuntu.

Ubuntu CLI Basics

Reference Materials

  1. What is Linux? - Linux.com
  2. Nature:Five reasons why researchers should learn to love the command line
  3. MIT CSAIL:The Missing Semester
  4. The Linux command line for beginners | Ubuntu
  5. Ubuntu CLI Cheat Sheet
  6. Ubuntu CLI Cheat Sheet (PDF ver.)