alias uncomment='_(){ if (($#<1))||[[ "${1}" =~ ^- || ! -e "${1}" ]]; then (($#>0))&&[[ ! "${1}" =~ ^- ]]&&[[ ! -e "$1" ]]&&echo "*** Error: File (\"$1\") not found.";echo "Usage: uncomment <file>"; return 1;fi;grep -iP "^([ \t\r\f]|[^#])*[^# \h\t\f\r]+" "$1";};_'
Tuesday, December 24, 2019
Uncomment - Quickly check config files
Monday, November 11, 2019
Custom File Colorer - In AWK
#!/usr/bin/awk -f
######################################################################
######################################################################
## Author: Adam Michael Danischewski
## GitHub: https://github.com/AdamDanischewski/scriptsandoneliners
## Created Date: 2019-11-12
## Name: colorfiles.awk
## Version: v0.00
## Last Modified: 2019-11-12
## Issues: If you find any issues emai1 me at <my first name> (dot)
## <my last name> (at) gmail (dot) com.
##
## Requirements: awk
##
## Allows the user to fine tune and customize their color scheme for
## files. LS_COLORS only allows for a small subset of file types. This
## script allows specific colors for all files and can be utilized in
## pipelines to color output after processing with tools that do not
## have a color option.
##
## This script includes a color template that can be applied to
## pipelines on the command line or to replace coloring features
## of shell utilities. Edit/hack to your preference.
##
## 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.
##
## Released under Creative Commons License: CC BY-SA 4.0
## https://creativecommons.org/licenses/by-sa/4.0/
######################################################################
######################################################################
function init_colormap() {
colormap["pdf"]="108;107;21"
startcolor="\033[38;2;" ## TrueColor RGB triplet prefix
endcolor="\033[0m"
colormap["jpg"]="0;85;0"
colormap["gif"]=colormap["jpg"] ## Grouping images, change as you wish.
colormap["png"]=colormap["jpg"]
colormap["jpeg"]=colormap["jpg"]
colormap["jpe"]=colormap["jpg"] ## JFIF
colormap["blend"]="251;143;21"
colormap["mp3"]="157;0;193"
colormap["bsh"]="85;229;0"
colormap["awk"]=colormap["bsh"]
colormap["php"]=colormap["bsh"]
colormap["js"]=colormap["bsh"]
colormap["pl"]=colormap["bsh"]
colormap["py"]=colormap["bsh"]
colormap["vtt"]="0;85;157"
colormap["tex"]="0;0;229"
colormap["nzb"]="85;229;0"
colormap["deb"]="193;12;199"
colormap["txt"]="215;215;21"
colormap["mp4"]="85;0;193"
colormap["log"]="193;85;12"
colormap["c"]="0;121;229"
colormap["cc"]=colormap["c"]
colormap["cpp"]=colormap["c"]
colormap["h"]=colormap["c"]
colormap["avi"]=colormap["mp4"]
colormap["mov"]=colormap["mp4"]
colormap["mkv"]=colormap["mp4"]
colormap["webm"]=colormap["mp4"]
colormap["html"]="157;0;193"
colormap["css"]=colormap["html"]
colormap["pdf"]="229;85;0"
colormap["dir"]="0;157;229" ## Directories - ending in /
colormap["links"]="251;22;143" ## Symbolic links - ending in @
colormap["part"]="157;0;85"
colormap["tar"]="229;0;193"
colormap["tar.gz"]=colormap["tar"]
colormap["tgz"]=colormap["tar"]
colormap["tar.xz"]=colormap["tar"]
colormap["tar.bz2"]=colormap["tar"]
colormap["xz"]="229;0;85"
colormap["gz"]=colormap["xz"]
colormap["7z"]=colormap["xz"]
colormap["iso"]=colormap["xz"]
colormap["rar"]=colormap["xz"]
colormap["par2"]=colormap["xz"]
colormap["bz2"]=colormap["xz"]
colormap["zip"]=colormap["xz"]
colormap["kgb"]=colormap["xz"] # Future standard compression (PAQ)
return
}
function wrap_color(s) {
if (tolower(s) ~ /\.jpg[*]?$/) {
selc=colormap["jpg"]
} else if (tolower(s) ~ /\.jpe[*]?$/) {
selc=colormap["jpe"]
} else if (tolower(s) ~ /\.gif[*]?$/) {
selc=colormap["gif"]
} else if (tolower(s) ~ /\.tar[*]?$/) {
selc=colormap["tar"]
} else if (tolower(s) ~ /\.tar\.gz[*]?$/) {
## Make sure these are before their respective
## compression conditionals to avoid tar's
## from being treated as simply compressed.
selc=colormap["tar.gz"]
} else if (tolower(s) ~ /\.tar.bz2[*]?$/) {
selc=colormap["tar.bz2"]
} else if (tolower(s) ~ /\.tar.xz[*]?$/) {
selc=colormap["tar.xz"]
} else if (tolower(s) ~ /\.tgz[*]?$/) {
selc=colormap["tgz"]
} else if (tolower(s) ~ /\.jpeg[*]?$/) {
selc=colormap["jpeg"]
} else if (tolower(s) ~ /\.png[*]?$/) {
selc=colormap["png"]
} else if (tolower(s) ~ /\.mp4[*]?$/) {
selc=colormap["mp4"]
} else if (tolower(s) ~ /\.rar[*]?$/) {
selc=colormap["rar"]
} else if (tolower(s) ~ /\.par2[*]?$/) {
selc=colormap["par2"]
} else if (tolower(s) ~ /\.log[*]?$/) {
selc=colormap["log"]
} else if (tolower(s) ~ /\.mp3[*]?$/) {
selc=colormap["mp3"]
} else if (tolower(s) ~ /\.deb[*]?$/) {
selc=colormap["deb"]
} else if (tolower(s) ~ /\.mov[*]?$/) {
selc=colormap["mov"]
} else if (tolower(s) ~ /\.bsh[*]?$/) {
selc=colormap["bsh"]
} else if (tolower(s) ~ /\.mkv[*]?$/) {
selc=colormap["mkv"]
} else if (tolower(s) ~ /\.pdf[*]?$/) {
selc=colormap["pdf"]
} else if (tolower(s) ~ /\.c[*]?$/) {
selc=colormap["c"]
} else if (tolower(s) ~ /\.cpp[*]?$/) {
selc=colormap["cpp"]
} else if (tolower(s) ~ /\.cc[*]?$/) {
selc=colormap["cc"]
} else if (tolower(s) ~ /\.h[*]?$/) {
selc=colormap["h"]
} else if (tolower(s) ~ /\.txt[*]?$/) {
selc=colormap["txt"]
} else if (tolower(s) ~ /\.vtt[*]?$/) {
selc=colormap["vtt"]
} else if (tolower(s) ~ /\.tex[*]?$/) {
selc=colormap["tex"]
} else if (tolower(s) ~ /\.nzb[*]?$/) {
selc=colormap["nzb"]
} else if (tolower(s) ~ /\.bz2[*]?$/) {
selc=colormap["bz2"]
} else if (tolower(s) ~ /\.iso[*]?$/) {
selc=colormap["iso"]
} else if (tolower(s) ~ /\.html[*]?$/) {
selc=colormap["html"]
} else if (tolower(s) ~ /\.css[*]?$/) {
selc=colormap["css"]
} else if (tolower(s) ~ /@$/) {
selc=colormap["links"]
} else if (tolower(s) ~ /\.webm[*]?$/) {
selc=colormap["webm"]
} else if (tolower(s) ~ /\.awk[*]?$/) {
selc=colormap["awk"]
} else if (tolower(s) ~ /\.php[*]?$/) {
selc=colormap["php"]
} else if (tolower(s) ~ /\.js[*]?$/) {
selc=colormap["js"]
} else if (tolower(s) ~ /\.pl[*]?$/) {
selc=colormap["pl"]
} else if (tolower(s) ~ /\.py[*]?$/) {
selc=colormap["py"]
} else if (tolower(s) ~ /\.part[*]?$/) {
selc=colormap["part"]
} else if (tolower(s) ~ /\.kgb[*]?$/) {
selc=colormap["kgb"]
} else if (tolower(s) ~ /\.xz[*]?$/) {
selc=colormap["xz"]
} else if (tolower(s) ~ /\.7z[*]?$/) {
selc=colormap["7z"]
} else if (tolower(s) ~ /\.gz[*]?$/) {
selc=colormap["gz"]
} else if (tolower(s) ~ /\.zip[*]?$/) {
selc=colormap["zip"]
} else if (tolower(s) ~ /\/$/) {
selc=colormap["dir"]
}
else {
return s
}
return (startcolor selc "m" s endcolor)
}
function usage() {
usage_str=("\nUsage: /bin/ls -f1 | colorfiles.awk \n\n \
Options: \n\
-? Help.\n\n\
Note: Make sure colors are not already present- you can sed the input.\n\
Eg. $> ls --color| sed 's/\\x1B[[0-9;]\\+[A-Za-z]//g' |colorfiles.awk\n\
$> alias colorfiles='sed \"s/\x1B[[0-9;]\\+[A-Za-z]//g\"|colorfiles.awk'\n\
$> alias lsd='find \"$(pwd)\" -maxdepth 1 -type d -printf \"%T@\t%Tc %f/\\n\" 2>/dev/null|sort -n|cut -f 2-|tail -50|colorfiles'\n\
$> alias lsf='find \"$(pwd)\" -maxdepth 1 -type f -printf \"%T@\t%Tc %f\\n\" 2>/dev/null|sort -n|cut -f 2-|tail -50|colorfiles'\n\
$> alias ls='/bin/ls -F -f1|colorfiles'\n")
return usage_str
}
BEGIN {if(v_opt1=="-?"){print usage();exit 1;}else{init_colormap();};}
## Main
{
print wrap_color($0)
}
Get the latest on GitHub.
Friday, November 8, 2019
Bash Implementation of B.R. Heaps' Permutation Algorithm
#!/bin/bash
######################################################################
######################################################################
## Author: Adam Michael Danischewski
## GitHub: https://github.com/AdamDanischewski/hpermutate
## Created Date: 2019-11-08
## Name: hpermutate.bsh
## Version: v0.00
## Last Modified: 2019-11-08
## Issues: If you find any issues emai1 me at <my first name> (dot)
## <my last name> (at) gmail (dot) com.
##
## This is a Bash implementation of Heap's algorithm which generates all
## possible permutations of n objects, as first proposed by B. R. Heap
## in 1963. Speed is reasonable for a recursive shell script.
##
## 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.
##
## Released under Creative Commons License: CC BY-SA 4.0
## https://creativecommons.org/licenses/by-sa/4.0/
######################################################################
######################################################################
_=$BASH_ARGC
OLDIFS=$IFS
declare arr=""
declare -i length=0
## Arg 1 - index 1: First swap index
## Arg 2 - index 2: Second swap index
function swap() {
local -a my_array
local tmpval=""
local -i index1=${1}
local -i index2=${2}
OLDIFS=$IFS
IFS=" " read -ra my_array <<< "${arr}"
tmpval=${my_array[${index1}]}
my_array[${index1}]=${my_array[${index2}]}
my_array[${index2}]=${tmpval}
IFS=$OLDIFS
arr="${my_array[@]}"
}
## Arg1 - Integer: size
function heappermutation() {
local -i size=${1}
local -i i=0
if((${size}==1)); then
echo "${arr}"
return
fi
for((i=0;i<${size};i++)) {
heappermutation $((${size}-1))
if(((${size}%2)==1)); then
swap 0 $((${size}-1))
else
swap ${i} $((${size}-1))
fi
}
}
function init() {
arr="${BASH_ARGV[@]}"
length=${BASH_ARGC}
}
function main() {
init
heappermutation ${length}
}
if(($#<1))||[[ "${1}" =~ ^- ]]; then
cat<<EOF
Usage: ${0##*/} <args=eg. 1 2 3 4 5>
Eg. $> ${0##*/} 1 2 3
3 2 1
2 3 1
1 3 2
3 1 2
2 1 3
1 2 3
EOF
exit 1
fi
main
exit 0
Wednesday, November 6, 2019
tarf - Pull Selected Files From a Tarball
alias tarf='_(){ (($#<1))||[[ "${1}" =~ ^- ]]&&echo "Usage: tarf <wildcardname eg.*ocean*pdf> <tarfile> <output dir-default=.>"&&return 1;local wildname="${1}";local tar="${2}";local location="${3:-.}";tar -xf "${tar}" -C "${location}" --wildcards "${wildname}" --transform "s,.*/,,";};_'
This alias makes it really easy to grab individual files from a tarball to the current [or specified] directory. Without having to dig through the directory structure or even knowing the exact name of the file. E.g. tarf '*mp4' media.tar.gz ~/mymp4s/; Get the latest on GitHub.
Sunday, November 3, 2019
alx driver - Reenable Wake On Lan
If you use the Linux Atheros driver alx you will very likely find that
Wake on Lan (wol) has been disabled since approximately 2013. When the
author was unable to find fixes for various wake-up issues including the
famous "wakeup twice" problem. This resulted in the author disabling wol
in alx. It turned out that the problem was not in alx at all yet instead
was within buggy firmware on the cards themselves.
Check your current alx driver:
$> sudo ethtool <ethdev,eg.enp3s0>
If you see something along the lines of:
Supports Wake-on: d
Wake-on: d
Then your alx has wol disabled.
You can find the history here:
https://bugzilla.kernel.org/show_bug.cgi?id=61651
The patch that disabled wol on alx was eventually reversed and found to
work perfectly. You can install the latest patch (supporting stable
kernel versions 5+) here: https://bugzilla.kernel.org/attachment.cgi?id=284877
(Right-click and choose Save Link As)
After you download it copy it to /usr/src, explode it and run setup:
$> sudo cp alx-dmks-installer-kernel5.tar.gz /usr/src
$> sudo tar xvf alx-dmks-installer-kernel5.tar.gz
$> cd alx-dmks-installer/
$> sudo ./setup
If all went well you should see a message along the lines of
DKMS: install completed.
Now install the newly patched alx driver, installed via dkms:
$> sudo modprobe -r alx && sudo modprobe alx
Note: in my testing, the above works remotely, without dropping your
connection!
Re-check your alx driver for wol support:
$> sudo ethtool <ethdev,eg.enp3s0>
You should now see something along the lines of:
Supports Wake-on: pg
Wake-on: g
Make sure to check that you don't have any scripts that unload the alx
driver when going to sleep, I found that I had one on my Ubuntu 19.04
system living in /usr/lib/pm-utils/sleep.d/50unload_alx
Remove any you find.
Now you can test, find your mac address and write it down:
$> ifconfig -a <ethdev,eg.enp3s0> | grep -o 'ether [^ ]*'
On your router turn on port forwarding for UDP Port 9 (Discard Protocol),
to the broadcast address (eg. 192.168.1.255).
Put your computer to sleep:
$> sudo pm-suspend
Now from another computer on the same LAN send a magic packet to the
sleeping computers MAC address on port 9 and voila! It will awaken.
Wake on Lan (wol) has been disabled since approximately 2013. When the
author was unable to find fixes for various wake-up issues including the
famous "wakeup twice" problem. This resulted in the author disabling wol
in alx. It turned out that the problem was not in alx at all yet instead
was within buggy firmware on the cards themselves.
Check your current alx driver:
$> sudo ethtool <ethdev,eg.enp3s0>
If you see something along the lines of:
Supports Wake-on: d
Wake-on: d
Then your alx has wol disabled.
You can find the history here:
https://bugzilla.kernel.org/show_bug.cgi?id=61651
The patch that disabled wol on alx was eventually reversed and found to
work perfectly. You can install the latest patch (supporting stable
kernel versions 5+) here: https://bugzilla.kernel.org/attachment.cgi?id=284877
(Right-click and choose Save Link As)
After you download it copy it to /usr/src, explode it and run setup:
$> sudo cp alx-dmks-installer-kernel5.tar.gz /usr/src
$> sudo tar xvf alx-dmks-installer-kernel5.tar.gz
$> cd alx-dmks-installer/
$> sudo ./setup
If all went well you should see a message along the lines of
DKMS: install completed.
Now install the newly patched alx driver, installed via dkms:
$> sudo modprobe -r alx && sudo modprobe alx
Note: in my testing, the above works remotely, without dropping your
connection!
Re-check your alx driver for wol support:
$> sudo ethtool <ethdev,eg.enp3s0>
You should now see something along the lines of:
Supports Wake-on: pg
Wake-on: g
Make sure to check that you don't have any scripts that unload the alx
driver when going to sleep, I found that I had one on my Ubuntu 19.04
system living in /usr/lib/pm-utils/sleep.d/50unload_alx
Remove any you find.
Now you can test, find your mac address and write it down:
$> ifconfig -a <ethdev,eg.enp3s0> | grep -o 'ether [^ ]*'
On your router turn on port forwarding for UDP Port 9 (Discard Protocol),
to the broadcast address (eg. 192.168.1.255).
Put your computer to sleep:
$> sudo pm-suspend
Now from another computer on the same LAN send a magic packet to the
sleeping computers MAC address on port 9 and voila! It will awaken.
Tuesday, October 29, 2019
lsvideo - List Video Card/Driver Info
alias lsvideo='sudo $(type -P lspci) -vvvv -d "$($(type -P lspci) -nn|sed -n "/VGA/p"|$(type -P grep) -oP "(?<=\[)[[:alnum:]]{4}:[[:alnum:]]{4}(?=\])")"'
Saturday, October 26, 2019
exclude - Excludes Match & Optionally N-before / N-After - in Awk
alias exclude='_(){ local match="${1}";local lnb="${2}";local lnf="${3}"; (($#<1))||[[ "${1}" =~ ^- ]]&&echo "Usage: exclude <match> [<lines back [0-9],def=0> <lines forward [0-9],def=0>]"&&return 1;awk -vmtch="${match}" -vlnb=${lnb} -vlnf=${lnf} "BEGIN{i=0;skip=0}{if(skip==0 && \$0 ~ mtch){for(j=0;j<lnb&&i>0;j++)delete arr[--i];skip=lnf;}else{if(skip==0)arr[i++]=\$0;else skip--;}}END{for(i=0;i<length(arr);i++)print arr[i];}";};_'
Friday, October 25, 2019
g2nasm - GAS ASM to NASM
Why didn't anyone else think to do this over the last 20+ years? I have no idea, but here it is. It's a Work In Progress, it's far from done but it's a decent start.
Get the latest on GitHub.
#!/bin/bash
######################################################################
######################################################################
## Author: Adam Michael Danischewski
## GitHub: https://github.com/AdamDanischewski/g2nasm
## Created Date: 2019-10-24
## Name: g2nasm.bsh
## Version: v0.01
## Last Modified: 2019-10-24
## Issues: If you find any issues emai1 me at <my first name> (dot)
## <my last name> (at) gmail (dot) com.
##
## Requirements: nasm, xxd, gnu awk, gnu sed
##
## This script takes gas asm generated by gcc and converts it to nasm.
## Eg. $> gcc -S -o code.gasm code.c
##
## You can then convert the code.gasm to nasm with this script:
## Eg. $> g2nasm.bsh code.gasm > code.nasm
##
## Then you can compile and run your nasm on Linux:
## Eg. $> nasm -f elf64 -o code.o code.nasm
## $> gcc -no-pie -o run_code code.o
## $> ./run_code
##
## As of this initial release it works for only a subset of gas asm - over
## time this will hopefully work on all of gas asm (except prbly macros).
##
## For now this should work on simple programs.
## Caveat: *** much is still missing/unmapped ***
## Get the lastest on GitHub: https://github.com/AdamDanischewski/g2nasm
## Pull requests invited.
##
## Tested on:
## #include <stdio.h>
##
## /* function to show bytes in memory, from location start to start+n*/
## void show_mem_rep(char *start, int n)
## {
## int i;
## for (i = 0; i < n; i++)
## printf(" %.2x", start[i]);
## printf("\n");
## }
##
## /*Main function to call above function for 0x01234567*/
## int main()
## {
## int i = 0x01234567;
## show_mem_rep((char *)&i, sizeof(i));
## getchar();
## return 0;
## }
##
## 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.
##
## Released under Creative Commons License: CC BY-SA 4.0
## https://creativecommons.org/licenses/by-sa/4.0/
######################################################################
######################################################################
_=${BASH_ARGC} ### Cause BASH_ARGV to be populated, for options_handler.
declare FILE="${1}"
declare -a OPTIONS="" ## Used by: options_handler ()
declare WORKING_COPY=""
declare -a GLOBALS
declare GLOBAL_OFFSET_TABLE_FLAG=0
declare -r GLOBAL_OFFSET_TABLE="extern _GLOBAL_OFFSET_TABLE_"
declare -a RO_BLOCKS
declare RO_ALIGN=""
function usage() {
cat << EOF
Usage: ${0##*/} [-h] <file>
<file>: Gas asm file.
gcc -S -o code.gasm code.c ## file=code.gasm
OPTIONS:
-h|--help Usage
EOF
}
function main_clean() {
WORKING_COPY=$(sed -E "/.string/!{s/, /,/g;s/%//g;s/\\$//g;s/je/jz/};/[ \t]*.(file|text|type|cfi_|size|ident|section|intel_syntax)/d;s/(^[ \t]*)(sub|lea|add|cmp|xor)([ql])([ \t]*)([^,]*)(,)([ \t]*)(.*$)/\1\2\4\8, \5/;s/\b(pop|push|sub|lea)([lq])\b/\1/;/^.LF..:/d" "${FILE}")
GLOBALS[0]="$(sed -En 's/(^[ \t]*.globl)([ \t]*)([^ \t]*)(.*$)/global \3/p' <<< "${WORKING_COPY}")"
WORKING_COPY=$(sed '/^[ \t]*.globl/d' <<< "${WORKING_COPY}")
WORKING_COPY=$(sed -E '/\b[a-z]{3}q\b/s/(-[0-9]+)(\()([a-z]{3})(\))/qword [\3\1H]/' <<< "${WORKING_COPY}")
WORKING_COPY=$(sed -E '/\b[a-z]{3}l\b/s/(-[0-9]+)(\()([a-z]{3})(\))/dword [\3\1H]/' <<< "${WORKING_COPY}")
## Every offset left except lea: -12(rbp)--> dword [rbp-0CH]
WORKING_COPY=$(sed -E '/\blea\b/!s/(-[0-9]+)(\()([a-z]{3})(\))/dword [\3\1H]/' <<< "${WORKING_COPY}")
## lea mapping: -12(rbp)-->[rbp-0CH]
WORKING_COPY=$(sed -E '/\blea\b/s/(-[0-9]+)(\()([a-z]{3})(\))/[\3\1H]/' <<< "${WORKING_COPY}")
}
function load_externals() {
local call=""
local plt=""
local -i i=0
while IFS= read -r call; do
let i+=1
call=$(awk '{print $2}' <<< "${call}")
call="${call%@PLT}"
if ((! GLOBAL_OFFSET_TABLE_FLAG)); then
plt="${call##*@}"
[[ -n "${plt}" ]] && GLOBAL_OFFSET_TABLE_FLAG=1
fi
GLOBALS[${i}]="extern ${call}"
done < <(grep -P '^[ \t]*call' "${FILE}")
WORKING_COPY="$(sed 's/@PLT//g' <<< "${WORKING_COPY}")"
}
function num2hex() { printf "%02X\n" "${1}";}
function print_globals() {
local -i i=0
for((i=0;i<${#GLOBALS[@]};i++)){
printf "%11s\n" "${GLOBALS[${i}]}"
((i==0))&&echo
}
((GLOBAL_OFFSET_TABLE_FLAG)) && printf "${GLOBAL_OFFSET_TABLE}\n"
echo
}
function add_map() {
local -i line=0
local val=""
local hexval=""
## -8(rbp)-->[qd]word [rbp-8H]
}
function offset_to_hex() {
local -i line=0
local val=""
local hexval=""
while IFS= read -r line; do
val="$(sed -nE "${line}s/(^[^0-9]+)([0-9]+H)(.*$)/\2/p" <<< "${WORKING_COPY}")"
hexval=$(num2hex ${val%H})
WORKING_COPY=$(sed "${line} s/${val}/${hexval}H/" <<< "${WORKING_COPY}")
done < <(sed -nE '/^.*([dq]word|lea)[^0-9]*[0-9]+H.*$/=' <<< "${WORKING_COPY}")
}
function mov_map() {
local -i line=0
local val=""
local hexval=""
## -8(rbp)-->qword [rbp-8H]
WORKING_COPY=$(sed -E 's/(^[ \t]*)(mov)(sbl|zbl|slq|l|q)([ \t]*)([^,]*)(,)([ \t]*)(.*$)/\1\2\3\4\8, \5/' <<< "${WORKING_COPY}")
WORKING_COPY=$(sed -E '/mov[dlq]/s//mov/;s/movzbl/movzx/g;s/movsbl/movsx/g;s/movslq/movsxd/g' <<< "${WORKING_COPY}")
}
function rax_map() {
WORKING_COPY=$(sed 's/(rax)/byte [rax]/g' <<< "${WORKING_COPY}")
}
function leaq_map() {
## .LC0(rip)-->[rel ?_001]
## --------------------------------------------------------------------------->> ## Make sure it's padded to three zeroes.
WORKING_COPY="$(sed -E '/.LC[0-9]/s/(\.LC)([0-9]+)(\()([a-z]{3})(\))/[rel ?_\2]/;s/(\[rel \?_)([0-9])]/\100\2]/;s/(\[rel \?_)([0-9][0-9])]/\10\2]/' <<< "${WORKING_COPY}")"
## Increment the label number.
WORKING_COPY="$(awk '{if($0 ~ /\[rel \?_[0-9]{3}/){match($0,/([0-9]{3})/,arr);var=arr[1]+1;var=sprintf("%03d",var);gsub(/[0-9]{3}/,var, $0); print $0;}else print;}' <<< "${WORKING_COPY}")"
}
## String Arg 1 - String to convert
function convert_string_to_hex() {
xxd -ps -c1 <(echo -en "${1}\x0"|sed 's/"//g')|tr '\n' ' '|sed -r 's/.{24}/\U&\n/g;s/^[ \t]*//'|sed -r '${s/^.*$/& 00 00 00 00 00 00 00 00 00/;s/^(.{24})(.*$)/\1/;};s/ / /g;s/^.*$/\U&/'|
sed -r 's/^./db &/;s/[0-9A-F]{2}[ \t]*$/&H/;s/[0-9A-F]{2}/&H,/g;'| sed -r 's/,[ \t]*$//;s/^.*$/ &/;s/0AH[ \t]*$/00H/;s/[,H \t]*$/H/'
}
## Load Read Only Blocks
function get_ro_blocks() {
local block_start_addr=""
local block_end_addr=""
local block=""
local string=""
local ro_align=""
local -i ro_block_index=${#RO_BLOCKS[@]}
## get all read only block start addr's
while IFS= read -r block_start_addr; do
## Note: start at index 1 since that's what nasm starts it's ro block labels at
let ro_block_index+=1
## save all read only blocks
block="$(sed -En "${block_start_addr},/^[^ \t]/{/^[^ \t]/b;p}" <<< "${WORKING_COPY}")"
block_end_addr="$(($(sed -En "$((${block_start_addr}+1)),\${/^[\.a-z]/{=;q}}" <<< "${WORKING_COPY}")-1))"
WORKING_COPY="$(sed -E "${block_start_addr},${block_end_addr}s/^.*$/DELETE_THIS/" <<< "${WORKING_COPY}")"
ro_align=$(sed -rn 's/^([ \t]*.align )([0-9]+)(.*$)/align=\2/p' <<< "${block}")
[[ -n "${ro_align}" ]] && RO_ALIGN="${ro_align}" && block="$(sed -r "s/.align[ \t]*[0-9]+//"<<<"${block}")"
string=$(sed -rn 's/^([ \t]*\.string)([ \t]*)("[^"]*")(.*$)/\3/p' <<< "${block}")
if [[ -n "${string}" ]]; then
string="$(convert_string_to_hex "${string}" | tr '\n' '\001')"
block="$(sed -r "s/^[ \t]*\.string.*\$/${string}/;/^[ \t]*.(text|type|globl)/d" <<< "${block}"|tr '\001' '\n')"
fi
RO_BLOCKS[${ro_block_index}]="${block}"
done < <(sed -En '/^\.LC[0-9]+:.*$/=' <<< "${WORKING_COPY}")
WORKING_COPY="$(sed '/^DELETE_THIS/d' <<< "${WORKING_COPY}")"
}
function print_ro_blocks() {
local -i i=1
printf "SECTION .rodata %s\n\n" "${RO_ALIGN}"
for((i=1;i<=${#RO_BLOCKS[@]};i++)){
printf "?_%03.f:\n" ${i}
printf "%s\n" "${RO_BLOCKS[$i]}"
}
}
function fs_map() {
## fs:40-->qword [fs:abs 28H]
WORKING_COPY="$(sed -E '/\bfs:[0-9]+\b/s/\bfs:([0-9]+)\b/qword [fs:abs \1H]/' <<< "${WORKING_COPY}")"
}
function init() {
main_clean
load_externals
get_ro_blocks
mov_map
leaq_map
rax_map
fs_map
offset_to_hex ## Call this last.
}
function print_section_text() {
printf "SECTION .text\n\n"
}
function print_section_data() {
printf "SECTION .data\n\n"
}
function print_section_bss() {
printf "SECTION .bss\n\n"
}
function main() {
init
print_globals
print_section_text
printf "%s\n\n" "${WORKING_COPY}"
print_section_data
print_section_bss
print_ro_blocks
}
function options_handler() {
local -i i=0
local CURVAL=""
local NEXTVAL=""
((${#BASH_ARGV[@]}==0)) && usage && exit 0
for((i=$((${#BASH_ARGV[@]}-1));i>=0;i--)); do
CURVAL=${BASH_ARGV[${i}]}
NEXTVAL=${BASH_ARGV[$((${i}-1))]}
case "${CURVAL}" in
-h|-?|--help)
usage
exit 0
;;
*)
FILE="${CURVAL}"
[[ ! -e "${FILE}" ]] && printf "*** ERROR - File (\"${FILE}\") not found. \n" && exit 1
;;
esac
done
}
options_handler
main
exit 0
Subscribe to:
Posts (Atom)