Python | System Gain and Compliance Reports Using Lynis

| | | | | | | | | | | | | | | | | | | | | | | | | | | | |

Because Lynis is flexible, it is used for several different purposes. Typical use cases for Lynis include:

  • Security Audit
  • Compliance Testing (e.g. PCI, HIPAA, SOx)
  • Penetration Testing
  • Vulnerability detection
  • System hardening

System protection — it protects your system from potential threats and vulnerabilities. Lynis can be used to generate a detailed report on various threats and vulnerabilities on your system. The user or system administrator can then take the necessary action to secure the system.

Lynis reports are difficult to read and usually contain a lot of information. Therefore, we use Bash and Python scripts to parse the report, extract relevant information from the report, such as warnings, suggestions, and save them to an Excel file as a report.

Prerequisites for Lynis —

    You must be running a Linux / Unix based OS such as Ubuntu, Mac Os or any other Linux distribution.

  • Install Lynis on your system by cloning github repository: https://github.com/CISOfy/lynis
  • Install the pandas library using the command sudo pip3 install pandas .
  • After you have installed Lynis on your system, navigate to the Lynis directory where you will find a set of files along with an executable named Lynis.
  • Use a bash script (code below) to extract relevant information such as the warning and suggestions given in the lynis report. create a file called run.sh and copy, paste the bash code into that file and type: sudo ./run.sh to run the bash script.
  • Run the Python script (the code is given below) to clean and parse the extracted data and output the relevant information as an Excel file.

Below are Bash and Python scripts —

Script Bash:

#! / bin / bash


# script to clean / parse the report file and
# extract the relevant details and run
# Python script to display details on the server.

echo "running ......"

echo ""

sudo . / lynis audit system --quick


# execute warnings. sudo ./warnings.sh

echo "Generating warnings"

echo ""

echo "warnings are:"

echo ""

sudo cat / var / log / lynis-report . dat | grep warning | sed - e "s / warning [] = // g"

sudo cat / var / log / lynis-report . dat | grep warning | sed - e "s / warning [] = // g" | cat & gt; warnings.txt

echo ""

echo "warnings generated"

echo "output file: warnings.txt"

sudo chmod 755 warnings .txt


# execute suggestions. sudo ./suggestions.sh

echo "Generating suggestions"

echo ""

echo "suggestions are:"

echo ""

sudo cat / var / log / lynis-report . dat | grep suggestion | sed - e "s / suggestion [] = // g"

sudo cat / var / log / lynis-report . dat | grep suggestion | sed - e "s / suggestion [] = // g" | cat & gt; suggestions.txt

echo ""

echo "suggestions generated"

echo "output file: suggestions.txt"

sudo chmod 755 suggestions .txt


# execute packages. sudo ./packages.sh

echo "Generating packages"

echo ""

echo "packages are:"

echo ""

sudo cat / var / log / lynis-report . dat | grep installed_package | sed - e "s / installed_package [] = // g"

sudo cat / var / log / lynis-report . dat | grep installed_package | sed - e "s / installed_package [] = // g" | cat & gt; packages.txt

echo ""

echo "packages generated"

sudo chmod 755 packages.txt


# execute shells. sudo ./shells.sh

echo "Generating avaliable shells"

echo ""

echo "shells are:"

echo ""

sudo cat / var / log / lynis-report . dat | grep available_shell | sed - e "s / available_shell [] = // g"

sudo cat / var / log / lynis-report . dat | grep available_shell | sed - e "s / available_shell [] = // g" | cat & gt; shells.txt

echo ""

echo "shells generated"

echo "output file: shells .txt "

sudo chmod 755 shells.txt

Python script:

# import libraries

import pandas as pd

from pandas import ExcelWriter

import os


# function to get data.

def get_data ():

warnings = open ( ’ warnings.txt’ , ’r’ )

suggestions = open ( ’suggestions.txt’ , ’r’ )

packages = open ( ’packages.txt’ , ’ r’ )

shells = open ( ’shells.txt’ , ’ r’ )

warn_data = warnings.readlines ()

sugg_data = suggestions.readlines ()

pack_dat a = packages.read ()

shell_data = shells.readlines ()

return warn_data, sugg_data, pack_data, shell_data

def clean_data ():

warn, sugg, pack, shell = get_data ()

warn_clean = []

for line in warn:

