Jump to content

Py injection exploit


Recommended Posts

What is the exploit?

In CPythonLauncher::RunMemoryTextFile a string is used to compile c_pcFileData, so the exploit starts here:
"exec(compile(" 
Hackers can easily search for the string in memory & compile scripts using the memory adress of c_pcFileData
This looks like an intentional backdoor left by one of the game devs or maybe even the leaker of the files "Rain"

Why?

It's hard to believe anyone would trouble to concatenate so many strings instead of just using Py_CompileString
If you pay attention the rest of the code is fine and uses the python api instead of some weird shells

Seems like there are still many exploits in m2
Hope you learned something new today, now here is the fix you paid for:

ScriptLib/PythonLauncher.cpp search for:

bool CPythonLauncher::RunMemoryTextFile(const char* c_szFileName, UINT uFileSize, const VOID* c_pvFileData)

replace with this:

bool CPythonLauncher::RunMemoryTextFile(const char* c_szFileName, UINT uFileSize, const VOID* c_pvFileData)
{
    const CHAR* c_pcFileData = (const CHAR*)c_pvFileData;
    std::string stConvFileData;
    stConvFileData.reserve(uFileSize);

    for (UINT i = 0; i < uFileSize; ++i)
    {
        if (c_pcFileData[i] != 13)
            stConvFileData += c_pcFileData[i];
    }

    const CHAR* c_pcConvFileData = stConvFileData.c_str();
    PyObject* pCompiledCode = Py_CompileString(c_pcConvFileData, c_szFileName, Py_file_input);//fix
    if (!pCompiledCode)
        return false;
    PyObject* pResult = PyEval_EvalCode((PyCodeObject*)pCompiledCode, m_poDic, m_poDic);
    Py_DECREF(pCompiledCode);//ref c
    if (!pResult)
        return false;

    Py_DECREF(pResult);
    if (Py_FlushLine())
        PyErr_Clear();

    return true;
}

 

  • Metin2 Dev 1
  • Eyes 3
  • Good 2
  • muscle 1
  • Love 4
Link to comment
https://metin2.dev/topic/33050-py-injection-exploit/
Share on other sites

bool CPythonLauncher::RunMemoryTextFile(const char* c_szFileName, UINT uFileSize, const VOID* c_pvFileData)
{
    const CHAR* c_pcFileData = (const CHAR*)c_pvFileData;
    std::string stConvFileData;
    stConvFileData.reserve(uFileSize);

    for (UINT i = 0; i < uFileSize; ++i)
    {
        if (c_pcFileData[i] != 13)
            stConvFileData += c_pcFileData[i];
    }

    const CHAR* c_pcConvFileData = stConvFileData.c_str();
    return RunLine(c_pcConvFileData); 
}

Here is the original fix that uses RunLine for the PyObject

  • muscle 1
Link to comment
https://metin2.dev/topic/33050-py-injection-exploit/#findComment-167287
Share on other sites

  • Honorable Member

If you're using cython, you can directly remove that function. RunFile isn't used elsewhere except for calling system.py.


#ifdef __USE_CYTHON__
		if (!pyLauncher.RunLine("import rootlib\nrootlib.moduleImport('system')"))
#else
		if (!pyLauncher.RunFile("system.py"))
#endif

 

Just wrap the two functions like this:

s1.png

  image.png

 

 

 

 

18 hours ago, Woops said:

It's hard to believe

Why not?
They are using c string allocation instead of std::string in the function calling it. It's not that hard to understand they were as dumb as monkeys.


bool CPythonLauncher::RunFile(const char* c_szFileName) const
{
	char* acBufData= nullptr;
	DWORD dwBufSize=0;

	{
		CMappedFile file;
		const VOID* pvData;
		CEterPackManager::Instance().Get(file, c_szFileName, &pvData);

		dwBufSize=file.Size();
		if (dwBufSize==0)
			return false;

		acBufData=new char[dwBufSize];
		memcpy(acBufData, pvData, dwBufSize);
	}

	bool ret=false;

	ret=RunMemoryTextFile(c_szFileName, dwBufSize, acBufData);

	delete [] acBufData;

	return ret;
}

Re-written:

bool CPythonLauncher::RunFile(const char* c_szFileName) const
{
	std::string acBufData;

	{
		CMappedFile file;
		const VOID* pvData;
		CEterPackManager::Instance().Get(file, c_szFileName, &pvData);

		if (file.Size() == 0)
			return false;

		acBufData.resize(file.Size());
		memcpy(acBufData.data(), pvData, acBufData.size());
	}

	return RunMemoryTextFile(c_szFileName, acBufData.size(), acBufData.data());
}

 

Edited by Metin2 Dev International
Core X - External 2 Internal
  • kekw 1
  • Flame 1
  • Lmao 1
  • Good 2
  • Love 7
Link to comment
https://metin2.dev/topic/33050-py-injection-exploit/#findComment-167295
Share on other sites

Don't use any images from : imgur, turkmmop, freakgamers, inforge, hizliresim... Or your content will be deleted without notice...
Use : https://metin2.download/media/add/

Please use https://metin2.download/ when uploading files smaller than 100MB, otherwise the approval will take longer due to manual upload.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.