Wednesday, December 22, 2021

How to upgrade Debian 9 Stretch to Linux Debian 10 Buster

 Introduction

Debian is a popular version, or fork, of the Linux operating system.

The most recently released version is Debian 10, codenamed Buster. The previous version Debian 9 Stretch is officially supported until June 2022. The newer version offers a longer support period and new features, so it is highly recommended to upgrade if running Debian 9.

This tutorial will walk you through how to upgrade from Debian 9 (Stretch) to Debian 10 (Buster).

Prerequisites

  • A system running Debian 9
  • A Debian user account with sudo privileges
  • Access to a command line/terminal window (CtrlAltF2)
  • A recent snapshot (backup) of the system

Upgrade to Debian Version 10 Buster

Make sure to back up your system and data prior to upgrading. That way, if anything breaks during the upgrade, you can easily restore your system configuration.

Step 1: Update Package Manager and Repositories

By default, Debian 9 refers to the repositories designated for Stretch.

1. First, create a backup copy of the /etc/apt/sources.list file:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

2. Then, open the file for editing (we are using GNU nano for text editing):

sudo nano /etc/apt/sources.list

3. Change all the references in this file from Stretch to Buster. The entries should appear as follows:

deb http://deb.debian.org/debian debian buster main

deb http://deb.debian.org/debian buster-updates main

deb http://deb.debian.org/debian buster/updates main
Debian Version 10 Buster successful install

4. Save the file using the keyboard combination Ctrl+and Exit by pressing Ctrl+X.

You can also check for FTP download mirrors to replace the web address .

Step 2: Upgrade from Debian 9 to Debian 10

1. Update the local package lists:

sudo apt-get update

2. Next, run updates on the software packages to prepare for the operating system upgrade:

sudo apt-get upgrade

3. Finally, run the distribution upgrade:

sudo apt-get dist-upgrade

4. Once the process completes, reboot the system to boot into Debian 10 Buster:

sudo reboot

Step 3: Verify Debian 10 Update

You can use the hostnamectl command to display the OS version with the command:

hostnamectl

The Operating System field should display Debian GNU/Linux 10 (buster).

The output should appear as:

PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

(Optional) Step 4: Remove Outdated Packages

After the upgrade to Debian 10, your system might have outdated packages and dependencies which are no longer necessary.

To remove outdated packages, run the --purge autoremove command:

sudo apt --purge autoremove

Conclusion

By following the directions above, you should have successfully upgraded from Debian 9 to Debian 10.


Source: https://phoenixnap.com/kb/how-to-upgrade-debian-9-stretch-to-debian-10-buster

Thursday, December 9, 2021

HOW TO USE NMAP TO SCAN ANY PORT [UDP/TCP] [2021]

 We can use several NMAP command flags to scan specific TCP and UDP ports, as seen in Table 1.1 below.

DescriptionNMAP Command Flag
Scan all ports-p “*”
Scan top ports–top-ports [number]
Scan specific ports-p [port]
Scan ports by name-p [name]
Scan ports by protocol -p U:[UDP ports],T:[TCP ports]
Perform a fast scan-F
Perform a sequential port scan-r
Table 1.1: [NMAP Scan Any Port]: Flags summary

Let’s discuss each NMAP flag in the above table, their syntax, and use case. 

Scan All Ports With NMAP

FLAG: -p “*”

Command syntax: nmap -p “*” [Target] 

Description: Perform NMAP scan for all 65,535 TCP/UDP ports. This command do does not mandate elevated privileges.

The NMAP Scan All Ports flag will produce a scan for all 65,535 TCP and UDP ports on a target machine and output a list of open ports as well as the services running on a specific port [Figure 1.1].

[NMAP Scan Any Port]: Scan All Ports example. Source: nudesystems.com
Figure 1.1: [NMAP Scan Any Port]: Scan All Ports example. 

Make sure your wildcard [*] statement is enclosed in quotes (“”); otherwise, NMAP will throw a syntax error, as seen in Figure 1.2 below.

[NMAP Scan Any Port]: Use of wrong wildcard quotes. Source: nudesystems.com
Figure 1.2: [NMAP Scan Any Port]: Use of wrong wildcard quotes.

Scan Top Ports With NMAP

Flag: –top-ports [Number]

Command Syntax: nmap –top-ports [Number] [Target] 

Description: Perform an NMAP scan by specifying the number of top-ranked ports to scan. 

The NMAP Scan Top Ports flag allows us to define the number of top-ranked ports to scan. For example, let’s assume we want to scan the top commonly used 50 ports. 

The NMAP command for scanning the top commonly used ports is shown below with the output in Figure 1.3.

nmap – top-ports 10000 172.16.121.134

NOTE: Remember to replace the IP address in my example with your target IP address. 

[NMAP Scan Any Port]: Scan top 50 most commonly used ports. Source: nudesystems.com
Figure 1.3: [NMAP Scan Any Port]: Scan top 50 most commonly used ports.

Let’s perform another scan, this time for the top 10000 commonly used ports on a target machine using the NMAP command below and the output shown in Figure 1.4

