CUDA Basics for Hackers, Part 1: Getting Started
Welcome back, my aspiring cyberwarriors!
I think it is abundantly clear by now that the future of cybersecurity depends upon AI. This is the reason we are offering the Wittgenstein Award for the best AI cybersecurity agents. It is also the reason we are offering a series of tutorials and classes in using AI in cybersecurity.
As you know, most of the AI models run on NVIDIA GPUβs. These graphics processing units are unique in that they can process thousands of threads simultaneously (your CPU can run (4-16). These means that a GPU can do calculations on many processes at the same time, perfect for generating the graphics in your favorite game.
Artificial intelligence or large language models (LLMs) use neural networks to calculate the best answer to your prompt. Each token (~word) is chosen by the probabilities that it is the right token in the right order. This requires calculating probabilities simultaneously for thousand or even millions of tokens. This where the NVIDIA GPUβs come in.
Many years ago, NVIDIA developed a system they call CUDA or Compute Unified Device Architecture for running general purpose applications on a GPU. It is CUDA that nearly every AI developer started with and why NVIDIA is the GPU of choice. CUDA is perfect for running operations on many pieces of data simultaneously. Itβs ideally suited for tasks where the work can be broken into many independent pieces and run the calculations in parallel such as image processing, deep learning, or even password cracking.

The code of any application that executes on the GPU is referred to as device code and those functions are referred as kernels. When a application launches a kernel, it launches many threads. These threads can number as many as millions. These threads are grouped and organized into thread blocks and these thread blocks are organized into a grid. These thread blocks are all exactly the same size with the same dimensions.

Since CUDA is such a key element of artificial intelligence development, we thought you should know at the least the basics of CUDA. This is the first of a series on using CUDA.
Step #1: Getting Started
Before we begin, letβs discuss the pre-requisites.
First, you need a NVIDIA GPU with CUDA support.
Second, as with any compute intensive task or cybersecurity, in general, I strongly recommend you use Linux (see my book, Linux Basics for Hackers from No Starch Press). Ubuntu or Debian is recommended (Kali Linux is Debian).
Third, you need to install the CUDA toolkit.
kali>sudo apt install nvidia-cuda-toolkit -y
Just like creating compiled programs in Linux with gcc, you will need the CUDA compiler or nvcc. You can check to see whether it is already installed by entering:
kali > nvcc --version
If you system responds with the nvcc version, the compiler is installed and we are ready to start!
Step 2: Write the CUDA Program
NVIDIAβs CUDA C++ is an extension of standard C++ designed for using the power of the GPU. Those of you comfortable with C++ should find CUDA C++ familiar and easy to work with. For the rest of you, pay close attention.
Letβs begin by creating a new file called hackers_arise_vector_cuda.cu (note the .cu extension).
Copy this program into that file.


Step 3: Compile the Code
Use the NVIDIA CUDA compiler or nvcc:
kali > nvcc hackers_arise_vector_cuda.cu -o myfirstcuda.cu
If you donβt see an error, your program has compiled successfully and is ready to run as myfirstcuda.cu (all CUDA programs use the .cu extension).
Step 4: Run the Program
Next, letβs run this simple CUDA program.
kali > ,/myfirstcuda.cu
Your output should look something this.

Step 4: What Happened?
Now, letβs look at what happened in our program.
In the first line, the code includes the C++ iostream that runs on the CPU providing standard in and standard out. In the second line, the code includes cuda_runtime.h which is the primary header file for the CUDA Runtime API.

Next, we create a CUDA kernel function that can run on a GPU. the __global__ indicates that this function can be installed from the CPU and run in the GPU.

Our program then calculates the global thread ID.

Next, after boundary checking, we generate the array size and then allocate memory on the host CPU.

Then, we initialize the host (CPU) arrays with sample data.

Next, we allocate memory on the device or GPU.

Then we copy the data from the host CPU to the device, GPU.

Next, we define the execution configuration (threads and blocks). In this case, we are designating 256 threads per block and then calculate how many blocks we need.

Next, we launch the kernel on the GPU using multiple threads

We need to wait for the GPU to complete itβs work before trying to access on the CPU.
Then we copy the results from the GPU to the CPU.

Next we verify the results by sending the first 5 values to standard out.

Finally, before we leave, we need free up the memory.

Summary
CUDA or Computer Unified Device Architecture is a framework developed by NVIDIA to fully utilize the strength of GPUβs. It use an extended C++ (as well as Python) to convert the problem in to a multi-threaded application. It is this framework that makes NVIDIA GPUβs the first choice for AI developers.
CUDA can also be used for many applications in cybersecurity including:
- Password cracking and brute-force attacks
- Real-time threat detection
- Phishing and Malware Analysis
- Post-Quantum Cryptography
- Network Graph Analysis.
As this world transitions to artificial intelligence, stick to Hackers-Arise for the latest developments and techniques including the awarding of the Wittgenstein Award in 2027 for the best cybersecurity AI agents.
The post CUDA Basics for Hackers, Part 1: Getting Started first appeared on Hackers Arise.














