Question

Background: For this assignment, you will write a small encryption utility that implements a simple encryption...

Background:

For this assignment, you will write a small encryption utility that implements a simple encryption algorithm described below. The program will take one command line argument as an input; this will represent the word which is to be encrypted. As an output, your program will print the encrypted version of the word to the console using a simple printf() statement. This is the only output your program needs to produce.

There is an important catch, however: your program is going to be left intentionally vulnerable to a format string attack (also explained below). Though it will still “work” as intended if it is used appropriately with the expected input, it should be possible to exploit the program in order to produce something other than the desired output. In short, your program will be “hackable.”

Command-Line Input:

If you have not yet written a program that takes arguments at run-time via the command line, this will be worth exploring before starting the rest of the assignment. In short, it is possible to redefine main() in such a way that it can take input when the program is initially run. Your new definition of main should look something like this:

int main(int argc, char** argv) {

     // your code below

}

Here, the integer “argc” represents the counter of arguments (counting ./a.out as the first argument), and the string array “argv” stores those arguments. For example, if the program was run using this command:

./a.out myArg1 myArg2 myArg3

Then argc would be equal to 4 (representing the four arguments, with ./a.out being the first and myArg3 being the last), argv[0] would store “./a.out”, argv[1] would store “myArg1”, argv[2] would store “myArg2”, and so on.

Your program should take one additional argument, representing the word to be encrypted. That means it should be run like this:

./a.out thisIsTheWordThatMyProgramWillEncrypt

Note here that argc == 2 and argv[1] == “thisIsTheWordThatMyProgramWillEncrypt” (of course, any string will work here as long as it does not contain spaces).

For additional help with taking command line arguments in C, see the following resource: https://www.geeksforgeeks.org/find-largest-among-three-different-positive-numbers-using-command-line-argument/ (Links to an external site.)

The Encryption Algorithm:

You will implement a simple XOR encryption against the word that is passed in as a command-line argument. If you are unfamiliar with XOR in general or in C, please see the following resource before continuing with this assignment:

https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/ (Links to an external site.)

Your encryption algorithm will work as follows:

  1. A hard-coded hexadecimal integer will represent the secret encryption key. In most cases, to keep the encryption secure, this key must be kept secret; ultimately, you will write your program in such a way that it is possible to “hack” it and recover the key using an exploit in the C language. You can use whatever you like here, but it must spell a valid English word and it must be declared inside main as an int. Here are some ideas to get you started:
    1. BA55C1EF -> bass clef
    2. B01DFACED -> boldfaced
    3. C0FFEBEAD -> coffee bead (there’s no N in hex, unfortunately)

Anything similar to the above can work, as long as it is a valid hexadecimal integer that can represent an English word or phrase. This will help the graders to easily identify your key when it appears later on.

  1. Each letter of the input string will be XOR’d against the encryption key. You should use some kind of loop structure that looks like this:

int key = 0xBA55C1EF

for i from 0 to the length of the string:

          string[i] = string[i] ^ key

Format String Attack:

A format string attack is a type of injection attack that takes advantage of undefined behavior in the C language specification for printf() (the same printf() you’ve been using all semester). For this program, you must write your final printf() statement in such a way that it is vulnerable to a format string attack. To get you started, see the following reference:

https://www.geeksforgeeks.org/format-string-vulnerability-and-prevention-with-example/ (Links to an external site.)

Output:

Your program will only produce a single line of output, which in most cases is the newly encrypted string. To receive full credit, however, you must print your string in such a way that it is vulnerable to the format string attack described above. To verify that your program is working, try to run it with a format string attack of your own: you will know that it is working when they secret key you declared inside your program is clearly visible in the console.

Deliverables:

Submit, as a zipped folder, both your C source code (the .c file), as well as two screenshots of your output. The first will represent your program running under normal conditions (i.e., it is given a normal word as an argument and simply prints the encrypted output). The second should demonstrate that your program is vulnerable to the format string attack, and should include your encryption key somewhere in the visible output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in C

Read comment for a better explanation

