Wednesday, October 29, 2014

Bash Script - Convert int to binary

There are many ways to convert integer variable to binary. Here is one:
#!/bin/bash
a=65535
out=""
while [ $a -gt 0 ]
do
  b=$(($a%2))
a=$(($a/2))
out="$b$out"
done
echo $out

Thursday, May 15, 2014

Linux - How to change default sshd port

There are many reasons why default port 22 for ssh is need to be changed. If you need to change default port or add other listening port:

1. open /etc/ssh/sshd_config with your favorite text editor
2. find line
Port 22

and

edit this value to your needs.

or if you want to add other listening port, simply add line with port. Example:

Port 22
Port 1000

after editing save config file and restart sshd:

$ sudo /etc/init.d/ssh restart

or

$ service sshd restart

dependant on system you use.

File /etc/ssh/sshd_config opened in editor



Monday, May 12, 2014

Bash - Substring

How to get substring in Bash script? Simple :)

if we have string:

$ a=123456789

substring from position 1 to 5 is

$ echo ${a:1:5}
23456

First position in a string is 0.

in general: ${varname:start_pos:length}


Sunday, May 11, 2014

Linux - Make ISO image of CD

If we want to make exact copy of cd media type, because Linux handles everything as file, we need to read all data from one file and write it to imagefile on disk.

$ cat /dev/cdrom >./imagefile.iso

another way is using dd command:

dd if=/dev/cdrom of=imagefile.iso

Saturday, May 10, 2014

Linux - Printing first or last 10 lines

There are many examples when we need to print first or last lines of some file. Commands head and tail ca help to do this.

Command head prints 10 first lines of file.

$ head file

or

$ cat file|head

There is command line argument -n, which says how many lines to be printed.

Example:

$ head -n 5 file

prints first 5 linest from file.

if number is negative, command head prints last n lines from file.

Command tail does oposite from command head: prints last 10 lines from a file.

$ tail file

same as command head there is argument -n which tells to command head how many last lines from a file is to be printed:

$ tail -n 7 file

prints last 7 lines from file.

If you want to print all lines in file excluded first n lines, command is:

$ tail -n +5 file

prints all lines in file, excluded first 5 lines.

Command tail is very useful in log analyzing, when you are not interested in whole file, but only last n lines.

Friday, May 9, 2014

Linux - Find ssh attacks

All login attempts, successful or not, are in /var/log/auth.log file. To find from which IP addresses attempts are coming, and only for valid usernames, execute following line:

$ cat /var/log/auth.log|grep -v "invalid"|grep "Failed password"|awk '{ print $(NF-3) }'|sort|uniq

grep -v "invalid" - -v switch reverts filter then only lines without word "invalid" are included
grep "Failed password" - only lines with words "Failed password" are included
awk '{ print $(NF-3) }' - Take 3rd arg from last
sort
and print only unique IP addresses.


Also, if you want to find on which usernames attacks are targeted change awk part into: awk '{ print $(NF-5) }'





Thursday, May 8, 2014

Linux - File Checksum

The purpose of file checksums is to validate integrity of files. There are many ways to do checksums on Linux, but the simplest is by using md5sum command (for MD5 checksum). Format is:
md5sum [options] [file1] ... [file n]

Options are:
-b or --binary - binary mode
-t or --text - text mode (default)
-c or --check - read file with checksums and check them

For test purpose make one file named test

$ md5sum test
ee10c66475e841d934c0e39d07650d4a  test

output md5 checksum to file:
$ md5sum test >checksum

check:
$md5sum -c checksum
test: OK

MD5 gives 128 bit checksum. For security related purposes is better to use SHA-2 algorithm implemented in: sha224sum, sha256sum, sha384sum and sha512sum programs.