Skip to content

Commit

Permalink
More SOCKS fixes (#515)
Browse files Browse the repository at this point in the history
* fixed port reuse issue with stale agents

* fixed error handling for sleep in ironpython

* fixed issue where ironpython did not support sleep

* updated lib.zip with updated secretsocks package

* fixed restarting existing socks server

* added socksclient to server restart

* move wrapfunction so its optional, update secretsocks lib.zip, change python to ironpython in c# stager

* reverted renaming languages in c# stager

* formatting
  • Loading branch information
Cx01N authored Dec 30, 2022
1 parent 8990f05 commit 55c7b86
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
8 changes: 7 additions & 1 deletion empire/client/src/menus/InteractMenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,13 @@ def sleep(self, delay: int, jitter: int) -> None:
"""
response = state.agent_sleep(self.session_id, delay, jitter)
log.info(f"Tasked agent to sleep delay/jitter {delay}/{jitter}")
log.info("Tasked " + self.selected + " to run Task " + str(response["id"]))
if "id" in response:
log.info(
"[*] Tasked " + self.session_id + " to run Task " + str(response["id"])
)

elif "detail" in response.keys():
log.error("[!] Error: " + response["detail"])

@command
def info(self) -> None:
Expand Down
15 changes: 8 additions & 7 deletions empire/server/core/agent_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.orm import Session

from empire.server.common.helpers import KThread
from empire.server.common.socks import start_client
from empire.server.common.socks import create_client, start_client
from empire.server.core.agent_task_service import AgentTaskService
from empire.server.core.db import models
from empire.server.core.db.base import SessionLocal
Expand Down Expand Up @@ -63,16 +63,17 @@ def start_existing_socks(self, db: Session, agent: models.Agent):
log.info(f"Starting SOCKS client for {agent.session_id}")
try:
self.main_menu.agents.socksqueue[agent.session_id] = queue.Queue()
client = create_client(
self.main_menu,
self.main_menu.agents.socksqueue[agent.session_id],
agent.session_id,
)
self.main_menu.agents.socksthread[agent.session_id] = KThread(
target=start_client,
args=(
self.main_menu.agenttasksv2,
self.main_menu.agents.socksqueue[agent.session_id],
agent.session_id,
agent.socks_port,
),
args=(client, agent.socks_port),
)

self.main_menu.agents.socksclient[agent.session_id] = client
self.main_menu.agents.socksthread[agent.session_id].daemon = True
self.main_menu.agents.socksthread[agent.session_id].start()
log.info(f'SOCKS client for "{agent.name}" successfully started')
Expand Down
9 changes: 8 additions & 1 deletion empire/server/core/agent_task_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import threading
import time
from collections import defaultdict
from datetime import datetime
from typing import Dict, List, Optional, Tuple
Expand Down Expand Up @@ -185,6 +186,12 @@ def create_task_exit(self, db, agent: models.Agent, current_user_id: int):
resp, err = self.add_task(db, agent, "TASK_EXIT", user_id=current_user_id)
agent.archived = True

# Close socks client
if (agent.session_id in self.main_menu.agents.socksthread) and agent.stale:
agent.socks = False
self.main_menu.agents.socksclient[agent.session_id].shutdown()
time.sleep(1)
self.main_menu.agents.socksthread[agent.session_id].kill()
return resp, err

def create_task_socks(
Expand Down Expand Up @@ -235,7 +242,7 @@ def create_task_update_sleep(
f"Set-Delay {str(delay)} {str(jitter)}",
user_id=user_id,
)
elif agent.language == "python":
elif agent.language in ["python", "ironpython"]:
return self.add_task(
db,
agent,
Expand Down
Binary file modified empire/server/csharp/Covenant/Data/EmbeddedResources/Lib.zip
Binary file not shown.
5 changes: 3 additions & 2 deletions empire/server/data/agent/ironpython_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ def process_tasking(data):

except Exception as e:
print(e)
# print "processTasking exception:",e
pass


Expand Down Expand Up @@ -1006,14 +1005,16 @@ def recv(self):
def write(self):
while self.alive:
try:
data = self.writebuf.get(timeout=3)
data = self.writebuf.get(timeout=10)
send_message(
build_response_packet(
61, base64.b64encode(data).decode("UTF-8"), self.resultID
)
)
except Queue.Empty:
continue
except:
self.alive = False


################################################
Expand Down
3 changes: 2 additions & 1 deletion empire/server/data/agent/stagers/http/comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def send_message(packets=None):
requestUri = server + taskURI

try:
wrapmodule(urllib.request)
if proxy_list:
wrapmodule(urllib.request)
data = (urllib.request.urlopen(urllib.request.Request(requestUri, data, headers))).read()
return ('200', data)

Expand Down

0 comments on commit 55c7b86

Please sign in to comment.