-
Notifications
You must be signed in to change notification settings - Fork 17
Add the ability to dump memory and vads for binaries. #4
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
Open
iantbutler01
wants to merge
2
commits into
moyix:master
Choose a base branch
from
iantbutler01:itb-2019-ressurection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,4 @@ | ||
| qcow/* | ||
| db/* | ||
| iso/* | ||
| *.pyc |
This file contains hidden or 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 hidden or 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,2 @@ | ||
| #!/bin/bash | ||
| ls queue/pending/$2/ | parallel -j 40 python scripts/runmal.py conf/malrec.config {/} {%} $1 ; sleep 600 |
This file contains hidden or 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,83 @@ | ||
| import os | ||
| import tarfile | ||
| import subprocess | ||
| import errno | ||
| import re | ||
| import time | ||
| from Queue import Queue, Empty | ||
| from threading import Thread | ||
| import signal | ||
|
|
||
| def dumpmem(panda_path, mem, sample_name, dumppath): | ||
| mkdir_p(dumppath) | ||
| path = os.path.join(dumppath, "memdump") | ||
| panda_args = [panda_path, | ||
| '-m', mem, | ||
| '-replay', sample_name, | ||
| '-panda', "n_instruct_mem:file={}".format(path) | ||
| ] | ||
| tarname = "{}.tar.gz".format(dumppath) | ||
|
|
||
| rr_path = "/".join(dumppath.split("/")[0:-1]) | ||
|
|
||
| proc = subprocess.Popen(panda_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=rr_path) | ||
|
|
||
| stdout_q = Queue() | ||
| stderr_q = Queue() | ||
| out_t = Thread(target=enqueue_output, args=(proc.stdout, stdout_q)) | ||
| err_t = Thread(target=enqueue_output, args=(proc.stderr, stderr_q)) | ||
| out_t.daemon = True | ||
| err_t.daemon = True | ||
| out_t.start() | ||
| err_t.start() | ||
|
|
||
| finished = False | ||
| success = False | ||
| count = 0 | ||
|
|
||
| while not finished: | ||
| stdout = get_nowait_q(stdout_q) | ||
| stderr = get_nowait_q(stderr_q) | ||
|
|
||
| print(stdout) | ||
|
|
||
| if stdout and re.search("Replay completed successfully", stdout): | ||
| print("Success case made") | ||
| finished = True | ||
| success = True | ||
| os.kill(proc.pid, signal.SIGKILL) | ||
| else: | ||
| time.sleep(2) | ||
|
|
||
| out_t.join() | ||
| err_t.join() | ||
|
|
||
| if success: | ||
| with tarfile.open(tarname, "w:gz") as tar: | ||
| print("Tart") | ||
| tar.add(dumppath, arcname=sample_name) | ||
| print("Tarted") | ||
| else: | ||
| return (False, "Failed to create memdumps for {}".format(sample_name), None) | ||
|
|
||
| return (True, "Success for {}".format(sample_name), tarname) | ||
|
|
||
| def mkdir_p(path): | ||
| try: | ||
| os.makedirs(path) | ||
| except OSError as exc: | ||
| if exc.errno == errno.EEXIST and os.path.isdir(path): | ||
| pass | ||
| else: | ||
| raise | ||
|
|
||
| def enqueue_output(out, queue): | ||
| for line in iter(out.readline, b''): | ||
| queue.put(line) | ||
| out.close() | ||
|
|
||
| def get_nowait_q(q): | ||
| try: | ||
| return q.get_nowait() | ||
| except Empty: | ||
| pass |
This file contains hidden or 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 hidden or 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,132 @@ | ||
| import os | ||
| import sys | ||
| import tarfile | ||
| import glob | ||
| import ConfigParser | ||
| import subprocess | ||
| import time | ||
| import errno | ||
| from collections import Counter | ||
| from pprint import pprint | ||
| from functools import partial | ||
| from multiprocessing import Pool | ||
|
|
||
| def find_possible_targets(folder_name): | ||
| proc_counts = Counter() | ||
|
|
||
| for memfile in sorted(glob.glob(folder_name + "/*"), key=lambda name: name.split("_")[-1]): | ||
| stdout = vol_pslist(memfile) | ||
| count = len(stdout.split("\n")[2:]) | ||
| proc_key = memfile.split("_")[-1] | ||
| proc_counts[proc_key] = count | ||
|
|
||
| count_by_dump = proc_counts.most_common() | ||
|
|
||
| _max = count_by_dump[0] | ||
| _min = count_by_dump[-1] | ||
|
|
||
| if _max[1] == _min[1]: | ||
| raise ValueError("It looks like the EXE was not run in the recording.") | ||
|
|
||
| potential_targets = None | ||
|
|
||
| max_index = int(_max[0].split("_")[-1]) | ||
| min_index = int(_max[0].split("_")[-1]) | ||
|
|
||
| if max_index > len(count_by_dump) // 2: | ||
| potential_targets = test_for_targets(_min[1], range(max_index, len(count_by_dump)), proc_counts) | ||
| else: | ||
| potential_targets = test_for_targets(_min[1], range(min_index, (len(count_by_dump) // 2) + 1 ), proc_counts) | ||
|
|
||
| return (_min, potential_targets) | ||
|
|
||
| def test_for_targets(baseline, _range, counts): | ||
| passing = [] | ||
|
|
||
| if len(counts.keys()) == 1: | ||
| return [0] | ||
|
|
||
| for index in _range: | ||
| if int(counts[str(index)]) - baseline > 0: | ||
| passing.append(index) | ||
|
|
||
| return passing | ||
|
|
||
| def dump_vads(folder_name, _type, os_procs, dump_index): | ||
| vad_path = "/tmp/vads/{}".format(folder_name) | ||
|
|
||
| mkdir_p(vad_path) | ||
|
|
||
| filename = "/tmp/{0}/{1}/memdump_{2}".format(_type, folder_name, dump_index) | ||
|
|
||
| vol_args = ["python", | ||
| conf.get("Main", 'volatility'), | ||
| '-f', filename, | ||
| '--profile', "Win7SP0x86", | ||
| 'vaddump', '-D', vad_path | ||
| ] | ||
|
|
||
| subprocess.check_output(vol_args, stderr=subprocess.PIPE) | ||
|
|
||
| for proc_name in os_procs: | ||
| for f in glob.glob(vad_path + "/*{}*".format(proc_name)): | ||
| os.remove(f) | ||
|
|
||
| def mkdir_p(path): | ||
| try: | ||
| os.makedirs(path) | ||
| except OSError as exc: | ||
| if exc.errno == errno.EEXIST and os.path.isdir(path): | ||
| pass | ||
| else: | ||
| raise | ||
|
|
||
| def proc_list_from_dump_index(folder_name, _type, dump_index): | ||
| memfile = "/tmp/{0}/{1}/memdump_{2}".format(_type, folder_name, dump_index) | ||
| stdout = filter(None, vol_pslist(memfile).split("\n")) | ||
| proc_names = map(lambda ln: ln.split(" ")[1], stdout[2:]) | ||
|
|
||
| return proc_names | ||
|
|
||
| def vol_pslist(memfile): | ||
| vol_args = ["python", | ||
| conf.get("Main", 'volatility'), | ||
| '-f', memfile, | ||
| '--profile', "Win7SP0x86", | ||
| 'pslist', | ||
| ] | ||
|
|
||
| return subprocess.check_output(vol_args, stderr=subprocess.PIPE) | ||
|
|
||
| def execute(_type, file): | ||
| with tarfile.open(file) as memdumps: | ||
| try: | ||
| memdumps.extractall(path="/tmp/{}/".format(_type)) | ||
| except: | ||
| print("Incomplete targz") | ||
|
|
||
| #not the clearest but basically grabbing the untarred folder name from the tarred folder name | ||
| folder_name = "_".join((".".join(file.split("/")[-1].split(".")[0:-2])).split("_")[0:-1]) | ||
| baseline, potential_targets = find_possible_targets("/tmp/{0}/{1}".format(_type, folder_name)) | ||
| os_procs = proc_list_from_dump_index(folder_name, _type, baseline[0]) | ||
|
|
||
| map(partial(dump_vads, folder_name, _type, os_procs), potential_targets) | ||
|
|
||
| def main(): | ||
| folder = sys.argv[1] | ||
| _type = "benign" | ||
|
|
||
| global conf | ||
|
|
||
| conf = ConfigParser.ConfigParser() | ||
| conf.read("conf/malrec.config") | ||
|
|
||
| if bool(int(sys.argv[2])): | ||
| _type = "malicious" | ||
|
|
||
| p = Pool(10) | ||
| files = glob.glob(folder + "/*") | ||
| p.map(partial(execute, _type), files) | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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.
What's the idea behind these changes? IIRC,
run_idwas intended to be a UUID, whereassample_namewas the filename of the sample. I had been trying to userun_ideverywhere because we may want to run the same sample more than once. (But I may be misremembering the purpose of these variables)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.
Right for the purpose of dumping the memory and finding the process in the list of dumped vads later on having the name was pretty important. One solution here would be to add a cli argument for naming scheme type so that either are available. What do you think?
Uh oh!
There was an error while loading. Please reload this page.
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.
Hmm, right now the UUID to filename map is put into an sqlite database so it can be retrieved later for things like matching the VADs. Would that work here?