warn_clean.append (line.split ( ’|’ ))

for i in range ( len (warn_clean)):

warn_clean [i] = warn_clean [i] [: 2 ]

# print (warn_clean [i])

sugg_clean = []

for line in sugg:

sugg_clean.append (line.split ( ’|’ ))

for i in range ( len (sugg_clean)):

sugg_clean [i] = sugg_clean [i] [: 2 ]

# print (sugg_clean [i])

pack_clean = []

pack = pack.split ( ’|’ )

pack_clean = pack

del pack_clean [ 0 ]

shell_clean = []

for i in range ( len (shell)):

shell_clean.append (shell [i] .rstrip ( ’’ ))

# print (shell_clean [i])

return warn_clean, sugg_clean, pack_clean, shell_clean

def convert_to_excel ():

warnings, suggestions, packages , shells = clean_data ()

try :

os.mkdir ( ’outputs’ )

except (Exception):

pass

os.chdir ( ’ outputs’ )

warn_packages = []

warn_text = []

for i in range ( len (w arnings)):

warn_packages.append (warnings [i] [ 0 ])

for i in range ( len (warnings)):

warn_text.append (warnings [i] [ 1 ])

print (warn_packages, warn_text)

warn = pd.DataFrame ()

warn [ ’Packages’ ] = warn_packages

warn [ ’warnings’ ] = warn_text

# warn.to_excel (& # 39; warnings.xlsx & # 39 ;, index = False)

writer = ExcelWriter ( ’warnings.xlsx’ )

warn.to_excel (writer, ’ report1’ , index = False )

workbook = writer.book

worksheet = writer. sheets [ ’report1’ ]

# Invoice information columns

worksheet.set_column ( ’A: A’ , 15 )

# State column

worksheet.set_column ( ’B: B’ , 45 )

# Zip code

# worksheet.set_column (& # 39; F: F & # 39 ;, 10)

writer.save ()

sugg_packages = []

sugg_text = []

for i in range ( len (suggestions)):

sugg_packages.append (suggestions [i] [ 0 ])

for i in range ( len (suggestions)):

sugg_text.append (suggestions [i] [ 1 ])

# print (sugg_packages, sugg_text)

suggestive = pd.DataFrame ()

sugg [ ’Packages’ ] = sugg_packages

[ ’suggestions’ ] = sugg_text

writer1 = ExcelWriter ( ’suggestions.xlsx’ )

sugg.to_excel (writer1, ’report2’ , index = False )

workbook = writer1.book

worksheet = writer1.sheets [ ’report2’ ]

# Invoice information columns

worksheet.set_column ( ’A: A’ , 25 )

# State column

worksheet.set_column ( ’B: B’ , 120 )

# Postal code

# worksheet.set_column (& # 39; F: F & # 39 ;, 10)

writer1.save ()

pack_data = pd.DataFrame ()

pack_data [ ’ Packages’ ] = packages

writer1 = ExcelWriter ( ’ packages.xlsx’ )

pack_data.to_excel (writer1, ’report3’ , index = False )

workbook = writer1.book

worksheet = writer1 .sheets [ ’report2’ ]

# Invoice information columns

worksheet.set_column ( ’A: A’ , 25 )

# State column

worksheet.set_column ( ’B: B’ , 120 )

# Zip code

# worksheet.set_column (& # 39; F: F & # 39 ;, 10)

writer1.save ()

Python | System Gain and Compliance Reports Using Lynis __del__: Questions

How can I make a time delay in Python?

5 answers

I would like to know how to put a time delay in a Python script.

2973

Answer #1

import time
time.sleep(5)   # Delays for 5 seconds. You can also use a float value.

Here is another example where something is run approximately once a minute:

import time
while True:
    print("This prints once a minute.")
    time.sleep(60) # Delay for 1 minute (60 seconds).

2973

Answer #2

You can use the sleep() function in the time module. It can take a float argument for sub-second resolution.

from time import sleep
sleep(0.1) # Time in seconds

Python | System Gain and Compliance Reports Using Lynis __del__: Questions

How to delete a file or folder in Python?

5 answers

How do I delete a file or folder in Python?

2639

Answer #1


Path objects from the Python 3.4+ pathlib module also expose these instance methods:

Shop

Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$
Gifts for programmers

Best laptop for Zoom

$499
Gifts for programmers

Best laptop for Minecraft

$590

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically