0% found this document useful (0 votes)
72 views

karthik_bip38_decrypt.py

This document is a Python script that uses OpenCL to perform a brute-force attack on passwords with a maximum length of 6 characters using a specified character set. It includes functions to set up the OpenCL environment, compile the kernel, and execute the brute-force operation on a GPU. The results of the brute-force attempt are printed after execution, showing the time taken and the first few results.

Uploaded by

vicky9879871
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

karthik_bip38_decrypt.py

This document is a Python script that uses OpenCL to perform a brute-force attack on passwords with a maximum length of 6 characters using a specified character set. It includes functions to set up the OpenCL environment, compile the kernel, and execute the brute-force operation on a GPU. The results of the brute-force attempt are printed after execution, showing the time taken and the first few results.

Uploaded by

vicky9879871
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pyopencl as cl

import numpy as np
import time

# Constants
CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
MAX_LENGTH = 6 # Maximum length of the password to crack
GLOBAL_WORK_SIZE = 1024 # Work size per kernel launch (can be tuned based on your
GPU)
LOCAL_WORK_SIZE = 256 # Local work size (also can be tuned)

# Function to find the best OpenCL device (GPU)


def get_opencl_device():
platforms = cl.get_platforms()
for platform in platforms:
devices = platform.get_devices(device_type=cl.device_type.GPU)
if devices:
return devices[0]
raise RuntimeError("No OpenCL GPU device found")

# Set up OpenCL context and queue


def setup_opencl():
device = get_opencl_device()
context = cl.Context([device])
queue = cl.CommandQueue(context, device=device)
return context, queue, device

# OpenCL kernel code for brute-forcing


opencl_code = """
__kernel void brute_force(__global const uchar *charset, __global int *results,
const int max_len) {
int id = get_global_id(0); // Get the global thread id
int len = max_len;
if (id < len) {
int idx = id % 62;
results[id] = charset[idx];
}
}
"""

# Compile the OpenCL program


def compile_opencl_program(context, code):
program = cl.Program(context, code).build()
return program

# Main function for brute-forcing using OpenCL


def brute_force_gpu():
# Set up OpenCL
context, queue, device = setup_opencl()
program = compile_opencl_program(context, opencl_code)

# Charset as bytes (ASCII)


charset = np.array([ord(c) for c in CHARSET], dtype=np.uint8)

# Allocate memory for results


results = np.zeros(GLOBAL_WORK_SIZE, dtype=np.int32)
results_buffer = cl.Buffer(context, cl.mem_flags.WRITE_ONLY, results.nbytes)

# Set up the kernel and launch the OpenCL code


kernel = program.brute_force
kernel.set_arg(0, charset)
kernel.set_arg(1, results_buffer)
kernel.set_arg(2, np.int32(MAX_LENGTH))

# Execute the kernel


global_work_size = (GLOBAL_WORK_SIZE,)
local_work_size = (LOCAL_WORK_SIZE,)
start_time = time.time()

# Launch the kernel to start brute-forcing


cl.enqueue_nd_range_kernel(queue, kernel, global_work_size,
local_work_size).wait()

# Retrieve the results


cl.enqueue_copy(queue, results, results_buffer).wait()
print(f"Brute-force finished in {time.time() - start_time:.2f} seconds")
print(f"Results: {results[:10]}") # Print first few results for verification

# Main entry point


if __name__ == "__main__":
brute_force_gpu()

You might also like