Free Essay

C Programming on Linux

In: Computers and Technology

Submitted By jamest2013
Words 1293
Pages 6
C Programming on Linux
What You Need for This Project * 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.

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); } >>>Take a screen capture for to include in your report
Save your file with Ctrl+X, Y, Enter.
>>>Take a screen capture for to include in your report
Compiling and running hello2
In a Terminal window, execute these commands: gcc hello2.c -o hello2
./hello2
These commands compile and run hello2.
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
>>>Take a screen capture for to include in your report

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.

Special Note: 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 window, execute this command:
./hello2
When you see the "What is your name?" prompt, type in this name, followed by the Enter key:
1234567890123456789012345678901234567890
You see a "Segmentation fault" error, as shown below.

Although this just crashes the machine, which could result in a denial of service, with carefully crafted false data it is often possible to use such errors to open a shell on the host, giving you complete control over it. That's how many of the Metasploit exploits work.
Saving a Screen Image
Make sure the "Segmentation fault" error message is visible, as shown above.
Click the taskbar at the bottom of your host Windows 7 desktop, to make the host machine listen to the keyboard, instead of the virtual machine.
>>>Take a screen capture for to include in your report
Writing the hello3.c Source Code
We need to patch this code. So we'll make another version.
In a Terminal window, execute these commands: cp hello2.c hello3.c nano hello3.c
This makes a copy of hello2.c named hello3.c, and opens it in the nano editor.
The nano editor opens. Modify the scanf call to match the program shown below: #include <stdio.h> main() { char name[10]; printf("What is your name?"); scanf("%10s", name); printf("Hi, %s\n\n", name); }
Save your file with Ctrl+X, Y, Enter.

>>>Take a screen capture for to include in your report

Compiling and running hello3
In a Terminal window, execute these commands: gcc hello3.c -o hello3
./hello3
When you see the "What is your name?" prompt, type in this name, followed by the Enter key:
1234567890123456789012345678901234567890
The program now runs without an error, as shown below:

The program now just ignores any characters after the first ten. There is no error message, and no stack overflow. The program is patched. This is what many Microsoft security patches do--correct code to remove buffer overflow vulnerabilities.
By the way, this is a sloppy patch, because it leaves some keyboard characters in an input buffer which could lead to unexpected results later in the program. For a more thorough way of patching scanf, see Activity 7-2
>>>Take a screen capture for to include in your report
Using ping
In a Terminal window, execute this command: ping 192.168.1.1
That number is the default gateway in S214. If you are not in S214, use your default gateway instead of that address.
You should see a series of lines starting "64 bytes from", as shown below:

