Check NTP server working or not, Check NTP server Date and Time – Windows, Linux

Windows

1 Use Win + R to open run Window

Microsoft Windows - Run window
Microsoft Windows – Run window

2 Type cmd, then hit Enter key

Microsoft Windows - Run window - cmd.exe
Microsoft Windows – Run window – cmd.exe

The command we use is in following format:

w32tm /stripchart /computer:domainnameORipaddress /dataonly /samples:5

3 Enter following command to test “time.windows.com”

w32tm /stripchart /computer:time.windows.com /dataonly /samples:5
Microsoft Windows - CMD -   w32tm /stripchart /computer:time.windows.com /dataonly /samples:5 output
Microsoft Windows – CMD – w32tm /stripchart /computer:time.windows.com /dataonly /samples:5 output

Bonus: Other commands

1 Show the current time zone settings:

w32tm /tz

2 List external NTP servers (peers)

w32tm /query /peers
Microsoft Windows - CMD -  w32tm /query /peers output
Microsoft Windows – CMD – w32tm /query /peers output

Note! If you have encountered this error: “The following error occurred: The service has not been started. (0x80070426)”, you need to start the “Windows Time” service, you can start a elevated cmd or PowerShell by using Win + X key combination then select “Command Prompt (Admin)” or “Windows PowerShell (Admin)” then type “sc start W32Time” to start the Windows Time service or using Run window and type “services.msc” then find “Windows Time” service and start it.

3 Restore Windows Time service settings to default

C:\>net stop w32time
C:\>w32tm /unregister
C:\>w32tm /register
C:\>net start w32time

4 Re-sync Time

Resynchronize the clock as soon as possible, disregarding all accumulated error statistics. If no computer is specified, the local computer will resynchronize. The command will wait for resynchronization unless the /nowait switch is used.

w32tm /resync

5 Redirect the network configuration and Re-sync time

Force redetection of network resource before resynchronization.

w32tm /resync /rediscover
Microsoft Windows - PowerShell - w32tm /resync /rediscover output
Microsoft Windows – PowerShell – w32tm /resync /rediscover output

6 Check current NTP configuration

Show Information:

w32tm /query /status
Microsoft Windows - PowerShell -  w32tm /query /status  output
Microsoft Windows – PowerShell – w32tm /query /status output

Show Configuration:

w32tm /query /configuration
Microsoft Windows - PowerShell -  w32tm /query /configuration  output
Microsoft Windows – PowerShell – w32tm /query /configuration output

Linux

1 ntpdate -q domainnameORipaddress

ntpdate -q time.windows.com
Linux - Terminal - ntpdate -q time.windows.com output
Linux – Terminal – ntpdate -q time.windows.com output

2 ntpq -p domainnameORipaddress

ntpq -p 192.168.1.1

How to use Apache .htaccess .htpasswd to protect files, folders and paths and to protect multiple files, multiple folders and paths

Keywords: Apache, .htaccess, .htpasswd, block access, protect file, protect folder, protect directory, protect path

If we have following folders

/web/resource1/file1.htm
/web/resource1/A/
/web/resource2/file2.php
/web/resource2/B/C/D/
/web/resource3/E/F/

and Following url path

https://www.example.com/
https://www.example.com resource1/file1.htm
https://www.example.com/ resource1/A/
https://www.example.com/ resource2/file2.php
https://www.example.com/ resource2/B/C/D/
https://www.example.com resource3/E/F/ https://www.example.com/secret

The document root for “https://www.example.com/” is “/web/”

Path secret is a virtual path which does not reflect to a real directory with name “secret” (e.g. an existing rewrite rule in .htaccess)

Now we want to protect file “file1.htm” directory “D” and directory “E”, “F” and virtual path “secret”

1.1 We need to create a .htaccess file under “web” directory

1.2 Open the .htaccess file, we need to add following contents for protecting files

#Protected file
<Files file1.htm>
#Password file path
AuthUserFile /web/.htpasswd
#Message for user to see
AuthName "Password protected"
AuthType Basic
#(If only allow specific user, use "require user username" if allow all valid users use "Require valid-user")
require user username
</Files>

Note: if dealing with multiple files, filesmatch should be used.

<FilesMatch "file1\.htm|file2\.php">
AuthUserFile /web/.htpasswd
AuthName "Password protected"
AuthType Basic
require user username
</FilesMatch>

Tip: Targeting files start with abc or def and end in .php

<FilesMatch "^(abc|def).php$">
AuthUserFile /web/.htpasswd
AuthName "Password protected"
AuthType Basic
require user username
</FilesMatch>

1.3 We add following content to protect directories and the virtual path (We can use this method to protect multiple sub-directories/sub-folders/paths)

#Do the regex check against the URI here, if match, set the "require_auth" var
SetEnvIf Request_URI ^/ resource2\/B\/C\/D require_auth=true
SetEnvIf Request_URI ^/ resource3\/E require_auth=true

#Auth stuff
AuthUserFile /web/.htpasswd
AuthName "Password protected"
AuthType Basic

#Setup a deny/allow
Order Deny,Allow
#Deny from everyone
Deny from all
#except if either of these are satisfied
Satisfy any
#1. a valid authenticated user
Require valid-user
#or 2. the "require_auth" var is NOT set
Allow from env=!require_auth

https://stackoverflow.com/questions/14603568/password-protect-a-specific-url

1.4 We create a .htpasswd file under “web” directory

1.5 Open the .htpasswd file we add following contents (File contains username:hashed user password)

user1:$apr1$MknR4YQ8$ls4RTpNIxaJWyedBK5m030
user2:$apr1$FtfabsVg$NoxTA07DDeGhSOYT9NMLF/

These password can be generated using htpasswd with following command:

$  sudo htpasswd -c /web/.htpasswd user1

(You will need to supply and confirm the password for the user)

$ sudo htpasswd /web/.htpasswd user2

Another way to protect current directory:

e.g. If we want to protect directory “A”

2.1 Creat a .htaccess file under in directory “A”, so we have “/web/resource1/A/.htaccess”

2.2 We add following content to the file

<Files ~ "^.(htaccess|htpasswd)$">
deny from all
</Files>
AuthUserFile /web/resource1/A/.htpasswd
AuthGroupFile /dev/null
AuthName "Please enter your ID and password"
AuthType Basic
require valid-user 
order deny,allow

2.3 We create the .htpasswd file under “/web/resource1/A/”, so we have “/web/resource1/A/.htpasswd”

2.4 We generate password as in step 1.5 (We need to change path from “/web/.htpasswd” to “/web/resource1/A/.htpasswd”)

Now the directory “A” is protected


Tips:

1 We can use online .htpasswd generator to create password for convenience

https://www.htaccesstools.com/htpasswd-generator/

https://www.web2generators.com/apache-tools/htpasswd-generator

https://www.askapache.com/online-tools/htpasswd-generator/

https://htmlstrip.com/htpasswd-generator

https://www.mobilefish.com/services/htpasswd_generator/htpasswd_generator.php

2 We can use online tools to generate .htaccess for convenience

https://www.htaccessredirect.net/

https://hostingfacts.com/htaccess-generator/

https://makeawebsitehub.com/htaccess-generator/

Linux clear intrusion footprint

1 Clear login Log

echo >/var/log/wtmp

2 Clear failed login Log

echo >/var/log/btmp

3 Remove last login echo (Comment out PrintLastLog yes and rstart ssdh service)

vi /etc/ssh/sshd_config

#PrintLastLog yes

service sshd restart OR /etc/init.d/sshd restart

4 Delete bash history

history -c

5 Clear bash history file

rm -rf ~/.bash_history

Kali Linux apt upgrade Error – File has unexpected size

If you have encountered this error, it can be your firewall or other anti virus software blocked the download.

Err:3 http://http.kali.org/kali kali-rolling/main amd64 gzip amd64 1.9-3+b1
   File has unexpected size (220668 != 128516). Mirror sync in progress? [IP: 104.18.103.100 80]
   Hashes of expected file:
 SHA256:e523e6898509a24e6a6424b4fcd4575354a771a4e17ebc244e4a52986a781873
 SHA1:a75db093d2b6f6939be80ab076c78d4e3c1cd54b [weak]
 MD5Sum:679ac9ca25637099514d9f3b1e3ceed5 [weak]
 Filesize:128516 [weak] 
Kali Linux - apt upgrade, File has unexpected size
Kali Linux – apt upgrade, File has unexpected size

A quick workaround can be using https instead of http

Enter this command, then use apt clean, apt update and apt upgrade

echo "deb https://http.kali.org/kali kali-rolling main non-free contrib" > /etc/apt/sources.list

apt clean
apt update
apt upgrade

Ubuntu 18.04.3 LTS /usr/lib/apt/methods/http: relocation error

When updating package list with sudo apt update, following error comes up

/usr/lib/apt/methods/http: relocation error: /usr/lib/x86_64-linux-gnu/libgnutls.so.30: symbol _idn2_punycode_decode version IDN2_0.0.0 not defined in file libidn2.so.0 with link time reference    
Reading package lists… Done
E: Method http has died unexpectedly!
E: Sub-process http returned an error code (127)
E: Method /usr/lib/apt/methods/http did not start correctly

1 Download http://ftp.us.debian.org/debian/pool/main/libi/libidn2/libidn2-0_2.0.5-1_amd64.deb

2 Run sudo dpkg -i path/libidn2-0_2.0.5-1_amd64.deb

Linux Server Tips

Keywords: Linux Server Tips, Linux commands

1 Check last login details, who, when

“w” to check last start up time, currently logged in users

“last” to check logins

“who” Currently logged in users’ details

“lastlog” Users’ last login time

“whoami” Show current username

“finger” Find user information

“id” Current user uid, gid and group information

2 Find ports

netstat -anp | grep 443

lsof -i:443

3 Compile source code

“ar” extract archive, archived installation files

“./configure” configure to fit current systems

“make” compile the source code into binary files e.g. executables

“make install” install the compiled program

4 Linux Directories

“/boot”: Linux kernel, boot configuration etc.

“/bin”: Basic user commands, can be run by users

“/dev”: HDD, Keyboard, Mouse, DVD/CD-ROM device files

“/etc”: Configuration files

“/home”: Users’ working directories (home directory etc.)

“/sbin”: Basic management commands, normally only accessable by administrators

“/usr”: Application, source code files, documents etc.

“/var”: Logs, user mail, process data

“/tmp”: Temporary files

5 Common log files

“/var/log/messages”: Kernel and public messages

“/var/log/cron”: Scheduled Tasks/Crontab

“/var/log/dmesg”: System boot log

“/var/log/maillog”: System mail log

“/var/log/secure”: Access control log

6 Common Linux boot files

“/etc/fstab”: Auto mount on boot

“/etc/initab”: Define default boot level

“/etc/rc.local”: Define custom boot tasks

7 suid, sgid, sticky bit

suid: Run as the owner of the file/binary

sgid: Files created within the folder inherits the folder owner’s permission

sticky bit: Users can only modify, delete wheir own files

8 Check process: “ps”, “top”

9 Check process scheduling: “at”, “crontab”, “batch”, “kill”

10 Extract files

1.*.tarExtract using tar -xvf
2.*.gz Extract using gzip -d OR gunzip
3.*.tar.gz Extract using tar -xzf
4.*.bz2 Extract using bzip2 -d OR bunzip2
5.*.tar.bz2 Extract using tar -xjf
6.*.Z Extract using uncompress
7.*.tar.Z Extract using tar -xZf
8.*.rar Extract using unrar e
9.*.zip Extract using unzip

11 Mounting Windows share

mount -t cifs -o username=windowsusername, password="windowspassword" //ipaddress/folder mnt/linuxtargetfolder

OR

mount.cifs //ipaddress/folder /mnt.linuxtargetfolder -o username="windowsusername", password="windowspassword"

Change hostname in Linux operating systems

Usually hostname is configured during system installating.

Here is how to change the hostame on up to date Linux Linux distributions with systemd installed by default.

To understand what is hostname: https://en.wikipedia.org/wiki/Hostname

Show Current hostname

To show current hostname, we can use this command: hostnamectl

(It will show information including: Static hostname, Icon name, Chassis, Machine ID, Boot ID, Virtualization, Operating System, Kernel, Architecture)

[email protected]:~$ hostnamectl
    Static hostname: ubuntu
          Icon name: computer-vm
            Chassis: vm
         Machine ID: eee8d3edb062470c8afc7e097eb9b962
            Boot ID: 126a5a692e754b56b59dc75f8f045984
     Virtualization: vmware
   Operating System: Ubuntu 19.04
             Kernel: Linux 5.0.0-23-generic
       Architecture: x86-64
Output of hostnamectl
Output of hostnamectl

Change hostname

Command: hostnamectl set-hostname newhostname

[email protected]:~$ hostnamectl set-hostname newubuntu
 [email protected]:~$ hostnamectl
    Static hostname: newubuntu
          Icon name: computer-vm
            Chassis: vm
         Machine ID: eee8d3edb062470c8afc7e097eb9b962
            Boot ID: 126a5a692e754b56b59dc75f8f045984
     Virtualization: vmware
   Operating System: Ubuntu 19.04
             Kernel: Linux 5.0.0-23-generic
       Architecture: x86-64
Output of hostnamectl set-hostname newhostname
Output of hostnamectl set-hostname newhostname

hostnamectl set-hostname will return 0 if the command was successfully executed, if the command failed, it will return non 0 results.

To check if the hostname is changed or not, just execute hostnamectl command again to verify.

Depend on your platform or Linux distro, you may need to modify the command to achieve same results.

Recover from rm

If a running program still has the deleted file open, you can recover the file through the open file descriptor in /proc/[pid]/fd/[num]

e.g. Deleting

$ rm -f /root/selenium/Spider/MySql.Data.dll
$ ll /root/selenium/Spider/MySql.Data.dll
ls: cannot access /root/selenium/Spider/MySql.Data.dll: No such file or directory 

Recovery

Use lsof to check opened processes

$ lsof | grep “/path/to/file”

$ lsof | grep /root/selenium/Spider/MySql.Data.dll in this example

File status will show (deleted) at the end

progname 6210 user 20r REG 8,1 16791251 265368 /path/to/file (deleted)

/proc/6210/fd:Folder of process descriptor

20:File descriptor

Take a note of the PID in the second column and the descriptor number in the forth column, using following command and these information to recover the file

$ cp /proc/6210/fd/20 /path/to/restored/file

How To: Create Apple Time Machine in Open Media Vault (OMV)

1 Create File System (Refer to this guide How To: Create SMB/CIFs/Windows Share in Open Media Vault (OMV))

2 Install OMV Extras from here http://omv-extras.org (For OMV V4, click on here, following the http://omv-extras.org/joomla/index.php/guides and install the plugin http://omv-extras.org/joomla/index.php/omv-plugins-4/4-stable )

3 Back to OMV, click on Plugins and install openmediavault-netatalk

4 Create Shared folder for Apple Time Machine (Refer to this guide How To: Create SMB/CIFs/Windows Share in Open Media Vault (OMV)) The steps are very similar but instead of going to SMB/CIFS, we use Apple Filing Tab this time

Screenshot of Open Media Vault (OMV) Services Tab -> Apple Filing -> Shares
Screenshot of Open Media Vault (OMV) Services Tab -> Apple Filing -> Shares

5 Make sure in the Edit Share window Enable Time Machine support, so that Mac OS can recognize the Share folder as supported Time Machine

Screenshot of Open Media Vault (OMV) Services Tab -> Apple Filing -> Shares
Screenshot of Open Media Vault (OMV) Services Tab -> Apple Filing -> Shares

6 Save settings

7 Click on Settings Tab, enable service, save settings and Apply

Creating a User

Since Mac OS require Time Machine require a password, we will create a user

1 Go to Access Rights Manager Tab and click on User

