C Language

Let's first start refreshing your memory with a couple of small interesting programs in C.

Why?

Because you are always going to be dealing with Executables compiled in C and C++; A better understanding and grip on these languages will help you a lot with solving challenges in CTFs. Also keep in mind that a lot of tools are made with C. A great grip of C will help you contribute to open source tools and maybe build a few yourself. PS : You have a easier time at college. :)

Assignment: C language

If you have not learned C before, don't worry this is your chance to :)

Try taking up a C refresher such that you have:

  • Good idea of Loops(while, for, switch) and control statements.
  • Good grip over functions and functions calls.
  • Basic idea about pointers (Syntax, Usage).
  • Basic knowledge on Structures and objects.
  • Basic File I/O.
  • Recursion.

If you are familiar with all these steps lets just complete a few simple C programming questions. These questions may need you to search around on the internet and also a bit of testing and tweaking.

Programming Questions

  1. Pointers are pretty important in c. A thing clarifying pointers would be nice. So try to implement the python int() function in C. Like atoi(char* ptr, int base) to return the integer stored at the pointer ptr.

    • The function accepts a pointer to a string and an integer mode as arguments and outputs the appropriate number.
    • the format should be Int(char* numstring, int base)
      1
      2
      3
      4
      5
      Example:
        Int(234234234, 10)
        Output: 234234234
        Int(64, 16)
        Output: 40
      
    • Banned functions: ato[--anything]
  2. Implement a function “print_this” in C which is the equivalent of the printf in C.

    • The function should accept a format string and the number of arguments as specified by the format string.
    • The function should be able to accept and process:
      • Strings -eg print_this(“%s”,ptr) should print ptr as a string
      • Hex numbers -eg print_this(“%x”, 10) should print 0xa
      • Octal numbers
      • Binary numbers
    • %p, %u, %d should all be handled
    • Numbers before like %02d should be used to specify bytes printed on the screen.
    • %$ should be treated as a illegal format string that cannot be used and print_this should print nothing and return an error. An EACCESS error must be returned.
    • Should print other illegal escape sequences directly
    • The function should not use printf. It can use putc to print to the screen.
    • The function should be available to use by other programs. Write a header file so that other functions can import this function.
    • Macros must be used to denote any integer values used inside thefunction.
    • The function should also return proper values to denote that the execution happened properly
    • Also make sure that the number of arguments passed is same as the number of the format strings present. Incase this is not same the function should return some error.
    • Feel free to add any good modifications that you feel will bebeneficial for yourself and the community.
Links to be Referred