Press Ctrl+C to stop the pings. In a Terminal window, execute this command: ping 192.168.1.1 -w1
Note that the last two characters are the letter w and the numeral 1.
This makes ping end faster--it stops after one second.
>>>Take a screen capture for to include in your report
Writing the pingscan.c Source Code
We will make a simple ping scanner, like one of the Nmap functions. It will ping each of 100 IP addresses for one second to see if there is any response. This works, although it is a lot slower and clumsier than Nmap.
In a Terminal window, execute this command: nano pingscan.c
The nano editor opens. Type in the program shown below. #include <stdio.h> main() { int i; for (i=0; i<100; i++) printf("ping 192.168.1.%d -w1\n", i); }
Note that the last two characters in the printf format string are the letter w and the numeral 1. (If you are on a different subnet, replace 192.168.1 with the first 3 numbers in your LAN's IP address.)
Save your file with Ctrl+X, Y, Enter.

>>>Take a screen capture for to include in your report

Compiling and running pingscan
In a Terminal window, execute these commands: gcc pingscan.c -o pingscan
./pingscan
The program prints 100 ping command lines on the terminal, as shown below. However, it doesn't execute the PINGs, it just prints out the commands.

Making a Script File with Output Redirection
To make the commands execute, we need to put them into a file and make the file executable.
In a Terminal window, execute this command:
./pingscan > ping100
You see another # prompt with no message, which is what Linux does when there is no problem. The > sign is the output redirection operator, and it took the lines of text that were going to the screen and put them into a file named ping100 instead.
In a Terminal window, execute this command: nano ping100
The ping100 file opens in the nano editor, as shown below. It has 100 ping commands.
Press Ctrl+X to close nano.

>>>Take a screen capture for to include in your report

Making ping100 Executable
In a Terminal window, execute this command: chmod a+x ping100
This command changes the mode of the ping100 file to make it executable by all users.
In a Terminal window, execute this command:
./ping100
The ping scan should run, with results like those shown below. It will take about 100 seconds to finish.

Saving a Screen Image
Make sure the Terminal window is visible, showing some of the pings.
>>>Take a screen capture for to include in your report

Similar Documents

Free Essay

Linux Paper

...Linux Features of Red Hat Red hat has many different features, I will cover a few of the main features in this section, and Red Hat contains more than 1,200 components covering a broad range of functionality. Red Hat Enterprise Linux provides CIOs and IT managers with the means to reduce costs while improving operational flexibility throughout their computing infrastructure. The following list provides a brief summary of the more important features: * Virtualization is provided in all Red Hat Enterprise Linux server products and is optionally available for desktop products. * Storage and extended server virtualization are provided with Red Hat Enterprise Linux Advanced Platform. * Red Hat Network supports virtualized guest operating systems * Virtual-manager, other management tools are available for single system or scripted virtualization management. * Integration with Red Hat Enterprise Virtualization is available for enterprise virtualization management. Networking & interoperability * Network storage enhancements include Autofs, FS-Cache, and iSCSI support * IPv6 support and conformance enhancements * Improved Microsoft® file/print and Active Directory integration, including support for Windows Security Features * SE Linux enhancements include Multi-Level Security and targeted policies for all services * SE troubleshooter GUI simplifies SE Linux management * Integrated directory and security capabilities * IPSEC enhancements...

Words: 769 - Pages: 4

Free Essay

Python

...NOS 120 Linux/UNIX Single User Compiling Linux Software from Source Code Objectives After reading this chapter and completing the exercises, you will be able to: * Understand basic elements of C programming * Debug C programs * Create, compile, and test C programs Text Reference: Chapter 10 – Developing UNIX/Linux Applications in C and C++ Programming Overview A computer program is a list of instructions given to a computer to make it perform a specific task or series of tasks. A programmer communicates these instructions to the computer in a language the computer understands. Computers, however, can only operate using binary code (1’s and 0’s), which makes a computer’s language very difficult for humans to understand. The solution to this problem is to create an intermediate language that both humans and computers can understand. These are called programming languages. Programmers create a list of instructions for the computer in a programming language such as C, C++, or Java. The C Programming Language The C programming language was released in 1973. C was created by Brian Kernighan, Dennis Ritchie and Ken Thompson working at Bell Labs. Both the UNIX and Linux operating systems are written in C. The C programming language is a high-level language because the code itself is written in relatively English-like statements. In 1978, Kernighan and Ritchie wrote "The C Programming Language," still one of the best C programming books available...

Words: 2308 - Pages: 10

Free Essay

Usb Usb Driver

...through USB interfaces. Currently most general purpose operating systems provide support for USB devices, and it is relatively easy to develop applications in C that access such peripherals. Linux operating system do not provide remote access through USB between different machines .Here we utilize its high bandwidth to transfer files between machines using USB without using any network cables. 1.2 Objectives The USB remote file access allows high speed transferring of files between two machines. 1.3 USB Cable Fig 1.1 A USB Cable 1.4 Advantages Of Using USB Cable for Remote Access • Transfer files between two PCs (or three PCs using two cables) • Fully compliant with USB 1.1 and USB 2.0 • USB 2.0 High-speed performance with data transfer rate of over 15 Mbps • No external power needed (Bus powered from either USB port) • Low power consumption • Supports suspend and resume for power management • Supports remote wake-up • Terrific for laptop/notebook to desktop file transfers. 1.5 Module Overview This project mainly contain two parts 1.Kernel Module (USB driver) 2.User Interface 1. Kernel Module In this module we create a device driver for USB direct link cable which is used to connect between two systems. The driver is designed in such a way that it should be efficient and simple to the Linux operating system. The files are transmitted using file transmission protocol. The low level system call...

Words: 557 - Pages: 3

Free Essay

It250

...Chapter One (History of Linux): The history of Linux begins not with the release of Linux in 1991 but with the creation of GNU (which stands for Gnu’s not UNIX). This started with the purpose of creating a free OS to anyone who wanted with. By 1991 everything was complete except for the kernel which Linus Torvalds provided. The Code was free, this dated to back to when UNIX was released to Universities for a low cost and that way its students would be familiar with it and it would slowly permeate the marketplace once those students got out of the schoolhouse and made their way up the ladder of business. Some of the best things about Linux is the fact that is has a large selection of applications written for it that range from word processing to graphical tools to security administration software. It provides a wide range of peripherals and easy to install software. It works on a variety of platforms as well with a variety of hardware which allows it to be extremely portable. Another big key in this development is the creation of UNIX in B programming language which gave rise to C, C++ and Objective C. Linux has the shells to be able to interpret command language and programming language. GUI allows people to customize the desktop environment to suit their needs or tastes. Chapter Two and Three (Installation and Step by Step Installation): In the installation of Linux there are many different mediums to install it from. You can install it form a Live CD, An install...

Words: 539 - Pages: 3

Free Essay

Linux and Windows Server Critique

...Linux and Windows Server Critique Team C POS 420 June 25, 2012 Yevgeniy Tovshteyn Linux vs. Windows Both Linux and Windows are operating systems with advantages and differences in functionality and user friendliness. With networking becoming an important part of company operation in this day and age. Kudler will come to depend on networking for availability to the marketplace via the Internet, while requiring their internal systems for email, Voice over Internet Protocol (VoIP) phone systems, and using business data. Linux and Windows are the only 2 operating systems that have grown to the top level in the field of networking. Learning Team C examines these types of strengths and weaknesses regarding security, administration, performance, and programming. Kudler Fine Foods Current Systems: 3 locations: La Jolla, Del Mar, and Encinitas La Jolla Novell 4.11 Server for POS 4 POS Workstations with Pentium II, Windows 9X 1 Standalone UPS (Uninterruptable Power Supply) 1 Server with Inventory Spreadsheet with Pentium II, 64mg Ram, Windows 9X, external CD burner and bubble jet printer. 1 Server with NT Server 1 56K modem Del Mar Novell 4.11 Server for POS 4 POS Workstations with Pentium II, Windows 9X 1 Standalone UPS (Uninterruptable Power Supply) 1 Server with Inventory Spreadsheet with Pentium II, 64mg Ram, Windows 9X, external CD burner and bubble jet printer. 1 Server with NT Server 1 56K modem Encinitas Novell 4.11 Server for POS 4 POS...

Words: 1833 - Pages: 8

Premium Essay

Doc Remove Delibitablement

...| Hours | Functional Analysis | 3,5 | 48 | Algebra for Engineers | 2,5 | 32 | Probability 1 | 3,5 | 48 | Statistical Decision (courses +Tuto) | 3,5 | 48 | Microprocessor System | 3 | 40 | Signal Transmission | 2,5 | 32 | Data Transmission | 2,5 | 32 | Workshop on Linux | 3 | 40 | Databases | 3 | 40 | TOEIC 1 | 2,5 | 32 | Advanced Maintenance | 2,5 | 32 | Numerical Analysis | 2,5 | 32 | Operations Research | 2,5 | 32 | Servo (Tuto) | 2,5 | 32 | Servo (Courses) | 2,5 | 32 | Algorithm (Data Structure) | 2,5 | 32 | Algorithm oriented object (Tuto, C++ Language) | 3 | 40 | Operating System (Theories and Fundamental) | 2,5 | 32 | WAN (courses + Tuto) | 4,5 | 60 | Method of Analysis 1 | 3 | 40 | Programming Workshop C | 2,5 | 32 | Software Engineering workshop (Access, VB) | 3 | 40 | Management Workshop for Science Engineer | 2 | 24 | Entrepreneurship | 1,5 | 20 |   |   |   | TOTAL | 63,5 | 832 | ------------------------------------------------- OBJECT ORIENTED ALGORITHM ------------------------------------------------- (Hands-On in Language C + +) CHAPTER I: GENERAL ON CLASS I. Notion of class • Generality of P.O.O • Incompatibility C / C + + II. Property of the member functions • Defaults • Member functions in-line • Transmission of object as argument III. Object assignment IV. Object Constructors and Destructors V. Object initialization VI. The copy constructor VII. Tables to Objects CHAPTER...

Words: 2262 - Pages: 10

Free Essay

Vivek

...JULY 2011) Department of Computer Applications Makhanlal Chaturvedi Rashtriya Patrakarita Evam Sanchar Vishwavidyalaya B-38, Press Complex, M.P. Nagar, Zone-I, Bhopal Ph.: 4294448, 2768274 www.mcu.ac.in SCHEME FOR POST GRADUATE DIPLOMA IN COMPUTER APPLICATIONS (PGDCA) (Effective From July 2011 Session) SEMESTER -I Subject Code Subject Name Scheme L 1PGDCA1 1PGDCA2 1PGDCA3 1PGDCA4(A) 1PGDCA4(B) Fundamentals of Computers & Information Technology Introduction to Operating Systems (Dos, Windows, Linux) PC Packages Elective - 1 Foxpro MS-Access 4 4 1 3 T P Theory Paper 80 80 Internal Evaluation 20 20 Practical Exams Total Marks 100 100 4 4 1 3 3 80 80 20 20 25 25 125 125 Semester Total 450 (*L-Lecture, T-Tutorial, P-Practical) SEMESTER –II Subject Code Subject Name Scheme L 2PGDCA1 2PGDCA2 2PGDCA3 2PGDCA4(A) 2PGDCA4(B) 2PGDCA5 System Analysis and Design Programming with Visual Basic.NET Internet & E-Commerce Elective - 2 OOPs & Programming with C++ Financial Accounting with Tally Project Work 4 4 4 4 T 1 P 3 3 3 Theory Paper 80 80 80 80 Internal Evaluation 20 20 20 20 Practical Exams Total Marks 100 125 100 125 25 1 25 4 100 Semester Total 550 General Instructions: 1. For passing the subject examination minimum 40% marks must be separately scored in Theory Paper, Practical Exams and Internal Evaluation in the subject. 2. For passing the semester, minimum aggregate marks must be 45% in the semester. Course : PGDCA...

Words: 4021 - Pages: 17

Premium Essay

Nt1430 Chapter 1 Questions

...terms/licensing. 2. Why is Linux popular? Why is it popular in academia? -Linux is popular for a number of different reasons. Its free software that is portable because it’s written in C, has an amazing range of supported "peripherals", has a lot of software available, and has standardization (POSIX). It’s generally popular in academia because it’s a free, open-source, portable, sophisticated programming environment that computer science students could use as a learning platform. 3. What are multiuser systems? Why are they successful? -A multiuser system is an operating system that allows multiple users to access a single computer's resources. It’s successful because it’s hard for a single user to use 100% of a modern computer’s resources, and allows for greater efficiency. 4. What is the Free Software Foundation/GNU? What is Linux? Which parts of the Linux operating system did each provide? Who else has helped build and refine this operating system? -GNU stands for "GNU'S Not Unix", and it was designed to be a UNIX-like operating system developed by Richard Stallman. Linux is the name of operating system kernel developed by Linus Torvalds. GNU's tools aside the Linux kernel make the operating system known as “Linux” aka “GNU/Linux”. Thousands of people helped refine the operating system online. 5. In what language is Linux written? What does the language have to do with the success of Linux? -Linux is written in the C programming...

Words: 750 - Pages: 3

Free Essay

