Hooking into Linux executable with LD_PRELOAD not work

55 views Asked by At

I have old game and i want to fix core dump of this game. I used gdb to recover game class and create my own shared library .so file. However, when i run program using LD_PRELOAD, nothing change as i code. This program runs on Linux 32bit.

$ ldd ./jx_linux_y
        linux-gate.so.1 =>  (0xf77d0000)
        libdl.so.2 => /lib/libdl.so.2 (0xf77c4000)
        libuuid.so.1 => /lib/libuuid.so.1 (0xf77be000)
        libpthread.so.0 => /lib/libpthread.so.0 (0xf77a3000)
        libstdc++.so.6 => /lib/libstdc++.so.6 (0xf76b7000)
        libm.so.6 => /lib/libm.so.6 (0xf7674000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xf7659000)
        libc.so.6 => /lib/libc.so.6 (0xf748e000)
        /lib/ld-linux.so.2 (0xf77d1000)

Please help me to know why. Thank you

I tried to compile my own code:

/usr/bin/gcc -lgcc_s -lstdc++ -m32 -ldl -std=c++11 -fPIC -shared gameserver.cpp -o gameserver.so

#include <stdio.h>
#include "KBuySell.h"

bool KBuySell::Init(void)
{
    printf("===========================\n");
    printf("Test hooking KBuySell::Init\n");
    printf("===========================\n");

    return 1;
};

bool KBuySell::Buy(int nPlayerIdx, int nBuy, int nBuyIdx, int nPlace, int nX, int nY)
{
    printf("===========================\n");
    printf("Test hooking KBuySell::Buy\n");
    printf("===========================\n");

    return 1;
};

KBuySell.h

#include <map>
#include "KSG_LogFile.h"
#include "KMemFile.h"

class KGoods;
class KItem;
class KTabFile;

enum CurrencyType
{
  CURRENCYTYPE_NONE,
  CURRENCYTYPE_MONEY,
  CURRENCYTYPE_FUYUAN,
  CURRENCYTYPE_COIN,
  CURRENCYTYPE_SCORE,
  CURRENCYTYPE_GOLDCOIN,
  CURRENCYTYPE_SLIVER,
  CURRENCYTYPE_TICKET,
  CURRENCYTYPE_YUANBAO,
  CURRENCYTYPE_GOLDCOIN_NEW,
  CURRENCYTYPE_CONTRIBUTION,
  CURRENCYTYPE_HONOUR,
  CURRENCYTYPE_GOLDCOIN_SCORE,
  CURRENCYTYPE_RESPECT,
  CURRENCYTYPE_REFINING,
  CURRENCYTYPE_ENERGY,
  CURRENCYTYPE_ARENA_CREDITS,
  CURRENCYTYPE_END
};

class KBuySell : public KMemFileNotifyHelper
{
  enum SALE_BEHAVIOR
  {
    BUY,
    SELL
  };

public:
  int **m_ShopGoods;

private:
  KGoods *m_Goods;
  int m_Width;
  int m_Height;
  int m_MaxGoods;
  static KTabFile ms_ItemBusinessShopSetting;
  std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char>> const, int>>> m_mapGoodsName;
  KItem *m_arySaleLogItem;
  int m_nSaleLogItemCount;
  KSG_LogFile m_SaleLogFile;

public:
  KBuySell();
  ~KBuySell();
  bool Init(void);
  int GetWidth(void);
  int GetHeight(void);
  int GetGoodsCount(void);
  KGoods *GetGoods(int);
  KGoods *GetGoods(const char *);
  int GetGoodsIndex(int, int);
  bool BuyCallBack(int, int, int);
  int AddGoodsToPlayer(int, KGoods *, CurrencyType, int, long long, int, int, int);
  void OpenSale(int, int, CurrencyType, int, const char *);
  void OpenItemBussinessSale(int, int, CurrencyType, int, const char *);
  bool Buy(int, int, int, int, int, int);
  bool Sell(int, int, int);
  void CreateStores(int);
  void AddShop2Stores(int, int, const char *, CurrencyType, int, const char *, bool);
  void OpenStores(int);
  void StoresChangeShop(int, int);
  bool WriteLog(int, KItem *, int, CurrencyType, int, KBuySell::SALE_BEHAVIOR, bool);
  bool WriteLog(int, KGoods *, int, CurrencyType, int, KBuySell::SALE_BEHAVIOR, bool);
  bool WriteLog(int, char *);
  static int ItemBusinessShopName2ID(char *);
  bool OnNotifyGoldCoinChangeResult(int, int, int, bool);
  bool OnNotifyBuyItem(int, int, int, long long);
  void ExportGoodsInfo(void);

private:
  bool LoadDataFromFile(void);
  bool RemoveAllData(void);
  virtual bool OnFileChanged(const char *, unsigned long, unsigned long);
  virtual bool OnlyForFileListChanged(void);
  bool InitItemSaleLog(void);
};

KMemFile.h

class IKMemFileNotify {
  public:
    virtual bool OnFileChanged(const char *, unsigned long, unsigned long);
    virtual bool OnlyForFileListChanged(void);
};


class KMemFileNotifyHelper : public IKMemFileNotify {
  private:
    unsigned long m_dwFileListGUID;
    unsigned long m_dwVerifyCode;

  public:
    KMemFileNotifyHelper(unsigned long);
    ~KMemFileNotifyHelper();
    unsigned long GetMemFileListGUID(void);
    unsigned long GetMemFileVerifyCode(void);
    virtual bool OnFileChanged(const char *, unsigned long, unsigned long);
};

KSG_LogFile.h

#include <stdint.h>
#include <stdio.h>

enum KE_LOGLEVEL
{
  emLOGLEVEL_FATAL,
  emLOGLEVEL_ERROR,
  emLOGLEVEL_WARN,
  emLOGLEVEL_INFO,
  emLOGLEVEL_DEBUG
};

struct KSG_LogFile
{
private:
  static unsigned int s_uCurDayOfYear;
  FILE *m_pLog;
  unsigned int m_uDayOfYear;
  int m_bFileNameAutoChangeWithDate;
  char m_szFileNameKey[260];
  char m_szSuffixName[20];
  KE_LOGLEVEL m_nLevel;
  size_t m_nFileSize;
  char m_szFile[260];

public:
  KSG_LogFile(char const *);
  ~KSG_LogFile();
  int Init(char const *);
  int InitWithDate(char const *, char const *, int);
  void ChangeLogLevel(KE_LOGLEVEL);
  void printf(const char *, ...);
  void puts(const char *);
  void write_time(void);
  void write_date(void);
  void write_date_time(void);
  void write_date_time_in(long);
  void printf_t(char const *, ...);
  void puts_t(char const *);
  void log(KE_LOGLEVEL, char const *, ...);
  void log_t(KE_LOGLEVEL, char const *, ...);

private:
  void WriteLog(KE_LOGLEVEL, char const *, char *, int);
  void WriteLog(char const *, size_t);
  int OpenNewFile(void);
};

Actually resulted: When buy an item in game, print the message.

0

There are 0 answers