Screenshot of Open Media Vault (OMV) Access Rights Manager Tab -> User
Screenshot of Open Media Vault (OMV) Access Rights Manager Tab -> User

2 Click on Add button and Create a user and Save

Screenshot of Open Media Vault (OMV) Access Rights Manager Tab -> User -> Add User
Screenshot of Open Media Vault (OMV) Access Rights Manager Tab -> User -> Add User

Configure permission for Time Machine folder

1 Click on Shared Folders

 Screenshot of Open Media Vault (OMV) Access Rights Manager Tab -> Shared Folders
Screenshot of Open Media Vault (OMV) Access Rights Manager Tab -> Shared Folders

2 Click on previously created shared Time Machine folder then click on Privileges button

Screenshot of Open Media Vault (OMV) Access Rights Manager Tab -> Shared Folders
Screenshot of Open Media Vault (OMV) Access Rights Manager Tab -> Shared Folders

3 Tick Read/Write permission for desired user for Time Machine

Screenshot of Open Media Vault (OMV) Access Rights Manager Tab -> Shared Folders -> Privileges
Screenshot of Open Media Vault (OMV) Access Rights Manager Tab -> Shared Folders -> Privileges

4 Now if we open our Mac, click on Top left corner (Apple icon) -> System preferences -> Time Machine, configure the settings to use the Time Machine we have just created

5 From now on, it will backup your everything from Mac OS, with hourly, weekly and monthly backup routine

How To: Create SMB/CIFs/Windows Share in Open Media Vault (OMV)

Create File system

1 Make sure you have added disk

Screenshot of Open Media Vault (OMV) Storage Tab - Disks
Screenshot of Open Media Vault (OMV) Storage Tab – Disks

2 Click on File Systems

Screenshot of Open Media Vault (OMV) Storage Tab - File Systems
Screenshot of Open Media Vault (OMV) Storage Tab – File Systems

3 Format the disk/partition

Screenshot of Open Media Vault (OMV) Storage Tab -> File Systems -> Create Button
Screenshot of Open Media Vault (OMV) Storage Tab -> File Systems -> Create Button

4 Mount the file system

Screenshot of Open Media Vault (OMV) Storage Tab -> File Systems -> Mount Button
Screenshot of Open Media Vault (OMV) Storage Tab -> File Systems -> Mount Button

Create Windows Share

1 Click on SMB/CIFS from Services tab

Screenshot of Open Media Vault (OMV) Storage Tab -> File Systems -> Mount Button
Screenshot of Open Media Vault (OMV) Storage Tab -> File Systems -> Mount Button

2 Click on Shares then click on Add button

Screenshot of Open Media Vault (OMV) Services Tab -> SMB/CIFS -> Shares -> Add
Screenshot of Open Media Vault (OMV) Services Tab -> SMB/CIFS -> Shares -> Add

3 Click on + button for Shared folder

Screenshot of Open Media Vault (OMV) Services Tab -> SMB/CIFS -> Shares -> Add
Screenshot of Open Media Vault (OMV) Services Tab -> SMB/CIFS -> Shares -> Add

4 Follow the screenshot

Screenshot of Open Media Vault (OMV) Services Tab -> SMB/CIFS -> Shares -> Add -> Add Shared Folder
Screenshot of Open Media Vault (OMV) Services Tab -> SMB/CIFS -> Shares -> Add -> Add Shared Folder

5 Change Public to Only guests if you only need a public share without password (Means anyone on the same LAN can access and modify the shared folder)

Screenshot of Open Media Vault (OMV) Services Tab -> SMB/CIFS -> Shares -> Add
Screenshot of Open Media Vault (OMV) Services Tab -> SMB/CIFS -> Shares -> Add

6 Save the settings

7 Finally, Click on Services Tab, click on SMB/CIFS, then enable and Apply the new settings

Screenshot of Open Media Vault (OMV) Services Tab -> SMB/CIFS -> Settings
Screenshot of Open Media Vault (OMV) Services Tab -> SMB/CIFS -> Settings

8 Now if you point your file explorer to Open Media Vault’s name or IP address, you should be able to access the folder we have created and make changes to it