C++

2 min readArticle

C++ is a compiled, statically-typed systems programming language that extends C with object-oriented features, templates, and the STL (Standard Template Library). It's used heavily in performance-critical software — game engines, operating system components, embedded systems, security tools, and malware.

Why Relevant for Security

  • Understanding C++ helps you reverse engineer compiled binaries
  • A lot of Windows internals and malware is written in C++
  • Exploit development and shellcode sometimes involves C++ patterns
  • Many network security tools (Wireshark, Nmap internals, etc.) are C++

Compilation

bash
# Compile a simple program
g++ -o myprogram main.cpp

# With debugging symbols (useful for reversing practice)
g++ -g -o myprogram main.cpp

# Enable all warnings
g++ -Wall -Wextra -o myprogram main.cpp

# Optimize
g++ -O2 -o myprogram main.cpp

Key Concepts

cpp
#include <iostream>
#include <string>
#include <vector>

// Classes
class NetworkScanner {
private:
    std::string target;
    int port;
public:
    NetworkScanner(std::string t, int p) : target(t), port(p) {}
    void scan();
};

// Pointers (important for understanding exploits)
int x = 42;
int* ptr = &x;   // ptr holds address of x
*ptr = 99;       // dereference to change value

// References
int& ref = x;    // alias for x

// Memory management (heap)
int* arr = new int[100];
delete[] arr;    // must free or leak

STL Basics

cpp
#include <vector>
#include <map>
#include <string>

std::vector<int> nums = {1, 2, 3, 4, 5};
nums.push_back(6);

std::map<std::string, int> ports;
ports["http"] = 80;
ports["https"] = 443;
ports["ssh"] = 22;

Buffer Overflows (Security Context)

C++ inherits C's memory unsafety. Classic buffer overflow:

cpp
char buf[64];
strcpy(buf, user_input);  // dangerous if input > 64 bytes
gets(buf);                // never use gets()

Use safe alternatives: strncpy(), snprintf(), or better yet C++ std::string.

Building Network Tools

cpp
#include <sys/socket.h>
#include <arpa/inet.h>

int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(80);
inet_pton(AF_INET, "192.168.1.1", &server.sin_addr);
connect(sock, (struct sockaddr*)&server, sizeof(server));

Resources

  • cppreference.com — the definitive reference
  • Learn C++ (learncpp.com) — excellent free tutorial
  • "The C++ Programming Language" by Stroustrup

See Also

  • c-programming-language-guide - C# notes
  • assembly-language-guide - Lower level
techzonesite.comUnlock Your IT Potential