Code review & Regular Expression commands

Grep Fu section

for i in $(grep -R <PATTERN_YOU_ARE_LOOKING_FOR>|cut -d : -f 1|sort -u); do echo "\n==========$i==========\n" && cat $i && read -s -d ' ' && clear;done
for i in $(grep -R <$PATTERN1>|cut -d : -f 1|sort -u); do echo "\n==========$i==========\n" && cat $i|grep -i <$PATTERN2> && read -s -d ' ' && clear;done
for i in $(grep -R <$PATTERN1>|cut -d : -f 1|sort -u); do echo "\n==========$i==========\n" && gedit $i;done

grep -rnw --color "<Pattern>"
# To show only-matching
grep -rnw -o --color "<Pattern>" 
# To show only files containing tha pattern. Useful when there a lot of text in the grep
grep -rnw -l --color "<Pattern>" 
# To exclude certain file extensions.
grep -rnw --color "<Pattern>" --exclude "*.js"

Regex Section

Targeted/Group Matching

perl -lne 'print $1 if /<R(E)GEX>/' < * 
perl -lne 'print "$1/$2" if /<R(E)GE(X)>/' < * 
perl -lne 'print "$1/$2" if /src=\"(.*?)\/(.*?)\//' blob.txt
for i in $(find .); do perl -lne 'print "$1/$2" if /src=\"(.*?)\/(.*?)\//' 2>/dev/null < $i;done|sort -u

Grouping^1

a(bc)           parentheses create a capturing group with value bc
a(?:bc)*        using ?: we disable the capturing group
a(?<foo>bc)     using ?<foo> we put a name to the group -> Try it!

To Test: https://regex101.com

Email Regex

[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+

URL Regex

https?://(www\.)?\w+\.\w+

IP Address^2

cat * | egrep -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"

Subdirectory finder

for i in $(find .); do perl -lne 'print "$1" if /https\:\/\/domain\.com\/(.*?)[\/\"]/'  2>/dev/null < $i;done|sort -u
for i in $(find .); do perl -lne 'print "$1" if /href\=\"(.*?)[\/\"]/'  2>/dev/null < $i;done|grep -v "http:" |grep -v "https:" | sed "s/\&\#32\;/\ /g" | grep -v "\.html" |sort -u

Modyfy the following file to remove SNORT/Suricata detections


=> osscan2.cc

User-Agent

ET SCAN Nmap Scripting Engine User-Agent Detected (Nmap Scripting Engine

User-Agent will be seen in the packet so lets remove the default

SET => 
USER_AGENT = stdnse.get_script_args('http.useragent') or "Mozilla/3.0 (compatible; )"

TCP Windows

ET SCAN NMAP -sS window 1024

SET =>
tcp->th_win = htons(9999); /* Who cares */

ZMap

Detect ZMAP scan

SET=>
tcp_header->th_win = htons(65535);
iph->ip_id = htons(54321);

Useful commands in Linux

comm

read

sed

Basic Sed Commands

Recursive sed

shred

testdisk

ps (Wide output)

grep

RDP

iptables ^4

# View rules
sudo iptables -L --line-numbers

# Add rule
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 22 -j ACCEPT

# Drop from everybody else
sudo iptables -A INPUT -p tcp --dport 22 -j DROP

# Save rules
sudo /sbin/iptables–save

# Delete Rule
sudo iptables -D INPUT <line_num>

# Example:
# Only allow SSH connection from 192.168.1.0/24
sudo iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j DROP

Mirror a website

wget --mirror --convert-links --adjust-extension --page-requisites --wait=1 -o wget-mirror.log --no-parent https://example.org

Upload files via Curl

Upgrade Reverse shell to fully interactive TTY[^6]

Create a user

# Create a user
useradd user -U -s /bin/bash
# Create a sudo user
useradd user -G sudo -U -s /bin/bash

Transfer files using nc (netcat)[^7]

Base64 encode

Rsync

Compressed RDP for low bandwidth

Encrypting files

zip[^8]

zip -e secure.tar.xz.zip notsecure.tar.xz -P someGoodPassword
unzip secure.tar.xz.zip

tar & OpenSSL[^9][^10]

Generate random passwords

openssl rand 256 | sha256sum | cut -d " " -f1

SERVICE_PASSWORD=$(openssl rand 32 | sha256sum | cut -d' ' -f1)
echo $SERVICE_PASSWORD

Go through filenames with spaces

List files and sort via date time

ls -t

Sort contents of a file in reverse

rev file.txt | sort | rev

Beep

grep for file extensions

sudo apt install -y pcregrep
cat spidered_shares.txt|pcregrep -M "\.pub.\n"
cat spidered_shares.txt|pcregrep -M "\.key.\n"
cat spidered_shares.txt|pcregrep -M "\.ppk.\n"

Mobile App Security Testing

Some commands

Logcat

Useful tips and tricks in Python

TEMPLATE or Skeleton script with tips and references

Generate requirements.txt

Update all pip packages

python3 -m pip install --upgrade pip
pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip3 install -U

Global Variables

# Below "import" <insert_package_names>
timeout = None

def imAFunction():
  if i > timeout:
    print("Greater than timeout")

# Inside function. In this example main()
global timeout 
timeout = args.timeout

URL Encode Python String

import urllib.parse
action = urllib.parse.quote_plus("GET_[TO]-THE")
location = urllib.parse.quote_plus("CHOPP@!")
url = "www.domain.com:2323/whatever?action=%s&location=%s" % (action,location)

Accept custom certificate

WinDbg

Shortcut Keys

Basic commands/shortcuts

bp kernel32!WriteFile ".if (poi(esp + 0x0C) == 4) {.printf \"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\\nHey it's 4 bytes! Stopping at breakpoint now...\\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\\n\"} .else {.printf \"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\\nThe number of bytes written is %p \\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\\n\",poi(esp + 0x0C);.echo;gc;}"

     Evaluate expression:
     Hex:     00000054
     Decimal: 84
     Octal:   00000000124
     Binary:  00000000 00000000 00000000 01010100
     Chars:   ...T
     Time:    Wed Dec 31 16:01:24 1969
     Float:   low 1.17709e-043 high 0
     Double:  4.15015e-322
* Pseudo Registers(Variables) : @$t0 to @$t19 * r @$t0 = (5454 - 54) * 0n10 * r @$t0 : $t0=00034800 * r @$t1 = @$t0 >> 8 * r @$t1 : $t1=00000348

Useful commands in Windows

Windows - Command Prompt

Find a file

dir "\secretfile" /s

Find strings in files

findstr /s /i "FLAG{" C:\*.*

Find multiple words, strings and patterns

Enable default local "administrator" account

net user administrator /active:yes

Adding local accounts (Must have system privileges)

net user trojand imashortpassword /add
net localgroup administrators trojand /add

Adding a Domain Admin account

net group "Domain Admins"
net user trojandDA P@ssw0rd /add /domain
net group "Domain Admins" trojandDA /add /domain
net group "Domain Admins"

List files using tree

tree /f
tree /f /a > tree.txt

Change user password

net user trojand imareallyreallyreallylongpasswordnow
netsh interface portproxy add v4tov4 listenport=<LPORT> listenaddress=0.0.0.0 connectport=<RPORT> connectaddress=<RHOST>

Show wireless interfaces

netsh wlan show networks mode=bssid

Check for logged on users

query user
qwinsta

List local drives^1

wmic logicaldisk get description,name | findstr /C:"Local"
fsutil fsinfo drives

bitsadmin[^4]

Copying a File

Execute a file


Windows - Powershell

Nested quotes or wrapping multiple double quotes

Download & Uploading files

cat, tail, grep in Windows PS

Find files and contents

Expand Archives

Expand-Archive Procdump.zip -DestinationPath "C:\temp\" -Force -Verbose

Compress and Archive

Base64 Encode Powershell commands

List directory and sort by Date Time

Tasklist

Taskkill

Including Status code ^1

fmt.Printf("RESPONSE:\n")
fmt.Println(resp.Status)
for k, v := range resp.Header {
  fmt.Print(k)
  fmt.Print(" : ")
  fmt.Println(v)
}

Includes Headers and Body in a well formatted way ^2 * Request * "Use httputil.DumpRequest() if you want to pretty-print the request on the server side." * "Use httputil.DumpRequestOut() if you want to dump the request on the client side."

    reqDump, err := httputil.DumpRequestOut(req, true)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("REQUEST:\n%s", string(reqDump))

^2: Go Samples

JQ tips

Disclaimer

There is no need for this nonsense. Refer to this awesome person: Lzone jq cheatsheet

Installation

sudo apt install jq


Filtering (Grepping) for specific key values, then selecting a specific key afterwards

Printing 2 or more values

How to list USB devices on linux command line

The following Gist describes the use of different commands to list all USB devices. Feel free to comment and contribute.

Table of Contents

  1. DF Command
  2. PARTED Command
  3. FDISK Command
  4. LSBLK Command
  5. FINDMNT Command
  6. LSUSB Command

1. First option - DF Command

What you probably are looking for is the df command. This command (which reports the file system disk space usage) can be used along with grep to only print the lines that match the patterns we require, in our case, media. As no file name is given, it will look for mounted file systems so if your USB is encrypted and not already mounted it won't be displayed.

df -Th | grep media

Where the output is:

device name                                     mounted on
/dev/sda       ext2       58G   52M   55G   1% /media/user/device
Option Description
-T Print file system type
-h Human readable (print sizes in powers of 1024)
-H Print sizes in powers of 1000

2. Second option - PARTED Command

In this case we are going to list the USB devices using a partition manipulation program. This command is named parted and requires superuser (root) privileges.

sudo parted -l

Where the output is:

Model: Vendors model (scsi)
Disk /dev/sda: 62,9GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0,00B  62,9GB  62,9GB  ext2


Model: Your computer (nvme)
Disk /dev/nvme0n1: 256GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system     Name                  Flags
 1      1049kB  538MB   537MB   fat32           EFI System Partition  boot
 2      538MB   2685MB  2147MB  linux-swap(v1)                        swap
 3      2685MB  4833MB  2147MB  zfs
 4      4833MB  256GB   251GB   zfs
Option Description
-l Lists partition layout on all block devices
-m Use it along with -l option to display machine parseable output

3. Third option - FDISK Command

The third option also requires the use of sudo to list the disk partition table with the command fdisk.

sudo fdisk -l | grep /dev/s

Where the output is:

      name
Disk  /dev/sda:     58,61 GiB, 62914560000 bytes, 122880000 sectors
Option Description
-l List the partition tables for the specified devices and then exit. If no devices are given, those mentioned in /proc/partitions are used

4. Fourth option - LSBLK Command

Here the command lists block devices. The command prints all block devices (except RAM disks) in a tree-like format by default. sudo blkid | grep sd can also be used.

lsblk | grep sd

Where the output is:

sda    8:0    1  58,6G  0 disk /media/user/device

5. Fifth option - FINDMNT Command

findmnt will list all mounted filesystems or search for a filesystem so if your USB is encrypted and not already mounted it won't be shown.

findmnt | grep media

Where the output is:

└─/media/user/device /dev/sda       ext2       rw,nosuid,nodev,relatime

6. Sixth option - LSUSB Command

This option won't tell where the device is mounted or how it is called but list all USB devices and display information about its buses in the system and the devices connected to them. Type only one -v to show less information, two -vv for full information. Once you have identified the bus and dev variables of your USB device, then you could grep them to usb-devices for even more information.

lsusb -tvv

Where the output is:

/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/6p, 5000M
    ID 1d6b:0003 Linux Foundation 3.0 root hub
    /sys/bus/usb/devices/usb2  /dev/bus/usb/002/001
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/12p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
    /sys/bus/usb/devices/usb1  /dev/bus/usb/001/001
    |__ Port 1: Dev 22, If 0, Class=Mass Storage, Driver=usb-storage, 480M
        ID 0930:6544 Toshiba Corp. TransMemory-Mini / Kingston DataTraveler 2.0 Stick
        /sys/bus/usb/devices/1-1  /dev/bus/usb/001/022
    |__ Port 5: Dev 3, If 3, Class=Video, Driver=uvcvideo, 480M
        ID 05c8:0815 Cheng Uei Precision Industry Co., Ltd (Foxlink) 
        /sys/bus/usb/devices/1-5  /dev/bus/usb/001/003
    |__ Port 5: Dev 3, If 1, Class=Video, Driver=uvcvideo, 480M
        ID 05c8:0815 Cheng Uei Precision Industry Co., Ltd (Foxlink) 
        /sys/bus/usb/devices/1-5  /dev/bus/usb/001/003
    |__ Port 5: Dev 3, If 2, Class=Video, Driver=uvcvideo, 480M
        ID 05c8:0815 Cheng Uei Precision Industry Co., Ltd (Foxlink) 
        /sys/bus/usb/devices/1-5  /dev/bus/usb/001/003
    |__ Port 5: Dev 3, If 0, Class=Video, Driver=uvcvideo, 480M
        ID 05c8:0815 Cheng Uei Precision Industry Co., Ltd (Foxlink) 
        /sys/bus/usb/devices/1-5  /dev/bus/usb/001/003
    |__ Port 7: Dev 5, If 0, Class=Wireless, Driver=btusb, 12M
        ID 8087:0a2b Intel Corp. 
        /sys/bus/usb/devices/1-7  /dev/bus/usb/001/005
    |__ Port 7: Dev 5, If 1, Class=Wireless, Driver=btusb, 12M
        ID 8087:0a2b Intel Corp. 
        /sys/bus/usb/devices/1-7  /dev/bus/usb/001/005
Option Description
-t Tells lsusb to dump the physical USB device hierarchy as a tree
-v Verbose. Tells lsusb to be verbose and display detailed information about the devices shown

[Reference]

^1: Stack Overflow

[^3]: It's FOSS [^4]: Fedora Project

[^6]: 6c2e6e2e - Spawning interactive reverse shells with TTY [^7]: Tutorials Technology - How to transfer files over the network using Netcat [^8]: StackOverflow - How zip file with encryption from bash script [^9]: Tecmint - How to Encrypt and Decrypt Files and Directories Using Tar and OpenSSL [^10]: StackOverflow - Securely passing password to openssl via stdin [^11]: AskUbuntu - Filenames with spaces breaking for loop, find command [^12]: StackOverflow - Understanding a sed command: sed 's/\s\s*/ /g' [^13]: Stackoverflow - How to sort a list of words by the last character [^14]: Reddit - Beep could not open any device... [^15]: StackOverflow- How to give a pattern for new line in grep? [^16]: Windows Commandline - List local drives from command line

[^18]: Tech Expert [^19]: TrustedSec - BITS for Script Kiddies [^20]: Windows Commandline - taskkill [^21]: Shellhacks - Windows: Zip | Unzip – Command Line

[^23]: Medium - Gitconnected [^24]: Factory Mind [^25]: Regex Tutorial