Skip to content

Instantly share code, notes, and snippets.

View rakia's full-sized avatar

Rakia Ben Sassi rakia

View GitHub Profile
@rakia
rakia / memory-safe-python-program.py
Created May 3, 2024 17:17
A memory-safe Python program
# Python handles memory management for you
list_of_items = ['item1', 'item2', 'item3']
print("Current list:", list_of_items)
# When objects like lists are no longer needed, Python cleans up
del list_of_items # This frees up memory space automatically
# The garbage collector in Python manages the allocation and deallocation of memory
@rakia
rakia / remote-control.c
Created May 3, 2024 16:51
A C program with remote control vulnerability
#include <stdio.h>
#include <string.h>
void process_command(char *input) {
char buffer[30];
strcpy(buffer, input); // Potential overflow here
// ... Process command
}
int main() {
char command[100];
@rakia
rakia / data-breaches.c
Created May 3, 2024 16:43
A C program with data breaches
#include <stdio.h>
#include <string.h>
void process_data(char *input) {
char buffer[50];
strcpy(buffer, input); // Potential overflow here
// ... Process data
}
int main() {
char data[100];
@rakia
rakia / authentication-buffer-overflow.c
Created May 3, 2024 16:36
Imagine a program that authenticates users, based on their username and password, has a buffer overflow vulnerability
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool authenticate(char *username, char *password) {
char stored_password[16];
strcpy(stored_password, "secret"); // The real password
return strcmp(password, stored_password) == 0;
}
int main() {
@rakia
rakia / non-memory-sage-c-program.c
Created May 3, 2024 16:00
A Non-Memory-Safe C program
#include <stdio.h>
#include <string.h>
void greet(char *input) {
char buffer[10];
strcpy(buffer, input); // Copy input to buffer
printf("Hello, %s!\n", buffer);
}
int main() {
char name[20];
@rakia
rakia / get-weather-wsdl-example.xml
Created April 11, 2024 20:25
SOAP API: WSDL file to use a get-weather service
<?xml version="1.0"?>
<definitions name="SimpleWeatherService"
targetNamespace="http://example.com/simpleWeather.wsdl"
xmlns:tns="http://example.com/simpleWeather.wsdl"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
@rakia
rakia / client-get-weather.py
Created April 11, 2024 13:32
Client-side logic of GetWeather ProtoBuf (gRPC) service
import grpc
import weather_pb2
import weather_pb2_grpc
def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = weather_pb2_grpc.WeatherServiceStub(channel)
response = stub.GetWeather(weather_pb2.GetWeatherRequest(city_name="New York"))
print(f"Weather: {response.temperature} degrees")
@rakia
rakia / weather-servicer.py
Created April 11, 2024 13:31
Server-side logic of GetWeather ProtoBuf (gRPC) service
from concurrent import futures
import grpc
import weather_pb2
import weather_pb2_grpc
class WeatherServicer(weather_pb2_grpc.WeatherServiceServicer):
def GetWeather(self, request, context):
# Implement the logic to retrieve the weather here. For simplicity, returning a fixed value.
return weather_pb2.GetWeatherResponse(temperature=22.5)
@rakia
rakia / get-weather.proto
Last active April 11, 2024 13:18
A ProtoBuf file defining a GetWeather service
syntax = "proto3";
package weather;
// The weather service definition.
service WeatherService {
// Sends a request to get the current weather for a city
rpc GetWeather(GetWeatherRequest) returns (GetWeatherResponse) {}
}
@rakia
rakia / extract-fields.ts
Last active December 25, 2023 12:17
ChatGPT Solution 3: extract Field Values from a text using delimiters
extractFieldValuesChatGPT(text: string, delimiters: string[]): string[] {
  const regexSafeDelimiters = delimiters.map(d => d.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
  let fieldValues = text.split(new RegExp(regexSafeDelimiters.join('|'), 'g'));
  fieldValues = fieldValues.filter(p => p);
  return fieldValues;
}