DetourAttach success but no functions hooked :( - dll

Good morning !
I have recently read articles quite interesting about hooking functions, I have followed one or two tutorials but it never seems to work, I am using Detoured and here is the full code which seems to me perfectly normal :(
#include <stdio.h>
#include <windows.h>
#include "stdafx.h"
#include "detours.h"
#pragma comment(lib, "detours.lib")
int(__stdcall* realFunc)(int) = (int(__stdcall*)(int))(0x004157B0);
void hookedFunc(int num)
{
printf("Test : %d\n", num + 100);
}
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
break;
case DLL_THREAD_ATTACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
DetourTransactionCommit();
hookedFunc(100);
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
DetourDetach((PVOID*)0x004157B0, hookedFunc);
break;
}
return TRUE;
}
When using RemoteDLL and a simple console application as dummy to hook the function, all steps are completed successfully (running as administrator), the memory address to the function I want to be hooked matches, however the code line "printf("Test : %d\n", num + 100);" is not executed, the result does not appears at screen...
If anyone would have an idea about what's going on I would be really happy to hear it !
Thanks in advance !

First, hookedFunc must have the same signature: int __stdcall hookedFunc(int x).
I suppose the following effect of your code: hookedFunc is called each time somebody calls the function at address 0x004157B0. Is it what you expect?
For testing, you call this address. Let me change the code a little to clarify:
extern int __stdcall FunctionIWantToHook(int);
int(__stdcall* realFunc)(int) = FunctionIWantToHook;
...
DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
FunctionIWantToHook(100); // hookedFunc will be called here

Related

Static declaration of '__vector_1' follows non-static declaration

Im trying to create a program which will interrupt when I press the button. I have Atmega8 and I use Microchip studio for coding.
I checked the document about interrupts on atmega's website however I can't say I totally got it.
Here is my code:
#define F_CPU 1000000UL
#define IRQ1 INT0_vect
#define IRQ2 INT1_vect
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
void init()
{
DDRB=0b11111111;
PORTB=255;
_delay_ms(2000);
PORTB=0;
DDRD = 0b00000000;
GICR=0xc0;
MCUCR=0x08;
}
int main(void){
init();
volatile int mode = 0;
ISR(IRQ1){
_delay_ms(500);
if (mode<3)mode++; else mode = 0;
}
ISR(IRQ2){
_delay_ms(150);
}
}
Errors I get:
Imgur
I would be glad if any admin edits my question and add picture here, website doesn't let me add photo because I need at least 10 reputation to post image
Don't try to define functions inside of other functions unless you really know what you are doing. You should move the ISR definitions to the top level of the file, putting them outside of main.

QTcpSocket does not emit connected()

I'm using QT5 and starting out with a basic Server/Client setup. I'm looking at going single threaded for both apps as there is no heavy processing on network data. Now, from everything I've read and researched here, when using asynchronous approach, you don't use waitForXXXX() otherwise it messes up all the signals and slots. The problem - On the client end, the connected() signal is either never emitted or never processed, even though the server consoles tells me that a new client has connected. I've been working on the same issue for 2 weeks now and couldn't find the exact same issue anywhere. I've stripped back both apps to the minimum and still no luck - also stripped out the UI part now - I just want see the console working. I have also tried switching to public slots and changing the signal/slot connection type and still have the same problem.
If you require code from the server, please let me know, but here is the basics of the client:
main.cpp
#include "QGameSocket.h"
#include <QtWidgets/QApplication>
#include <windows.h>
int main(int argc, char *argv[])
{
AllocConsole();
freopen( "conin$", "r", stdin );
freopen( "conout$", "w", stdout );
freopen( "conout$", "w", stderr );
QApplication a( argc, argv );
QGameSocket* pSocket = new QGameSocket();
return a.exec();
}
QGameSocket.h
#ifndef _QGAMESOCKET_H
#define _QGAMESOCKET_H
#include <QtNetwork/qtcpsocket.h>
#pragma comment ( lib, "Qt5Network.lib" )
class QGameSocket: public QObject
{
Q_OBJECT
public:
explicit QGameSocket( QObject* pParent = 0 );
~QGameSocket();
private slots:
void __OnConnected();
void __OnReadyRead();
private:
QTcpSocket* m_pSocket;
};
#endif
QGameSocket.cpp
#include "QGameSocket.h"
#include <qdatastream.h>
QGameSocket::QGameSocket( QObject* pParent ) :
QObject( pParent )
{
m_pSocket = new QTcpSocket();
connect( m_pSocket, SIGNAL( connected() ), this, SLOT( __OnConnected() ) );
connect( m_pSocket, SIGNAL( readyRead() ), this, SLOT( __OnReadyRead() ) );
const QString strHost = "127.0.0.1";
qDebug() << "Connecting to host ...";
m_pSocket->connectToHost( strHost, 27015 );
}
QGameSocket::~QGameSocket()
{
m_pSocket->deleteLater();
}
void QGameSocket::__OnConnected()
{
qDebug() << "Successfully connected to host!";
}
void QGameSocket::__OnReadyRead()
{
//handle messages
}
Any help would be much appreciated, thank you!
I finally figured it out - I was using the release network library, not the debug library.
I changed:
#pragma comment ( lib, "Qt5Network.lib" )
to
#pragma comment ( lib, "Qt5Networkd.lib" )

Dll missing entry point timeGetTime

Trying to compile this DLL in MingGWx64, using the following command
gcc -shared -o evil.dll evil.cpp -DWIN32_LEAN_AND_MEAN
Through trial and error I moved the "int fireMyLaser ()" below the declaration, from the bottom of the code sample I found. But I still get an error on the load of the EXE that it can't find the entry-point timeGetTime. Anyone have any ideas?
#include <windows.h>
#define DllExport __declspec (dllexport)
int fireMyLaser()
{
WinExec("calc", 0);
return 0;
}
DllExport void timeGetTime() { fireMyLaser(); }
BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason, LPVOID lpvReserved)
{
fireMyLaser();
return 0;
}`
Compiling the DLL works, on loading the EXE I get "The procedure entry point timeGetTime could not be located in the dynamic link library"
I don't have access to the exe code, but through trial and error the below worked.
// includes adjusted here to allow for timeGetTime to be used as an entry point
#include <windef.h>
#include <stdio.h>
#include <WinBase.h>
//entrypoint timeGetTime below for exe to hit... repeatedly
extern "C" __declspec(dllexport) int timeGetTime() {
WinExec("calc.exe", 0);
return 0;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason, LPVOID lpvReserved)
{
timeGetTime();
return TRUE;
}

Android NDK/JNI torch state control

How can I change state of the camera flash throgh JNI function? I am looking to be able to have ON/OFF state control, just like in Java CameraManager.setTorchMode(cameraId, state); method. I've tried to search for it in native camera API ,but no success. Here's what I have done so far:
#include <jni.h>
#include <assert.h>
#include <jni.h>
#include <pthread.h>
#include <android/native_window_jni.h>
#include <camera/NdkCameraDevice.h>
#include <camera/NdkCameraManager.h>
#include <android/asset_manager.h>
#include "messages-internal.h"
JNIEXPORT void JNICALL
Java_com_android_rxjava_flashlightflicker_MainActivity_flasher(JNIEnv *env, jobject instance) {
ACameraIdList *cameraIdList = NULL;
const char *selectedCameraId = NULL;
ACameraManager *cameraManager = ACameraManager_create();
camera_status_t camera_status = ACAMERA_OK;
camera_status = ACameraManager_getCameraIdList(cameraManager, &cameraIdList);
/// Camera status not ok
if (camera_status != ACAMERA_OK) {
LOGE("Camera is bad id: %d \n", camera_status);
return;
}
// There is no camera
if (cameraIdList->numCameras < 1 ) {
LOGE("Camera is not present on the device.");
return;
}
selectedCameraId = cameraIdList->cameraIds[0];
ACameraMetadata *cameraMetedata = NULL;
ACameraManager_getCameraCharacteristics(cameraManager, selectedCameraId, &cameraMetedata);
// ACaptureSessionOutput_create()
}
I also tried to look in asset manager but no success, can anybody experienced with NDK camera give me a hand with it?
Thanks in advance!
This method is only available in Java API. You could access it through JNI, but IMO it would be easier and safer to write a wrapper static method in Java and have this wrapper called from your C++ code.

How to make a loadable dll to use tcl code functionality by any program

I have created a GUI using tcl. I want to make some of the core functionalities of the tcl code available to be used by any program which supports dll. For that i have taken a very simple tcl code example, which adds two integer numbers and i have written a c wrapper function to use this functionality. This is working for me. Now how can i create a dll for these two c and tcl files, so that any program can use this addition functionality by simply loading the dll.
Here is my simple tcl code :
/* Filename : simple_addition.tcl */
#!/usr/bin/env tclsh8.5
proc add_two_nos { } {
set a 10
set b 20
set c [expr { $a + $b } ]
puts " c is $c ......."
}
And here is my c wrapper function which uses the above tcl addition functionality :
#include <tcl.h>
#include <tclDecls.h>
#include <tclPlatDecls.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char **argv) {
Tcl_Interp *interp;
int code;
char *result;
printf("inside main function \n");
Tcl_FindExecutable(argv[0]);
interp = Tcl_CreateInterp();
code = Tcl_Eval(interp, "source simple_addition.tcl; add_two_nos");
/* Retrieve the result... */
result = Tcl_GetString(Tcl_GetObjResult(interp));
/* Check for error! If an error, message is result. */
if (code == TCL_ERROR) {
fprintf(stderr, "ERROR in script: %s\n", result);
exit(1);
}
/* Print (normal) result if non-empty; we'll skip handling encodings for now */
if (strlen(result)) {
printf("%s\n", result);
}
/* Clean up */
Tcl_DeleteInterp(interp);
exit(0);
}
This c wrapper is working fine for me and gives correct results.
Now I want to create a dll file, so that if i include that dll to any program that supports dll, it should be able to use this addition functionality of the above tcl code. Can anybody please tell me the way i can do it. Please help me. I am new to this dll concept.
In order to create the .dll you'll have to use something like Visual Studio and C or C++ to create the .dll (there are lots of other tools out there that can create .dll files but VS is easy to get hold of and to use.) So in VS create a new project, this needs to be a C++ WIN32 project. Select the DLL application type and the Export Symbols additional option.
VS will create a basic .dll that you can then amend to do what you want. I short I'd look at putting the creating/destruction of the intrepter into the dllmain:
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
Tcl_FindExecutable(NULL);
interp = Tcl_CreateInterp();
}
case DLL_THREAD_ATTACH:
break ;
case DLL_THREAD_DETACH:
break ;
case DLL_PROCESS_DETACH:
{
Tcl_DeleteInterp(interp);
break;
}
}
return TRUE;
}
and then create functions exported by the .dll that make use of the interpreter. If you aren't familiar with the concept of shared libaries then I'd suggest spending a little time reading up on them, try here and here for some background reading.