-
Notifications
You must be signed in to change notification settings - Fork 44
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
Javahashes #341
Merged
Merged
Javahashes #341
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
53c7ea1
added jar plugin
393885e
added jar plugin test, fixed name in jar util test case
bc3fef7
fixed whitespace
2a06638
fixed whitespace
9ef0a03
fixed whitespace issues
f24f91e
fixed whitespace issues
0bea3e9
fixed test case name, added container test case
84d1699
Merge branch 'master' into javahashes
sastryduri b4077e1
Merge branch 'master' of github.com:cloudviz/agentless-system-crawler…
58e9c8a
Merge branch 'javahashes' of github.com:sastryduri/agentless-system-c…
d59f7dd
adding support for getting mmaped files in process feature with get_m…
sahilsuneja1 b44043a
fix vm and host crawler with new mmapfiles process feature field
sahilsuneja1 b3c8eab
Merge branch 'javahashes' of github.com:sastryduri/agentless-system-c…
ea7f39e
fixed jst host crawler usage in test plugins
8c283b9
updated test case
a05c09c
test case fix
c12e200
added a new test case
c9b3f88
updated test case
c201e03
Merge branch 'master' into javahashes
sahilsuneja1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[Core] | ||
Name = jar_container | ||
Module = jar_container_crawler | ||
|
||
[Documentation] | ||
Author = IBM | ||
Version = 0.1 | ||
Description = Produces md5 hashes for jar files and class files in jars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import logging | ||
|
||
import utils.dockerutils | ||
import utils.misc | ||
from icrawl_plugin import IContainerCrawler | ||
from utils.jar_utils import crawl_jar_files | ||
from utils.namespace import run_as_another_namespace | ||
|
||
logger = logging.getLogger('crawlutils') | ||
|
||
|
||
class JarContainerCrawler(IContainerCrawler): | ||
|
||
def get_feature(self): | ||
return 'jar' | ||
|
||
def crawl( | ||
self, | ||
container_id=None, | ||
avoid_setns=False, | ||
root_dir='/', | ||
exclude_dirs=[ | ||
'/boot', | ||
'/dev', | ||
'/proc', | ||
'/sys', | ||
'/mnt', | ||
'/tmp', | ||
'/var/cache', | ||
'/usr/share/man', | ||
'/usr/share/doc', | ||
'/usr/share/mime'], | ||
**kwargs): | ||
inspect = utils.dockerutils.exec_dockerinspect(container_id) | ||
state = inspect['State'] | ||
pid = str(state['Pid']) | ||
logger.debug('Crawling jars for container %s' % container_id) | ||
|
||
if avoid_setns: | ||
rootfs_dir = utils.dockerutils.get_docker_container_rootfs_path( | ||
container_id) | ||
exclude_dirs = [utils.misc.join_abs_paths(rootfs_dir, d) | ||
for d in exclude_dirs] | ||
return crawl_jar_files( | ||
root_dir=utils.misc.join_abs_paths(rootfs_dir, root_dir), | ||
exclude_dirs=exclude_dirs, | ||
root_dir_alias=root_dir) | ||
else: # in all other cases, including wrong mode set | ||
return run_as_another_namespace(pid, | ||
['mnt'], | ||
crawl_jar_files, | ||
root_dir, | ||
exclude_dirs, | ||
None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[Core] | ||
Name = jar_host | ||
Module = jar_host_crawler | ||
|
||
[Documentation] | ||
Author = IBM | ||
Version = 0.1 | ||
Description = Produces md5 hashes for jar files and class files in jars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from icrawl_plugin import IHostCrawler | ||
from utils.jar_utils import crawl_jar_files | ||
|
||
|
||
class JarHostCrawler(IHostCrawler): | ||
|
||
def get_feature(self): | ||
return 'jar' | ||
|
||
def crawl( | ||
self, | ||
root_dir='/', | ||
exclude_dirs=[ | ||
'/boot', | ||
'/dev', | ||
'/proc', | ||
'/sys', | ||
'/mnt', | ||
'/tmp', | ||
'/var/cache', | ||
'/usr/share/man', | ||
'/usr/share/doc', | ||
'/usr/share/mime'], | ||
**kwargs): | ||
return crawl_jar_files(root_dir=root_dir, | ||
exclude_dirs=exclude_dirs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import fnmatch | ||
import logging | ||
import os | ||
import re | ||
import hashlib | ||
import zipfile | ||
|
||
from utils.features import JarFeature | ||
|
||
logger = logging.getLogger('crawlutils') | ||
|
||
|
||
def crawl_jar_files( | ||
root_dir='/', | ||
exclude_dirs=[], | ||
root_dir_alias=None, | ||
accessed_since=0): | ||
|
||
if not os.path.isdir(root_dir): | ||
return | ||
|
||
saved_args = locals() | ||
logger.debug('crawl_jar_files: %s' % (saved_args)) | ||
|
||
assert os.path.isdir(root_dir) | ||
if root_dir_alias is None: | ||
root_dir_alias = root_dir | ||
exclude_dirs = [os.path.join(root_dir, d) for d in | ||
exclude_dirs] | ||
exclude_regex = r'|'.join([fnmatch.translate(d) | ||
for d in exclude_dirs]) or r'$.' | ||
|
||
# walk the directory hierarchy starting at 'root_dir' in BFS | ||
# order | ||
|
||
for (root_dirpath, dirs, files) in os.walk(root_dir): | ||
dirs[:] = [os.path.join(root_dirpath, d) for d in | ||
dirs] | ||
dirs[:] = [d for d in dirs | ||
if not re.match(exclude_regex, d)] | ||
files = [os.path.join(root_dirpath, f) for f in | ||
files] | ||
files = [f for f in files | ||
if not re.match(exclude_regex, f)] | ||
|
||
for fpath in files: | ||
if not fpath.endswith('.jar'): | ||
continue | ||
feature = _crawl_jar_file(root_dir, fpath, root_dir_alias) | ||
if feature: | ||
yield (feature.path, feature, 'jar') | ||
|
||
|
||
# crawl a single file | ||
def _crawl_jar_file( | ||
root_dir, | ||
fpath, | ||
root_dir_alias, | ||
): | ||
if not fpath.endswith('.jar'): | ||
return | ||
|
||
hashes = [] | ||
with zipfile.ZipFile(fpath, 'r') as zf: | ||
for info in zf.infolist(): | ||
if not info.filename.endswith('.class'): | ||
continue | ||
data = zf.read(info.filename) | ||
md = hashlib.md5() | ||
md.update(data) | ||
hashes.append(md.hexdigest()) | ||
|
||
# compute hash of jar file | ||
with open(fpath, 'rb') as jarin: | ||
md = hashlib.md5() | ||
md.update(jarin.read()) | ||
jarhash = md.hexdigest() | ||
# This replaces `/<root_dir>/a/b/c` with `/<root_dir_alias>/a/b/c` | ||
frelpath = os.path.join(root_dir_alias, | ||
os.path.relpath(fpath, root_dir)) | ||
|
||
# This converts something like `/.` to `/` | ||
|
||
frelpath = os.path.normpath(frelpath) | ||
|
||
(_, fname) = os.path.split(frelpath) | ||
return JarFeature( | ||
os.path.basename(fpath), | ||
fpath, | ||
jarhash, | ||
hashes | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import unittest | ||
|
||
import os | ||
import sys | ||
import tempfile | ||
from zipfile import ZipFile, ZipInfo | ||
|
||
from utils import jar_utils | ||
from utils.features import JarFeature | ||
|
||
# | ||
# https://security.openstack.org/guidelines/dg_using-temporary-files-securely.html | ||
# | ||
|
||
sys.path.append('tests/unit/') | ||
from plugins.systems.jar_host_crawler import JarHostCrawler | ||
|
||
|
||
class GPUPluginTests(unittest.TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename this :) |
||
|
||
def setUp(self): | ||
pass | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
def test_jar_host_crawler_plugin(self, *args): | ||
tmpdir = tempfile.mkdtemp() | ||
jar_file_name = 'myfile.jar' | ||
|
||
# Ensure the file is read/write by the creator only | ||
saved_umask = os.umask(0077) | ||
|
||
path = os.path.join(tmpdir, jar_file_name) | ||
try: | ||
with ZipFile(path, "w") as myjar: | ||
myjar.writestr(ZipInfo('first.class',(1980,1,1,1,1,1)), "first secrets!") | ||
myjar.writestr(ZipInfo('second.class',(1980,1,1,1,1,1)), "second secrets!") | ||
myjar.writestr(ZipInfo('second.txt',(1980,1,1,1,1,1)), "second secrets!") | ||
|
||
fc = JarHostCrawler() | ||
jars = list(fc.crawl(root_dir=tmpdir)) | ||
#jars = list(jar_utils.crawl_jar_files(root_dir=tmpdir)) | ||
print jars | ||
jar_feature = jars[0][1] | ||
assert 'myfile.jar' == jar_feature.name | ||
assert '48ac85a26ffa7ff5cefdd5c73a9fb888' == jar_feature.jarhash | ||
assert ['ddc6eff37020aa858e26b1ba8a49ee0e', | ||
'cbe2a13eb99c1c8ac5f30d0a04f8c492'] == jar_feature.hashes | ||
assert 'jar' == jars[0][2] | ||
|
||
except IOError as e: | ||
print 'IOError' | ||
finally: | ||
os.remove(path) | ||
os.umask(saved_umask) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
|
||
import os | ||
import tempfile | ||
from zipfile import ZipFile, ZipInfo | ||
|
||
from utils import jar_utils | ||
from utils.features import JarFeature | ||
|
||
# | ||
# https://security.openstack.org/guidelines/dg_using-temporary-files-securely.html | ||
# | ||
|
||
class JarUtilsTests(unittest.TestCase): | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
def test_get_jar_features(self): | ||
tmpdir = tempfile.mkdtemp() | ||
jar_file_name = 'myfile.jar' | ||
|
||
# Ensure the file is read/write by the creator only | ||
saved_umask = os.umask(0077) | ||
|
||
path = os.path.join(tmpdir, jar_file_name) | ||
try: | ||
with ZipFile(path, "w") as myjar: | ||
myjar.writestr(ZipInfo('first.class',(1980,1,1,1,1,1)), "first secrets!") | ||
myjar.writestr(ZipInfo('second.class',(1980,1,1,1,1,1)), "second secrets!") | ||
myjar.writestr(ZipInfo('second.txt',(1980,1,1,1,1,1)), "second secrets!") | ||
|
||
jars = list(jar_utils.crawl_jar_files(root_dir=tmpdir)) | ||
print jars | ||
jar_feature = jars[0][1] | ||
assert 'myfile.jar' == jar_feature.name | ||
assert '48ac85a26ffa7ff5cefdd5c73a9fb888' == jar_feature.jarhash | ||
assert ['ddc6eff37020aa858e26b1ba8a49ee0e', | ||
'cbe2a13eb99c1c8ac5f30d0a04f8c492'] == jar_feature.hashes | ||
assert 'jar' == jars[0][2] | ||
|
||
except IOError as e: | ||
print 'IOError' | ||
finally: | ||
os.remove(path) | ||
os.umask(saved_umask) | ||
os.rmdir(tmpdir) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please also test JarContainerCrawler class with a dummy container?