Lab-Proj 05.doc - Lab-Project 5 C Programming on Linux What You Need A Kali Linux virtual machine You could use other operating systems too if they have

Lab-Proj 05.doc - Lab-Project 5 C Programming on Linux What...

This preview shows page 1 - 4 out of 9 pages.

Lab-Project 5: C Programming on Linux What You Need A Kali Linux virtual machine. You could use other operating systems too, if they have a C compiler. Writing the hello.c Source Code In a Terminal window, execute this command: nano hello.c The nano editor opens. Type in the program shown below. #include <stdio.h> main() { printf("Hello World!\n"); } Save your file with Ctrl+X , Y , Enter . Compiling hello.c to Create the hello File In a Terminal window, execute these commands: gcc hello.c -o hello ./hello These commands compile the hello.c program, creating an executable machine language file named hello, and run the hello executable. You should see "Hello World!", as shown below.
Image of page 1
This program works, but it would be nicer if it greeted you by name, and if it put a couple of newline characters after the greeting to make it cleaner-looking. The next version, hello2, will add these features. Writing the hello2.c Source Code In a Terminal window, execute this command: nano hello2.c The nano editor opens. Type in the program shown below. #include <stdio.h> main() { char name[10]; printf("What is your name?"); scanf("%s", name); printf("Hi, %s\n\n", name); } Save your file with Ctrl+X , Y , Enter . Compiling and running hello2 In a Terminal window, execute these commands: gcc hello2.c -o hello2 ./hello2 These commands compile and run hello2.
Image of page 2
It should ask you for your name. When you type in your name (no longer than 10 characters), you should be greeted by name, as shown below: Crashing the hello2 Program With a Long Name--Buffer Overflow The hello2 program is poorly written, and exposes your machine to being exploited by hackers. That's because it takes the name from typed input and puts it in the name string, but the name string has a size limit--it only has enough room for 10 characters. Names longer than 10 characters will cause user-input data to overwrite parts of memory that were not intended to store data, making the program crash. This is a Buffer Overflow. In a Terminal
Image of page 3
Image of page 4

You've reached the end of your free preview.

Want to read all 9 pages?

  • Fall '15

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes