220 Programming in C
220 Programming in C
S e c t i o n 0 2 | M o d u l e 0 2
© Caendra Inc. 2019
All Rights Reserved
Table of Contents
• C++ IDE
• C++ fundamentals: Variables, functions and basic
code constructs
C++ IDE
You can find all the C++ code samples used on the Resources drop-down
menu of this module.
To do that, click on
File>New>Source
File.
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.7
2.1 C++ IDE
Structure of C++
Programs
In this chapter, you will see what a C++ program looks like.
#include <iostream>
int main ()
{
instructions here
}
return 0;
If the program is
successfully compiled we
should see a window like
the following; this window
tells us that the compiler
is done and that no errors
or warnings were
generated.
You should now be able to run the program and see the
console output.
Now that we are more confident about the IDE, let’s see
how we can define variables with different types.
short
Short integer (2 bytes)
short int
There are many int Integer (4 bytes)
different data long
Long integer (4 bytes)
long int
types we can use. bool Boolean (1 byte)
float Floating point number (4 bytes)
In this case, the second cout prints the following string: Value of
global_variable: 2
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.40
2.3 Variables & Types
If we use global_variable in
another function, its value will be
4, no matter if we have already
Local Scope
executed the instruction
“int global_variable = 2;”
Input / Output
cout << "The value of variable sum is: " << sum << endl;
cout << "The value of variable sum is: " << sum << endl;
The << operator tells the program to insert the next data
into the stream. Since we use cout, it puts the data in the
standard output stream. This operator can be used multiple
times, and it is useful when we want to print a combination
of strings and variables. In this case, we want the string
“The value of variable sum is:” followed by the value of the
variable sum.
cout << "The value of variable sum is: " << sum << endl;
cout << "The value of variable sum is: " << sum << endl;
Operators
variable_name = expression;
variable_name = expression;
In C++ literature, you will see these two terms: lvalue and
rvalue.
• lvalue is any label that appears on the left side of an
assignment statement; in other words, we can say it is the
variable name.
• rvalue refers to expressions/value on the right side of an
assignment and simply means the value that will be
assigned to the variable.
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.56
2.5 Operators
b = variable1;
Operator Action
- Subtraction
+ Addition
* Multiplication
/ Division
% Modulus
x = 10; x = 10;
y = ++x; Set y to 11 y = x++;
Set y to 10 and x to 11
Operator Action
Relational
> Greater than
>= Greater than or equal
Operator Action
< Less than Logical
<= Less than or equal && AND
== Equal || OR
For this purpose, in C++ we can use the bool data type and
the Boolean constants true and false. So a 0 value
automatically converts to false while a non-zero value
automatically converts to true.
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.62
2.5 Operators
Operation Value
Relational operators are used ( 10 > 1 ) True
in order to evaluate a ( 10 >= 10 ) True
comparison between two ( 10 < 5 ) False
expressions. The result is a ( 5 <= 10 ) True
Boolean value. ( 1 == 1 ) True
( 1 != 1 ) False
The first line may be read as the following: when x is false (0) and y is false (0) the result
of x AND y (x && y) is false (0), the result of x OR y is false, and the inverse of x is true
The logical operator ! has only one operand (at its right) and
it inverses this value (false if its operand is true, and true if
its operand is false). The logical operator && and || evaluate
two expressions in order to obtain a relational result.
• && (AND) results true if both operands are true and
false otherwise
• || (OR) results true if either one of its operands is true,
false when both are false
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.65
2.5 Operators
Bitwise AND
11001110 & 206 &
10011000 = 152 =
10001000 136
• if • while • break
• switch • for • continue
• do-while • goto
• return
if (expression)
statement;
else
statement;
if (expression)
statement;
If the expression evaluates to
else true, the statement or block of
statement;
statements that form the target
of if is executed; otherwise, the statement or block that is
the target of else will be executed.
The following
example shows a
menu selection.
This simple
program displays a
menu, gets the
user input and calls
the proper
procedures.
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.78
2.6 Iteration & Conditional Structures
ITERATION
Where:
• initialization is an assignment statement that sets the starting
value of the loop control variable
• condition determines when the loop must end
• increment defines how the control variable changes for each
iteration
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.80
2.6 Iteration & Conditional Structures
Start with a = 2
The for loop continues as long as the condition is true. Once the
condition fails, the program executes the statement right after
the for. In the above program, the for loop is used to print all the
multiples of 2 until 50 is reached.
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.81
2.6 Iteration & Conditional Structures
As shown in the code above, an infinite loop used with a break statement can be
useful to keep the console alive until the user chooses to exit. The program
terminates only if the user inserts the number 2; otherwise, they will see the menu
over and over again.
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.84
2.6 Iteration & Conditional Structures
The second loop available in C++ is the while loop. Its general
form is:
while(condition) {
statement;
}
Where:
• statement is either an empty statement, a single
statement, or a block of statements
• condition may be any expression, and true is any non-zero
value
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.87
2.6 Iteration & Conditional Structures
while(condition) {
The loop continues while the statement;
condition evaluates to true. }
Unlike for and while loops, which test the condition at the
beginning of the loop, the do-while loop checks its
condition at the end of the loop; a do-while loop always
executes at least once.
You have all the skills needed to write a program that prints
out a simple Xmas tree. Given a number, write a program
that will display a triangle made of * chars, which has as
many lines as the number provided.
*
Let’s type in 5; the program ***
should print something like this: *****
*******
*********
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.93
2.6 Iteration & Conditional Structures
JUMP STATEMENT
return expression;
Pointers
type *name;
Where:
• type is the base type of the pointer (int, char…);
• name is the name (identifier) of the pointer variable
Type defines the type of variable the pointer can point to.
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.107
2.7 Pointers
There are two special pointer operators: * and &. The &
returns the memory address of the variable. For example:
x = &y;
Arrays
type var_name[size];
Where:
• type declares the type of the array (the type of each element in the
array)
• size defines the length of the array (how many elements the array can
contain)
Functions
Where:
• type specifies the type of data that the function returns
• function_name is the identifier used to call that function
• parameters is a comma-separated list of variables and their
associated types. Those variables receive the values when the
function is called.
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.120
2.9 Functions
type function_name(){
statements;
}
Function definition
Here is an example of a
function that takes two
numbers from the user,
sums them and returns
the result of the Function call
operation.
Formal parameters
a = 5;
The program will then b = 3;
ask the user to insert two
values: a and b.
x = 5; y = 3;
The execution now occurs
in the sum function. z = x + y;
z = 5 + 3;
Here a new variable is z = 8;
declared and the operation
z = x + y is executed.
z = 8;
return(z);
Since the function result = 8
returned a value (z), this
will be copied in the
variable result: the value
of the result will then be 8.
By value By reference
By default, C++ uses call by value; this means that the code
in the function does not alter the arguments used by the
caller.
Hera Lab
C++-assisted exploitation
C++-assisted exploitation
In the lab, you will:
• Create a simple keylogger
program
• Create a simple data stealing
program
• Exfiltrate stolen data via a
network connection
*Labs are only available in Full or Elite Editions of the course. To upgrade, click HERE. To access, go to the course
in your members area and click the labs drop-down in the appropriate module line or to the virtual labs tabs on
the left navigation.
PTSv4: Section 2, Module 2 - Caendra Inc. © 2019 | p.140
References
C++ tutorial
http://www.cplusplus.com/doc/tutorial/
*Labs are only available in Full or Elite Editions of the course. To upgrade, click HERE. To access, go to the course in your
members area and click the labs drop-down in the appropriate module line or to the virtual labs tabs on the left navigation.