nmap – top-ports 10000 172.16.121.134
[NMAP Scan Any Port]: Scan the top 10000 top ports. Source: nudesystems.com
Figure 1.4: [NMAP Scan Any Port]: Scan the top 10000 top ports.

NOTE: Top commonly used ports e.g., 10000, do not refer to the range of ports starting with, e.g., 1 to 10000. 

Scan Specific Ports With NMAP

Flag: -p

Command Syntax: nmap -p [Port] [Target]

Description: Perform an NMAP scan for a specified port or range of ports.

The Scan Specific Port(s) flag instructs NMAP to perform a scan on a single port or multiple ports [separated by a comma and no spaces] on a specified target. Check out the list of the most commonly hacked ports for your reference.

Let’s practice this method by instructing NMAP to scan a single port, e.g., port 80, using the following command with the output shown in Figure 1.5. 

nmap -p 80 172.16.121.134
[NMAP Scan Any Port]: Scan port 80. Source: nudesystems.com
Figure 1.5: [NMAP Scan Any Port]: Scan port 80.

Next, let’s instruct NMAP to scan multiple or a range of ports, e.g., 25, 53, 80-500 using the command below with the output seen in Figure 1.6.

nmap -p 25,53,80-500 172.16.121.134
[NMAP Scan Any Port]: Scan port 80. Source: nudesystems.com
Figure 1.6: [NMAP Scan Any Port]: Scan port 80.

Scan Ports By Name With NMAP

Flag: -p

Command Syntax: nmap -p [Port name(s)] [Target]

Description: Perform an NMAP scan for a specified port or ports.

The NMAP Scan Ports By Name uses the same -p flag as used in the previous method. Here we will instruct NMAP to scan based on the port name [http,ftp,smtp] and not a number. 

NOTE: When scanning multiple ports by name, make sure you separate the name with a comma and no spaces.

The port name must match the exact name as specified in the nmap-service file.

On Windows, you can find the nmap-service file in the c:\Program Files\Nmap\ folder. On Linux/UNIX, the nmap-service file is generally located in /usr/share/nmap folder [Kali Linux 2021].

Alternatively, you can access the list of NMAP port names included in the nmap-service file HERE.

Let’s instruct NMAP to scan the FTP [21] and HTTP [80] ports on a target using the following command and the output shown in Figure 1.7.

nmap -p ftp,http 172.16.121.134
[NMAP Scan Any Port]: Scan FTP and HTTP ports using the port name. Source: nudesystems.com
Figure 1.7: [NMAP Scan Any Port]: Scan FTP and HTTP ports using the port name.

This NMAP command flag also supports wildcard. Let’s filter all the FTP ports using the command below with the output shown in Figure 1.8.

nmap -p ftp* 172.16.121.134
[NMAP Scan Any Port]: Scan FTP and HTTP ports using the port name wildcard. Source: nudesystems.com
Figure 1.8: [NMAP Scan Any Port]: Scan FTP and HTTP ports using the port name wildcard.

Scan Ports By Protocol With NMAP

Flag: -p 

Additional Flags: U:[UDP Ports],T:[TCP Ports]

Command Syntax: nmap -p U:[UDP Ports],T:[TCP Ports] [Target]

Description: Search for a specific protocol and port combination. 

As in previous examples, the Scan Ports By Protocol method uses the -p option followed by T: [for TCP ports] and U: [for UDP ports] flags. Simply said, we will instruct NMAP to scan for a specific TCP and UDP port on a target machine in a single command line. 

Let’s execute a scan using this method for UDP port 53 [DNS] and TCP port 25 [SMTP], as shown in Figure 1.9 below. Pay attention to the NMAP WARNING message highlight in yellow.

 [NMAP Scan Any Port]: Scan ports by protocol. Source: nudesystems.com
Figure 1.9: [NMAP Scan Any Port]: Scan ports by protocol.

By default, NMAP scans only the TCP ports. We need to instruct NMAP to check the UDP ports as well. To do that, we need to enforce our command with the additional -sU and -sT flags.

So let’s run the command again, this time including the additional flags as shown below with the output in Figure: 1.10.

nmap -sU -sT -p U:53,T:25 172.16.121.134
[NMAP Scan Any Port]: Scan ports by protocol. Source: nudesystems.com
Figure 1.10: [NMAP Scan Any Port]: Scan ports by protocol.

Voilà! Both TCP port 25 and UDP port 53 were successfully scanned on the target machine. 

Fast Scan Flag With NMAP

FLAG: -F

Command Syntax: nmap -F [Target] 

Description: Perform a fast scan of only 100 most commonly used ports. Do not mandate elevated privileges.

As explained in the previous sections, NMAP will scan by default only the commonly used 1000 ports. 

The NMAP Fast Scan Flag will reduce the number of ports scanned from 1000 to 100 [commonly used ports] and drastically improve the time per scan while still providing us valuable information on the open ports in numerical order [Figure 1.11].

[NMAP Scan Any Port]: Fast Scan Flag. Source: nudesystems.com
Figure 1.11: [NMAP Scan Any Port]: Fast Scan Flag.

