Bash

Simple Tshark script to dump MAC and IP address pairs to file

Blog tags: 

For the S4x19 competition, I needed a simple script to go through several hundred GB of pcaps and dump out a list of IP and MAC address pairings to prepare elements for the challenge. Here is a simple BASH script that can achieve that simply with tshark (sudo apt-get install tshark):

Printing out a string as a byte array and corresponding hexadecimal

Blog tags: 

Here is a quick script that is pretty handy!

#!/bin/bash
STR="Its a small world afterall"
CNT=$(wc -c <<< $STR})
TMP_CNT=0

printf "Char Hex\n"

while [ ${TMP_CNT} -lt $[${CNT} -1] ]; do
  printf "%-5s 0x%-2X\n" "${STR:$TMP_CNT:1}" "'${STR:$TMP_CNT:1}"
  TMP_CNT=$[$TMP_CNT+1]
done

Which will output the following:

Pure BASH to remove spaces line by line

Blog tags: 

Here is a neat little script I wrote to remove spaces in CSVs recursively line by line using only pure Bash

#!/bin/bash
INPUT_CSV="test.csv"

set IFS=,
set oldIFS = $IFS
readarray -t arry < ${INPUT_CSV}

for i in "${arry[@]}"
do
   :
        res="${i//[^ ]}"
        cnt="${#res}"
        while [ ${cnt} -gt 0 ]; do
                i=${i/, /,}
                cnt=$[$cnt-1]
        done
        echo $i
done

Simple C Math Program

Blog tags: 

While writing an exercise for my book currently in editing, I wrote a quick utility for math operations on the CLI in Bash

/**
 * @file main.c
 * @author Ron Brash
 * @date Nov 16, 2017
 * @brief Create a simple math helper for CLI usage in Bash
 *
 * @note To compile:
 *      gcc -Wall -O2 -o mhelper main.c -lm
 */
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include <math.h>

#define USAGE "%s <param1> <operation> <param2>\n"
#define TRUE 1
#define FALSE 0

// Function pointer for math operations
typedef void (*mathfpr_t) (double, double, uint8_t, uint8_t);

// Typedef of structure containing math operation function pointers
// think array of callbacks
typedef struct mathop_s {
        char op;
        mathfpr_t func;
} mathop_t;

// Forward function declarations
static void subtract(double a, double b, uint8_t a_type, uint8_t b_type);
static void addition(double a, double b, uint8_t a_type, uint8_t b_type);
static void divide(double a, double b, uint8_t a_type, uint8_t b_type);
static void multiply(double a, double b, uint8_t a_type, uint8_t b_type);
static inline int is_whole(double a);
static inline int has_decimal(char *a);

// Declared function pointer array
static mathop_t math_arr[] = { {'-', subtract}, {'+', addition}, {'*', multiply}, {'/', divide}, };

static inline int has_decimal(char *a)
{

        int len = 0;
        for (len = strlen(a); len > 0; len--) {
                if (a[len] == '.') {
                        return (TRUE);
                }
        }
        return (FALSE);
}

static inline int is_whole(double a)
{
        if (a == (int)a) {
                // true
                return (TRUE);
        }
        // false
        return (FALSE);
}

static inline void print_val(double a, int type)
{

        if (type == 0) {
                printf("%.2f\n", a);    // Only print out two decimal points
        } else {
                printf("%i\n", (int)a);
        }
}

static void subtract(double a, double b, uint8_t a_type, uint8_t b_type)
{

        double res = a - b;
        print_val(res, is_whole(res));
}

static void divide(double a, double b, uint8_t a_type, uint8_t b_type)
{

        double res = a / b;
        print_val(res, is_whole(res));
}

static void multiply(double a, double b, uint8_t a_type, uint8_t b_type)
{

        double res = a * b;
        print_val(res, is_whole(res));
}

static void addition(double a, double b, uint8_t a_type, uint8_t b_type)
{

        double res = a + b;
        print_val(res, is_whole(res));
}

static void set_param(char *param, double *val, int *type)
{

        char *tmp;
        if (has_decimal(param) > 0) {
                *val = strtof(param, NULL);
                *type = 1;
        } else {
                *val = strtoll(param, &tmp, 10);
        }

}

int main(int argc, char *argv[])
{

        /// Initialize function variables in the stack
        double a = 0, b = 0;
        int a_type = 0, b_type = 0;
        char op = '\0';

        /// There are four parameters including the binary itself on the CLI
        if (argc == 4) {

                /// Copy params to values
                strncpy(&op, argv[2], sizeof(char));

                /// Let's set &  check if it has a decimal (signify a float early)
                set_param(argv[1], &a, &a_type);
                set_param(argv[3], &b, &b_type);

                int i = 0;
                for (i = 0; i < sizeof(math_arr); i++) {
                        if (op == math_arr[i].op) {
                                math_arr[i].func(a, b, a_type, b_type);
                                return (TRUE);
                                break;
                        }
                }

        }

        printf(USAGE, argv[0]);
        return (FALSE);
}

Enjoy

Bash Scripting - Recursive MD5sum On Files In Directory

Blog tags: 

Recently, I wanted to get the MD5sums of all of the files with a specific extension in the current directory and create a hash sum file for each. This was achieved using the following Bash script.

#!/bin/sh

# Get a list of files in a directory without the .md5 extension
# Note the ticks
LIST=`find . -name "*.txt" -a ! -name '.md5'`

Remote Logging Using Syslog And Logging Shell Commands Remotely

Blog tags: 

While trying to come up with a simple keylogging solution that provides remote logging, I came across a pretty good solution of using the audit package and altering bash.

One of the problems I came across was that many of the keyloggers could not log any commands sent through a SSH connection - this does on Fedora anyways.

Edit /etc/rsyslog.conf and uncoment this line:

vi /etc/rsyslog.conf

*.* @192.168.18.1:514>/code>

<b>Note: this is where the remote server is.</b>

Install and run the following:

<code>yum install -y audispd-plugins pasacct

Pages

Subscribe to RSS - Bash