Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Support for URLs analysis in cuckoo distributed (no db migration needed) #1700

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 27 additions & 13 deletions cuckoo/distributed/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,35 @@ def submit_task(url, task):
enforce_timeout=task["enforce_timeout"],
)

# If the file does not exist anymore, ignore it and move on
# to the next file.
if not os.path.isfile(task["path"]):
# The task is a URL analysis
if not task["filename"] and task["path"]:
data["url"] = task["path"]
try:
r = requests.post(
urlparse.urljoin(url, "/tasks/create/url"),
data=data
)
return r.json()["task_id"]
except Exception:
pass

# The task is a sample analysis
elif os.path.isfile(task["path"]):
files = {"file": (task["filename"], open(task["path"], "rb"))}
try:
r = requests.post(
urlparse.urljoin(url, "/tasks/create/file"),
data=data, files=files
)
return r.json()["task_id"]
except Exception:
pass

else:
# If the file does not exist anymore and no URL was submitted,
# ignore it and move on to the next file or URL.
return task["id"], None

files = {"file": (task["filename"], open(task["path"], "rb"))}
try:
r = requests.post(
urlparse.urljoin(url, "/tasks/create/file"),
data=data, files=files
)
return r.json()["task_id"]
except Exception:
pass

def fetch_tasks(url, status, limit):
r = _get(url, "/tasks/list/%s", limit, params=dict(status=status))
if r.status_code == 200:
Expand Down
22 changes: 14 additions & 8 deletions cuckoo/distributed/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def task_list():

@blueprint.route("/task", methods=["POST"])
def task_post():
if "file" not in request.files:
return json_error(404, "No file has been provided")
if "file" not in request.files and "url" not in request.form:
return json_error(404, "No file or URL has been provided")

args = dict(
package=request.form.get("package"),
Expand All @@ -201,15 +201,21 @@ def task_post():
enforce_timeout=request.form.get("enforce_timeout"),
)

f = request.files["file"]
if "file" in request.files:
f = request.files["file"]
fd, path = tempfile.mkstemp(dir=settings.samples_directory)
f.save(path)
os.close(fd)

fd, path = tempfile.mkstemp(dir=settings.samples_directory)
f.save(path)
os.close(fd)
task = Task(path=path, filename=os.path.basename(f.filename), **args)
db.session.add(task)

else:
task = Task(path=request.form.get("url"), **args)
db.session.add(task)

task = Task(path=path, filename=os.path.basename(f.filename), **args)
db.session.add(task)
db.session.commit()

return jsonify(success=True, task_id=task.id)

@blueprint.route("/task/<int:task_id>")
Expand Down