#include<stdio.h>
#include<string.h>
//taking input from command line
int main(int argc, char *argv[])
{
//check if word is given as input or not by checking argc
if(argc<2)
{
printf("Enter word to be encrypted.\n");
return 0;
}

//Now argv[1] will contains word which need to be encrypted

//Define secret key in hexadecimal
int key = 0xBA55C1EF;

//To store encrypted text
char buffer[100];

//loop over argv[1] and encrypt every character using xor operation i.e., char ^ key
int i,buff_len = 0; //length of buffer
for(i=0;i<strlen(argv[1]);i++)
{
buffer[buff_len++] = argv[1][i] ^ key; //perform xor operation
}

//append null at last
buffer[buff_len] = '\0';

//For vulnerable to a format string attack we should write printf() without format string (%s)
printf("Encrypted: ");
printf(buffer);

//For decryption NOT asked in question just for validation
char dec[100];
int dec_len = 0;
for(i=0;i<buff_len;i++)
dec[dec_len++] = buffer[i] ^ key;

printf("\nDecrypted: ");
printf(dec);

return 0;


}

Output

Add a comment
Know the answer?
Add Answer to:
Background: For this assignment, you will write a small encryption utility that implements a simple encryption...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer...

    T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer is true or false in order to receive credit. #include <stdio.h> #include <string.h> int run_through(int num, char **a) { int i; int check=0; for(i=0;i<num;i++) { printf("%s\n", *(a+i)); if(strcmp(*(a+i), "filename")==0) { check=1; } } return check; } char** find_filename(int n, char **b) { int i; int check=0; for(i=0;i<n;i++) { if(strcmp(*b, "filename")==0) { b++; break; } b++; } return b; } int main(int argc, char **argv)...

  • Write a javascript program which implements the following two classical cryptosystem which we covered in class:...

    Write a javascript program which implements the following two classical cryptosystem which we covered in class: Affine Cipher Vigenere Cipher Your program should consist of at least five functions: Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string and a key as...

  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • Hi, need this question ansered in c++, has multiple levels will post again if you can...

    Hi, need this question ansered in c++, has multiple levels will post again if you can complete every level so keep an eye out for that. here is a sketch of the program from the screenshot int main (int argc, char** argv) { enum { total, unique } mode = total; for (int c; (c = getopt(argc, argv, "tu")) != -1;) { switch(c) { case 't': mode = total; break; case 'u': mode = unique; break; } } argc -=...

  • Problem: Write a program that behaves as described below.If the first command-line argument after the program...

    Problem: Write a program that behaves as described below.If the first command-line argument after the program name (argv[1]) is “--help”, print the usage information for the program. If that argument is not “--help”, you are to expectargv[1]and subsequent arguments to be real numbers(C, integer, float, or double types)in formats acceptable to the sscanf()function of the C library or strings of ASCII chars that are not readable as real numbers. You are to read the numbers, count them and calculate the...

  • I am using xcode Use the following ideas to develop a nonrecursive, linear-time algorithm for the...

    I am using xcode Use the following ideas to develop a nonrecursive, linear-time algorithm for the maximum-subarray problem. Start at the left end of the array, and progress toward the right, keeping track of the maximum subarray seen so far. Knowing a maximum subarray of A[1..j], extend the answer to find a maximum subarray ending at index j + 1 by using the following observation: a maximum subarray of A[1..j + 1] is either a maximum subarray of A[1..j] or...

  • Write a C++ program that takes two numbers from the command line and perform and arithmetic...

    Write a C++ program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. Do not use 'cin' or gets() type functions. Do not for user input. All input must be specified on the command line separated by blank spaces...

  • Please help run all the examples correctly and only the examples without extra things in or...

    Please help run all the examples correctly and only the examples without extra things in or less things in, it should run EXACTLY 100% like the examples. C language ONLY. And please dont reply if you cant run all the examples given. We know the tr command allows you to replace or translate characters in of a stream. It takes in two arguments - a set of characters to be replaced and a set of replacement characters. The full functionality...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT