Skip to content

Instantly share code, notes, and snippets.

@nhahv
Last active September 23, 2021 11:02
Show Gist options
  • Save nhahv/f65738ef3fa189e22d4f8258d7af50a6 to your computer and use it in GitHub Desktop.
Save nhahv/f65738ef3fa189e22d4f8258d7af50a6 to your computer and use it in GitHub Desktop.
Bash

MINIFY BASH

Download obfsh

wget -O - http://www.comp.eonworks.com/scripts/obfsh.gz | gunzip > obfsh  && chmod +x obfsh

Clean up bash file

./obfsh -i -f test.sh > test.min.sh

Minify to string

cat test.min.sh | gzip -cf | base64 -w0

Embed to bash file

echo "H4sIACERRGEAA8VVXW/aMBR996/wXFcqkzJCmTRNKA9AM4SGAkrYXmhlhdS00YITOUk/FuW/z3YI+aKrpnXaE/jc65t7zrm2z971tz7rb934HkyXVyZxxt9NMv1mO0vbQNf6cLiJURGxTWe9tFvB9BAsQDK3iDO1l4sFGdvm+JAzGMukhQh/mQmIbob6vkQmBfLxsoaQyWI5/WpeFZGhiJSfLvd/rmFlBYmt7OVM4E5VYecGMUVgbY9Xq7k1I6Y1nixaAeKY6yMgiNimtSaWTRZzy3QMHcQ0SSMSezwMAuJy6l70YAb8HdxAhNuVETQgSnhKEbwZweSeMpBwNyIhIz5LKOdplICdDwKf0djAF0mUJlAtet1vYxUAAT2kHABtAKh3H0KNMoiuGaqtcNvEelC5oY9wpqrkvLux6XE3ftJmBG65+0giHt5xGsdk63Kogxzc0jjh4XNHuVPU/zcDL6Aub1BoSCxFrhkuJ+YFo6EG59ZaOpx3ZVH0I8o9yhL3jhp4cFKMtt/lp9UCQY1RyaY1LFUbnXmV3fyJwKW8I32HgGqMBi+frYiLwZbsSEKfEogrfq+ak4NtEHo/3kSjf0BQuft3/Lpz9eIReHtGr7fX5Fa0FnpuAFsGKMwLg2PP8n/Rstwc+z9FnsS0wacqO+QGwll59efl38ksP56mtugnTtXvCpXbREF5p1LZ2D4Sv7To6QKX7b2v+dbrD3RdZXO6d312S0sKZbaGG3VA3UAhQSGsPIYbNII1mTPVbC5ApeyOMPoI0RmCzXrNPdXrllcLKVKzygdRpdlvWUY2coN6I1At4erQMcRZRTw/l8sal1wOAWXuNqBE3l6Rz+7UFHQfzOIw5N3X7JhfvKNFnroK5fCztJmtbsfiXLRDqtCJRwPQJz8ph1WJoTJFnpxNlu4NfAkehC1FAkTnGs4EnIu7EmGRhno1ZXD20O/DvsQV+1/FWZgIAgkAAA==" | base64 -d | gunzip > /tmp/draw_progress_bar
source /tmp/draw_progress_bar
#!/bin/bash
function __curl() {
read proto server path <<<$(echo ${1//// })
DOC=/${path// //}
HOST=${server//:*}
PORT=${server//*:}
[[ x"${HOST}" == x"${PORT}" ]] && PORT=80
exec 3<>/dev/tcp/${HOST}/$PORT
echo -en "GET ${DOC} HTTP/1.0\r\nHost: ${HOST}\r\n\r\n" >&3
(while read line; do
[[ "$line" == $'\r' ]] && break
done && cat) <&3
exec 3>&-
}
#!/bin/bash
function __wget() {
: ${DEBUG:=0}
local URL=$1
local tag="Connection: close"
if [ -z "${URL}" ]; then
printf "Usage: %s \"URL\" [e.g.: %s http://www.google.com/]" \
"${FUNCNAME[0]}" "${FUNCNAME[0]}"
return 1;
fi
read proto server path <<<$(echo ${URL//// })
local SCHEME=${proto//:*}
local PATH=/${path// //}
local HOST=${server//:*}
local PORT=${server//*:}
if [[ "$SCHEME" != "http" ]]; then
printf "sorry, %s only support http\n" "${FUNCNAME[0]}"
return 1
fi
[[ x"${HOST}" == x"${PORT}" ]] && PORT=80
[[ $DEBUG -eq 1 ]] && echo "SCHEME=$SCHEME" >&2
[[ $DEBUG -eq 1 ]] && echo "HOST=$HOST" >&2
[[ $DEBUG -eq 1 ]] && echo "PORT=$PORT" >&2
[[ $DEBUG -eq 1 ]] && echo "PATH=$PATH" >&2
exec 3<>/dev/tcp/${HOST}/$PORT
if [ $? -ne 0 ]; then
return $?
fi
echo -en "GET ${PATH} HTTP/1.1\r\nHost: ${HOST}\r\n${tag}\r\n\r\n" >&3
if [ $? -ne 0 ]; then
return $?
fi
# 0: at begin, before reading http response
# 1: reading header
# 2: reading body
local state=0
local num=0
local code=0
while read line; do
num=$(($num + 1))
# check http code
if [ $state -eq 0 ]; then
if [ $num -eq 1 ]; then
if [[ $line =~ ^HTTP/1\.[01][[:space:]]([0-9]{3}).*$ ]]; then
code="${BASH_REMATCH[1]}"
if [[ "$code" != "200" ]]; then
printf "failed to wget '%s', code is not 200 (%s)\n" "$URL" "$code"
exec 3>&-
return 1
fi
state=1
else
printf "invalid http response from '%s'" "$URL"
exec 3>&-
return 1
fi
fi
elif [ $state -eq 1 ]; then
if [[ "$line" == $'\r' ]]; then
# found "\r\n"
state=2
fi
elif [ $state -eq 2 ]; then
# redirect body to stdout
# TODO: any way to pipe data directly to stdout?
echo "$line"
fi
done <&3
exec 3>&-
}

CYGWIN

"c:\cygwin64\bin\sh" -lic "cd ${OLDPWD-.}; zsh"

lscpu | tr -s " " | grep -e "Model name:" -e "^CPU(s)" | sed 's/Model name/CPU Model/; s/CPU(s)/CPU Cores/' | tac && free -g --si | tr -s " " | grep "Mem:" | cut -d " " -f2 | read x; echo "Ram Total: ${x}GB" && fdisk -l | grep "^Disk /" | awk '{print "Disk ",$2,$3,$4 }' | sed 's/,//; s/\/dev\///; s/[^ _-]*/\u&/g'

Common bash function in linux container

cat 


#!/bin/bash
# https://github.com/pollev/bash_progress_bar - See license at end of file
# Usage:
# Source this script
# enable_trapping <- optional to clean up properly if user presses ctrl-c
# setup_scroll_area <- create empty progress bar
# draw_progress_bar 10 <- advance progress bar
# draw_progress_bar 40 <- advance progress bar
# block_progress_bar 45 <- turns the progress bar yellow to indicate some action is requested from the user
# draw_progress_bar 90 <- advance progress bar
# destroy_scroll_area <- remove progress bar
# Constants
CODE_SAVE_CURSOR="\033[s"
CODE_RESTORE_CURSOR="\033[u"
CODE_CURSOR_IN_SCROLL_AREA="\033[1A"
COLOR_FG="\e[30m"
COLOR_BG="\e[42m"
COLOR_BG_BLOCKED="\e[43m"
RESTORE_FG="\e[39m"
RESTORE_BG="\e[49m"
# Variables
PROGRESS_BLOCKED="false"
TRAPPING_ENABLED="false"
TRAP_SET="false"
CURRENT_NR_LINES=0
setup_scroll_area() {
# If trapping is enabled, we will want to activate it whenever we setup the scroll area and remove it when we break the scroll area
if [ "$TRAPPING_ENABLED" = "true" ]; then
trap_on_interrupt
fi
lines=$(tput lines)
CURRENT_NR_LINES=$lines
let lines=$lines-1
# Scroll down a bit to avoid visual glitch when the screen area shrinks by one row
echo -en "\n"
# Save cursor
echo -en "$CODE_SAVE_CURSOR"
# Set scroll region (this will place the cursor in the top left)
echo -en "\033[0;${lines}r"
# Restore cursor but ensure its inside the scrolling area
echo -en "$CODE_RESTORE_CURSOR"
echo -en "$CODE_CURSOR_IN_SCROLL_AREA"
# Start empty progress bar
draw_progress_bar 0
}
destroy_scroll_area() {
lines=$(tput lines)
# Save cursor
echo -en "$CODE_SAVE_CURSOR"
# Set scroll region (this will place the cursor in the top left)
echo -en "\033[0;${lines}r"
# Restore cursor but ensure its inside the scrolling area
echo -en "$CODE_RESTORE_CURSOR"
echo -en "$CODE_CURSOR_IN_SCROLL_AREA"
# We are done so clear the scroll bar
clear_progress_bar
# Scroll down a bit to avoid visual glitch when the screen area grows by one row
echo -en "\n\n"
# Once the scroll area is cleared, we want to remove any trap previously set. Otherwise, ctrl+c will exit our shell
if [ "$TRAP_SET" = "true" ]; then
trap - INT
fi
}
draw_progress_bar() {
percentage=$1
lines=$(tput lines)
let lines=$lines
# Check if the window has been resized. If so, reset the scroll area
if [ "$lines" -ne "$CURRENT_NR_LINES" ]; then
setup_scroll_area
fi
# Save cursor
echo -en "$CODE_SAVE_CURSOR"
# Move cursor position to last row
echo -en "\033[${lines};0f"
# Clear progress bar
tput el
# Draw progress bar
PROGRESS_BLOCKED="false"
print_bar_text $percentage
# Restore cursor position
echo -en "$CODE_RESTORE_CURSOR"
}
block_progress_bar() {
percentage=$1
lines=$(tput lines)
let lines=$lines
# Save cursor
echo -en "$CODE_SAVE_CURSOR"
# Move cursor position to last row
echo -en "\033[${lines};0f"
# Clear progress bar
tput el
# Draw progress bar
PROGRESS_BLOCKED="true"
print_bar_text $percentage
# Restore cursor position
echo -en "$CODE_RESTORE_CURSOR"
}
clear_progress_bar() {
lines=$(tput lines)
let lines=$lines
# Save cursor
echo -en "$CODE_SAVE_CURSOR"
# Move cursor position to last row
echo -en "\033[${lines};0f"
# clear progress bar
tput el
# Restore cursor position
echo -en "$CODE_RESTORE_CURSOR"
}
print_bar_text() {
local percentage=$1
local cols=$(tput cols)
let bar_size=$cols-17
local color="${COLOR_FG}${COLOR_BG}"
if [ "$PROGRESS_BLOCKED" = "true" ]; then
color="${COLOR_FG}${COLOR_BG_BLOCKED}"
fi
# Prepare progress bar
let complete_size=($bar_size*$percentage)/100
let remainder_size=$bar_size-$complete_size
progress_bar=$(echo -ne "["; echo -en "${color}"; printf_new "#" $complete_size; echo -en "${RESTORE_FG}${RESTORE_BG}"; printf_new "." $remainder_size; echo -ne "]");
# Print progress bar
echo -ne " Progress ${percentage}% ${progress_bar}"
}
enable_trapping() {
TRAPPING_ENABLED="true"
}
trap_on_interrupt() {
# If this function is called, we setup an interrupt handler to cleanup the progress bar
TRAP_SET="true"
trap cleanup_on_interrupt INT
}
cleanup_on_interrupt() {
destroy_scroll_area
exit
}
printf_new() {
str=$1
num=$2
v=$(printf "%-${num}s" "$str")
echo -ne "${v// /$str}"
}
# SPDX-License-Identifier: MIT
#
# Copyright (c) 2018--2020 Polle Vanhoof
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
alias genpw="cat /dev/urandom | tr -dc 'a-zA-Z0-9~!@#$%^&*-=:;.,/?' | fold -w ${1:-16} | head -n 1"
awk 'BEGIN{states["01"]="TCP_ESTABLISHED";states["02"]="TCP_SYN_SENT";states["03"]="TCP_SYN_RECV";states["04"]="TCP_FIN_WAIT1";states["05"]="TCP_FIN_WAIT2";states["06"]="TCP_TIME_WAIT";states["07"]="TCP_CLOSE";states["08"]="TCP_CLOSE_WAIT";states["09"]="TCP_LAST_ACK";states["0A"]="TCP_LISTEN";states["0B"]="TCP_CLOSING";states["0C"]="TCP_NEW_SYN_RECV";}
function hextodec(str,ret,n,i,k,c){ret = 0;n = length(str);for (i = 1; i <= n; i++) {c = tolower(substr(str, i, 1));k = index("123456789abcdef", c);ret = ret * 16 + k;};return ret;}
function getIP(str,ret){ret=hextodec(substr(str,index(str,":")-2,2));for (i=5; i>0; i-=2) {ret = ret"."hextodec(substr(str,i,2));}ret = ret":"hextodec(substr(str,index(str,":")+1,4));return ret;}
NR > 1 {{
if(NR==2)
printf"┌──────────────────────┬──────────────────────┬─────────────────┐\r\n│ %-21s│ %-21s│ %-16s│\r\n├──────────────────────┼──────────────────────┼─────────────────┤\r\n","LOCAL","REMOTE","STATE";
printf "│ %-21s│ %-21s│ %-16s│\r\n",getIP($2),getIP($3),states[$4];
}}
END{print "└──────────────────────┴──────────────────────┴─────────────────┘";}
' /proc/net/tcp
sed 's/:/\n/g' <<< "$PATH"
#echo > /dev/tcp/127.0.0.1/1234
cat < /dev/tcp/127.0.0.1/22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment