Created
May 5, 2023 13:30
-
-
Save blue-devil/4113f9c94d5926087e96173f5198bd0c to your computer and use it in GitHub Desktop.
Raw bytes dumper from commandline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* rbdumbper.c <Raw bytes dumper from commandline> | |
* Copyright (C) 2022 Blue DeviL <bluedevil.SCT@gmail.com> | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <https://www.gnu.org/licenses/>. | |
*/ | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
void usage() | |
{ | |
printf("[*] USAGE:\n"); | |
printf("\trbdumper \"\\x53\\x43\\X54\"\n"); | |
return; | |
} | |
/** | |
* @brief dump_mem: prints a memory block with a given size. | |
* | |
* @param mem_blk : pointer to a memory block | |
* @param len : byte count to be printed | |
*/ | |
void dump_mem(const void *mem_blk, unsigned long long len) | |
{ | |
char szLineFmt[] = "0x%016X\t"; | |
// char szLineFmt2[] = "0x%08X"; | |
unsigned long long i; | |
unsigned long long f32bit = 0xFFFFFFFF; | |
if (len < f32bit) | |
{ | |
strcpy(szLineFmt, "0x%08X\t"); | |
} | |
for (i = 0; i < len; i++) | |
{ | |
if (i % 8 == 0) | |
printf(" "); | |
if (i % 16 == 0) | |
{ | |
printf("\n"); | |
printf(szLineFmt, i); | |
} | |
printf("%02X ", *(unsigned char *)(mem_blk + i)); | |
} | |
printf("\n"); | |
} | |
/** | |
* @brief dumps an prints raw bytes from cmdline | |
* | |
* @param rb :raw bytes from commandline as "\x53\x43\X54" | |
* @return ssize_t : raw byte count | |
*/ | |
ssize_t raw_byte_dumper(char *rb) | |
{ | |
static ssize_t file_size = 0; | |
// lowercase the raw bytes if user uses \X instead of \x | |
char c; | |
for (int i = 0; i < strlen(rb); i++) | |
{ | |
c = rb[i]; | |
rb[i] = tolower(c); | |
} | |
file_size = strlen(rb) / 4 + 1; | |
char *buf = malloc(file_size); | |
size_t i = 0; | |
for (char *tok = strtok(rb, "\\x"); tok; tok = strtok(NULL, "\\x")) | |
{ | |
sscanf(tok, "%02hhx", buf + i); | |
i++; | |
} | |
buf[i] = '\0'; | |
dump_mem(buf, file_size); | |
// Be carefull raw bytes from commandline might not be printable | |
printf("%s", buf); | |
free(buf); | |
return file_size - 1; | |
} | |
int main(int argc, char **argv) | |
{ | |
printf("Raw bytes dumper from commandline\n\n"); | |
if (argc < 2) | |
{ | |
usage(); | |
exit(EXIT_FAILURE); | |
} | |
char *rb = argv[1]; | |
ssize_t rbc = raw_byte_dumper(rb); | |
printf("\n[+] Raw byte count : %ld", rbc); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This small code sniplet:
\x
from commandline