ADS and Local staging

This commit is contained in:
lsecqt
2025-01-11 20:22:07 +02:00
parent d325801aeb
commit 3a074389dc
3 changed files with 161 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib") // Link with Winsock library
// Function to get the IP address
char* getIPAddress() {
WSADATA wsaData;
static char ipstr[INET_ADDRSTRLEN]; // Static to ensure it persists after the function exits
char hostname[256];
struct addrinfo hints, * res, * p;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
return NULL;
}
// Get the hostname of the machine
if (gethostname(hostname, sizeof(hostname)) != 0) {
WSACleanup();
return NULL;
}
// Set up hints for getaddrinfo
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET; // IPv4
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Get address information
if (getaddrinfo(hostname, NULL, &hints, &res) != 0) {
WSACleanup();
return NULL;
}
// Loop through the results and get the first IPv4 address
for (p = res; p != NULL; p = p->ai_next) {
struct sockaddr_in* addr = (struct sockaddr_in*)p->ai_addr;
inet_ntop(AF_INET, &addr->sin_addr, ipstr, sizeof(ipstr));
break; // Stop after the first address
}
// Free the address information and cleanup
freeaddrinfo(res);
WSACleanup();
return ipstr;
}
int main() {
char* ipAddress = getIPAddress();
if (ipAddress) {
printf("%s\n", ipAddress);
}
else {
printf("Failed to retrieve IP address.\n");
}
return 0;
}
+59
View File
@@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void writeToADS(const char* fileName, const char* streamName, const char* data) {
char fullPath[MAX_PATH];
snprintf(fullPath, sizeof(fullPath), "%s:%s", fileName, streamName);
FILE* file = fopen(fullPath, "wb");
if (file == NULL) {
fprintf(stderr, "Error opening stream for writing.\n");
return;
}
fwrite(data, sizeof(char), strlen(data), file);
fclose(file);
printf("Data written to stream '%s' successfully.\n", streamName);
}
void readFromADS(const char* fileName, const char* streamName) {
char fullPath[MAX_PATH];
snprintf(fullPath, sizeof(fullPath), "%s:%s", fileName, streamName);
FILE* file = fopen(fullPath, "rb");
if (file == NULL) {
fprintf(stderr, "Error opening stream for reading.\n");
return;
}
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
char* buffer = (char*)malloc(size + 1);
if (buffer == NULL) {
fprintf(stderr, "Memory allocation error.\n");
fclose(file);
return;
}
fread(buffer, sizeof(char), size, file);
buffer[size] = '\0'; // Null-terminate the string
fclose(file);
printf("Data read from stream '%s':\n%s\n", streamName, buffer);
free(buffer);
}
int main() {
const char* fileName = "example.txt";
const char* streamName = "myStream";
const char* data = "This is some data stored in an alternate data stream.";
writeToADS(fileName, streamName, data);
readFromADS(fileName, streamName);
return 0;
}
+42
View File
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
// Global variables
unsigned char* buf = NULL; // Buffer to store file content
DWORD size = 0; // Variable to store file size
// Function to read binary file
void readBin(const char* fileName) {
FILE* file;
file = fopen(fileName, "rb");
if (file == NULL) {
fprintf(stderr, "Error opening file.\n");
return;
}
fseek(file, 0, SEEK_END); // Move to end of file
size = ftell(file); // Get file size
fseek(file, 0, SEEK_SET); // Move back to start of file
buf = (unsigned char*)malloc(size * sizeof(char)); // Allocate memory for file content
if (buf == NULL) {
fprintf(stderr, "Memory allocation error.\n");
fclose(file);
return;
}
fread(buf, sizeof(char), size, file); // Read file content into buffer
fclose(file);
printf("File read successfully. Size: %lu bytes.\n", size);
}
int main() {
const char* fileName = "example.bin"; // Specify the binary file to read
readBin(fileName); // Call the function to read the file
return 0;
}