But wait! There’s a catch.

Each time NMAP shows an output will automatically sort the final result. We can use the additional flag -v [verbose mode] and see the sequential port scan in real-time [Figure 1.12].

[NMAP Scan Any Port]: Example of a sequential port scan in verbose mode. Source: nudesystems.com
Figure 1.12: [NMAP Scan Any Port]: Example of a sequential port scan in verbose mode.

That’s it. By now, you should know how to use NMAP to scan any TCP and UDP port(s) on a target computer.

Source: https://nudesystems.com/how-to-use-nmap-to-scan-any-port-udp-tcp-2021/


Tuesday, June 22, 2021

Querying SQL Server Agent Job History Data

Problem

Often times we are tasked with having to programmatically come with a list of durations per SQL Server Agent Job to trend the run times and order the results by date. Unfortunately Microsoft has not made that very easy in the way the data is stored in the system tables in the MSDB database. In this tip I will explain how to use the system tables to get the data into the correct format for dates and job durations.

Solution

SQL Server stores job information in the MSDB system tables. The table that stores historical job executions is msdb.dbo.sysjobhistory and the date/time information as well as the duration of the job execution is stored a little different than what you will see in most system tables. Let's dive into the tables to learn how to perform the calculations.

MSDB.dbo.sysjobhistory Table - run_date and run_time Columns

In the msdb.dbo.sysjobhistory table, the date (run_date column) and time (run_time column) are stored as two different columns and the columns are an INT data type as seen in the screenshot below.

msdb.dbo.sysjobhistory table

If you were to query the sysjobhistory (joined to sysjobs of course) this is what the raw data would look like. Note run_date appears to be in YYYYMMDD format but the time is a little trickier. It is in format HHMMSS and not always 6 numbers long as you can see in this example the times are 3:30 AM represented by 33000 in the run_time column.

Original Columns with DATE/TIME from the MSDB.dbo.sysjobhistory table

The result set in the picture above was generated by running the following T-SQL script:

select 
 j.name as 'JobName',
 run_date,
 run_time
From msdb.dbo.sysjobs j 
INNER JOIN msdb.dbo.sysjobhistory h 
 ON j.job_id = h.job_id 
where j.enabled = 1  --Only Enabled Jobs
order by JobName, run_date, run_time desc

This can be very confusing when trying to analyze these results and more importantly trying to order results by date and time, which as a common practice for DBA's. There are many ways to get the date/time into a true DATETIME format and I have seen some very creative scripts over the years. Starting in SQL Server 2005 there is an system scalar function located in MSDB that will convert this for you. I say it is undocumented as I could not find a BOL article for it. The function name is MSDB.dbo.agent_datetime(run_date, run_time) and will return a nicely formatted DATETIME column that is much easier to use programmatically. Below is the same query as the first one above with just an additional column that is a call to this function.

select 
 j.name as 'JobName',
 run_date,
 run_time,
 msdb.dbo.agent_datetime(run_date, run_time) as 'RunDateTime'
From msdb.dbo.sysjobs j 
INNER JOIN msdb.dbo.sysjobhistory h 
 ON j.job_id = h.job_id 
where j.enabled = 1  --Only Enabled Jobs
order by JobName, RunDateTime desc

Below are the results that this query produces and the nicely formatted DATETIME column at the end. This makes sorting easy as well as date range queries against your SQL Agent Jobs.

New Columns with DATE/TIME formatted data from the msdb.dbo.sysjobhistory table 

Tuesday, June 8, 2021

How to monitor backup and restore progress in SQL Server

Below script can give us a summary of current backups and restores which are happening on the server:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SELECT r.session_id AS [Session_Id]
    ,r.command AS [command]
    ,CONVERT(NUMERIC(6, 2), r.percent_complete) AS [% Complete]
    ,GETDATE() AS [Current Time]
    ,CONVERT(VARCHAR(20), DATEADD(ms, r.estimated_completion_time, GetDate()), 20) AS [Estimated Completion Time]
    ,CONVERT(NUMERIC(32, 2), r.total_elapsed_time / 1000.0 / 60.0) AS [Elapsed Min]
    ,CONVERT(NUMERIC(32, 2), r.estimated_completion_time / 1000.0 / 60.0) AS [Estimated Min]
    ,CONVERT(NUMERIC(32, 2), r.estimated_completion_time / 1000.0 / 60.0 / 60.0) AS [Estimated Hours]
    ,CONVERT(VARCHAR(1000), (
            SELECT SUBSTRING(TEXT, r.statement_start_offset / 2, CASE
                        WHEN r.statement_end_offset = - 1
                            THEN 1000
                        ELSE (r.statement_end_offset - r.statement_start_offset) / 2
                        END) 'Statement text'
            FROM sys.dm_exec_sql_text(sql_handle)
            ))
FROM sys.dm_exec_requests r
WHERE command like 'RESTORE%'
or  command like 'BACKUP%'

Source:

https://blog.sqlauthority.com/2017/03/12/get-status-running-backup-restore-sql-server-interview-question-week-113/