Nothing

...Term Paper Synopsis On The Topic:- Shell for LINUX (Details of specifications & services of different old & new shell interfaces of Linux) Submitted to: - Submitted by:- Mr. Akash Bharadwaj Deepak Gautam 11010183 K2R21- A 08 MCA-M.tech (cse) What is shell an introduction? The shell sits between you and the operating system, acting as a command interpreter. It reads your terminal input and translates the commands into actions taken by the system. The shell is analogous to command.com in DOS. When you log into the system you are given a default shell. When the shell starts up it reads its startup files and may set environment variables, command search paths, and command aliases, and executes any commands specified in these files. The original shell was the Bourne shell, sh. Every UNIX platform will either have the Bourne shell, or a Bourne compatible shell available. The default prompt for the Bourne shell is $ (or #, for the root user). Another popular shell is C Shell. The default prompt for the C shell is %. Shell script Normally shells are interactive. It means shell accept command from you (via keyboard) and execute them. But if you use command one by one (sequence of 'n' number of commands) , the you can store this sequence of command to text file and tell the shell...

Words: 581 - Pages: 3

Free Essay

C Language

...UNIT 1 NOTES Digital Computer A digital computer is an electronic computing machine that uses the binary digits (bits) 0 and 1 to represent all forms of information internally in digital form. Every computer has a set of instructions that define the basic functions it can perform. Sequences of these instructions. Component of Digital Computer: (1)CPU: The Central Processing Unit (CPU) or the processor is the portion of a computer system that carries out the instructions of a Computer, and is the primary element carrying out the computer's functions. This term has been in use in the computer industry at least since the early 1960s . The form, design and implementation of CPUs have changed dramatically since the earliest examples, but their fundamental operation remains much the same. (2)ALU: an arithmetic logic unit (ALU) is a Digital computer that performs arthimatic and logical operations. The ALU is a fundamental building block of the central processing unit(CPU) of a computer, and even the simplest microprocessor contain one for purposes such as maintaining timers. The processors found inside modern CPUs and graphics processing units(CPU) accommodate very powerful and very complex ALUs; a single component may contain a number of ALUs. Mathematician proposed the ALU concept in 1945, when he wrote a report on the foundations for a new computer called the EDVAC. (3)Memory: memory is an organism's ability to store, retain, and recall information. Traditional studies of memory...

Words: 2515 - Pages: 11

Premium Essay

Linux Kernel Advantages

...The kernel is the most crucial part in the operating system, as it is the connector that links the applications and the components at the hardware level. The kernel is the only part or component of the operating system that keeps on working at all time and is the first to launch, used by programmers and developers. The kernel plays a vital part in managing the system resources and responds to any kind of system call, interrupts and exceptions. The kernel in both operating systems Windows and Linux have a lot in common as for example they both interact with the hardware Both of these kernels are written in C language which is a high level language because the C language have been included to all hardware, which will consume less time and...

Words: 889 - Pages: 4

Free Essay

Programming

...http://coolshell.cn/articles/4990.html 6 12 programming An open letter to those who want to start id Mailper Python Web “ Build Your Programming Technical Skills “ ( ) Notes/ActiveX/COM/ADO/ATL/.NET …… Mailper Delphi/Power builder ” Lotus 5 3D TA 561 ” The architecture of w-ai.org –... Some updates and my first Engl... / 10 Unix/Linux Windows 1 Linux Web+ 1 Python/Ruby Web 2 iOS Android Windows Windows Windows Linux+ Windows 9 : csv ( log python csv, python open, python sys) 52 word count (sys, os, path) (python sqlite) print Google (phrase, domain, use reader to follow tech blogs) 2 ( Vim / Emacs / Notepad++ Source Insight ( ctag) Cool IDE) / / 3 Unix/Linux Shell windows man ls/chmod/chown/rm/find/ln/cat/mount/mkdir/tar/gzip … sed/awk/grep/tail/less/more … ps/top/lsof/netstat/kill/tcpdump/iptables/dd… /etc /var/log /proc linux vmware player Ubuntu 1 6 9/6/13 1:36 PM - -[ ] http://blog.renren.com/blog/73603/740437492 /Linux 4 Web Web HTML CSS HTML Firefox + Firebug Javascript HTML DOM Firefox + Firebug Apache PHP PHP PHP chrome Nginx HTML MySQL MySQL SQL http://www.stanford.edu/~ouster/cgi-bin/cs142-fall10/index.php ) javascript HTTP: The Definite Guide browsers) Cookie/Session jQuery 4 3-5 ExtJS + Ajax ( +JSON (proxy, gateway, Javascript box model chrome DOM http://oreilly.com/catalog/9780596527402) HTML/CSS/JS) + WEB (LAMP) W3School 1 C C C C Unix/Linux fork/wait/waitpid signal/kill/raise/alarm/pause/sigprocmask...

Words: 807 - Pages: 4

Premium Essay

Linux vs Windows

...Windows vs. Linux Enterprise Distribution Recommendation Introduction to UNIX Linux vs. Windows Enterprise Distribution Recommendation X Industries is a large sized business that is in the midst of a decision regarding wither or not to switch their existing Windows-based IT system over to a less proprietary product, Linux. This is no easy choice to make, considering X Industries is currently running 170+ Windows-based servers of various types. Their staff fulfills company operations on these servers with their 750+ array of desktop and laptop computers – all running Windows XP Professional. Linux may just be a little dog in the Microsoft park, but a company that makes the big switch can expect Linux to yield benefits in cost savings, increased security, and arguably better usability. However, each individual enterprise situation is always different, so a cookie cutter solution of which system to use will never suffice. Rather, the numbers through subjects of cost, market share, programming capabilities, software compatibility and availability, and the ever-important user interface will tell the best choice. Cost The only way to evaluate cost is to get down to the basics, and look at the statistics. The first topic of cost will center solely on the price for Windows operating system software, first for the network side, then the node side. Other topics will focus on the cost of the Ubuntu Linux distribution, which will run on the servers and desktops,...

Words: 3478 - Pages: 14

Premium Essay

Nt1330 Unit 3 Assignment 1 Shell Script

...In this introduction, I provide any overview of the concepts regarding shell scripts. Explaining what is a shell script is and describing several common shells. Later, a tutorial is made available for demonstration how to backup a file or folder using a tool to accomplish these tasks. The SNTbackup is a shell script backup tool that gives users the option to select to select a file or folder to be backed up. A detail demonstration with screenshots is provided as a visual aid to readers. I used a simple Linux shell script to back-up the home directory of my system. The script uses the tar command to execute this task. SNTback is a simple, effective, fully customizable backup script that protects your folder by creating a duplicate of the specific folder. The duplicate of the selected folder is the place in a location of the user’s preference. Users will find the SNTbackup as a resource which allows folders such as directories to be back-up in no time....

Words: 1666 - Pages: 7

Premium Essay

Comparing Unix and Windows Operating Systems

...WINDOWS OPERATING SYSTEMS The Comparisons of UNIX and Windows Operating Systems Herlinda Gibson Instructor: Richard Guirguis CIS 155 – Strayer University August 20, 2012 Abstract In today’s world filled with computers and computer technology, having an Operating System (OS) is a must in order to utilize or do anything on your computer but what are the differences in the operating systems and which one would you use? This paper will investigate and analyze some of the similarities and difference between two major Operating Systems; UNIX and Windows XP by comparing and contrasting with some key features such as; cost measurements, market share availability, hardware requirements, file processing, programming capabilities, availabilities of applications software and graphical user interface (GUI) with the hopes of selecting a better computer when purchased. The Comparisons of UNIX and Windows Operating Systems We now live in a world that is basically run by computers and in order to do anything on your computer you will need to have or run some type of Operating System (OS). An operating system is a program that allows you to interact directly with all of the software and hardware on your computer by entering various commands (Haas, 2010). But which operating system should you use? Two major types of Operating Systems are UNIX and Windows. The UNIX operating system which began as early as 1969 (Duvall, 2006) is like...

Words: 1184 - Pages: 5