Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check sudo permissions before call openvas. #87

Merged
merged 6 commits into from
Jun 26, 2019

Conversation

jjnicola
Copy link
Member

@jjnicola jjnicola commented Jun 25, 2019

Check if the user running ospd-openvas has permissions to call openvas
with sudo and without password.
Depending on it, it will run openvas with sudo or without it.

How to give the user permission to call openvas with sudo and without password:
$ sudo visudo
Add at the end of the file
<user> ALL = NOPASSWD: /<install/path/sbin/openvas

Check if the user running ospd-openvas has permissions to call openvas
with sudo and without password.
Depending on it, it will run openvas with sudo or without it.
@jjnicola jjnicola added the Work in progress Waiting for Feature Specification validation. label Jun 25, 2019
Copy link
Contributor

@bjoernricks bjoernricks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sudo_available I would propose to use the property decorator https://docs.python.org/3/library/functions.html#property

It allows to use calling a method when a property on the class instance is accessed.

except subprocess.CalledProcessError as e:
logger.debug('It was not possible to call openvas with sudo. '
'The scanner will run as non-root user. Reason %s', e)
return _sudo_available
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return _sudo_available
self._sudo_available = False

try:
result = subprocess.check_call(
['sudo', '-n', 'openvas', '-s'], stdout=subprocess.PIPE
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
)
)
self._sudo_available = True

if result == 0:
_sudo_available = True

return _sudo_available
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remove these lines above.

""" Checks that sudo is available and set the global var. """
_sudo_available = False
try:
result = subprocess.check_call(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check_call doesn't return the returncode. If the returncode is != 0 it raises an exception https://docs.python.org/3/library/subprocess.html#subprocess.check_output So no need to check the result.

@@ -715,6 +716,23 @@ def get_detection_vt_as_xml_str(

return tostring(_detection).decode('utf-8')

def sudo_check(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def sudo_check(self):
@property
def sudo_available(self):

@@ -715,6 +716,23 @@ def get_detection_vt_as_xml_str(

return tostring(_detection).decode('utf-8')

def sudo_check(self):
""" Checks that sudo is available and set the global var. """
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
""" Checks that sudo is available and set the global var. """
""" Checks that sudo is available and set the global var. """
if self._sudo_available is not None:
return self._sudo_available

@@ -715,6 +716,23 @@ def get_detection_vt_as_xml_str(

return tostring(_detection).decode('utf-8')

def sudo_check(self):
""" Checks that sudo is available and set the global var. """
_sudo_available = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_sudo_available = False

""" Checks that sudo is available and set the global var. """
_sudo_available = False
try:
result = subprocess.check_call(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result = subprocess.check_call(
subprocess.check_call(

if self.sudo_available:
cmd = ['sudo', 'openvas', '--scan-stop', scan_id]
else:
cmd = ['openvas', '--scan-stop', scan_id]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines can be simplified by something like:

cmd = ['openvas', '--scan-stop', scan_id]
if self.sudo_available:
    cmd = ['sudo'] + cmd

cmd = ['sudo', 'openvas', '--scan-start', openvas_scan_id]
else:
cmd = ['openvas', '--scan-start', openvas_scan_id]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified as above.

Maybe it would even better to extract calling openvas into an own method/function e.g.

def call_openvas(args, use_sudo=False, niceness=None):
   cmd = args

   if use_sudo:
       cmd = ['sudo'] + cmd

   if niceness is not None:
      cmd = ['nice', ...] + cmd

    try:
        subproces. ....
    ...

but i am not 100 percent sure about that because we are calling different functions of subrocess at the moment with slightly different arguments.

@codecov
Copy link

codecov bot commented Jun 26, 2019

Codecov Report

Merging #87 into master will decrease coverage by 0.22%.
The diff coverage is 37.5%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #87      +/-   ##
==========================================
- Coverage    72.1%   71.88%   -0.23%     
==========================================
  Files           4        4              
  Lines         864      875      +11     
==========================================
+ Hits          623      629       +6     
- Misses        241      246       +5
Impacted Files Coverage Δ
ospd_openvas/wrapper.py 59.02% <37.5%> (-0.09%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5cbc020...8594ead. Read the comment docs.

@codecov
Copy link

codecov bot commented Jun 26, 2019

Codecov Report

Merging #87 into master will decrease coverage by 0.15%.
The diff coverage is 44.44%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #87      +/-   ##
==========================================
- Coverage    72.1%   71.94%   -0.16%     
==========================================
  Files           4        4              
  Lines         864      877      +13     
==========================================
+ Hits          623      631       +8     
- Misses        241      246       +5
Impacted Files Coverage Δ
ospd_openvas/wrapper.py 59.15% <44.44%> (+0.05%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5cbc020...d75ef15. Read the comment docs.

@jjnicola jjnicola merged commit 597abf5 into greenbone:master Jun 26, 2019
@jjnicola jjnicola deleted the sudo-opt branch June 26, 2019 12:36
@jjnicola jjnicola removed the Work in progress Waiting for Feature Specification validation. label Jun 28, 2019
ArnoStiefvater pushed a commit to ArnoStiefvater/ospd-openvas that referenced this pull request Oct 25, 2021
Fix file name of ospd-scanner INSTALL guide
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants