You need to sign in or sign up before continuing.
texthook.cc 9.38 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
// texthook.cc
// 10/14/2011 jichi

#include "texthook/texthook.h"
#include "texthook/texthook_p.h"
#include "texthook/ihf_p.h"
#include "texthook/textthread_p.h"
#include "texthook/winapi_p.h"
#include <QtCore>

//#define DEBUG "texthook.cc"
#include "sakurakit/skdebug.h"

//#include <ITH/IHF_SYS.h>
//namespace { int _ = IthInitSystemService(); }

/** Private class */

TextHookPrivate *TextHookPrivate::instance_;

/** Public class */

// - Construction -

//TextHook *TextHook::g_;
//TextHook *TextHook::globalInstance() { static Self g; return &g; }

//TextHook::TextHook(QObject *parent)
//  : Base(parent), d_(new D)
//{}

TextHook::TextHook(QObject *parent)
  : Base(parent), d_(new D(this))
{
  Ihf::init();
  //Ihf::setUserDefinedThreadName(d_->source);
  DOUT("pass");
}

TextHook::~TextHook()
{
  DOUT("enter");
  if (isActive())
    stop();
  delete d_;

  Ihf::destroy();
  DOUT("leave");
}

// - Properties -

int TextHook::dataCapacity() const
{ return TextThreadDelegate::capacity(); }

void TextHook::setDataCapacity(int value)
{ TextThreadDelegate::setCapacity(value); }

bool TextHook::removesRepeat() const
{ return TextThreadDelegate::removesRepeat(); }

void TextHook::setRemovesRepeat(bool value)
{ TextThreadDelegate::setRemovesRepeat(value); }

bool TextHook::keepsSpace() const
{ return TextThreadDelegate::keepsSpace(); }

void TextHook::setKeepsSpace(bool value)
{ TextThreadDelegate::setKeepsSpace(value); }

bool TextHook::wideCharacter() const
{ return TextThreadDelegate::wideCharacter(); }

void TextHook::setWideCharacter(bool value)
{ TextThreadDelegate::setWideCharacter(value); }

// see: ITH/common.h
int TextHook::capacity() const
{ return 0x20; }

QString TextHook::defaultHookName() const
{ return d_->source; }

void TextHook::setDefaultHookName(const QString &name)
{
  d_->source = name;
  //Ihf::setUserDefinedThreadName(name);
}

bool TextHook::isEnabled() const
{ return d_->enabled; }

void TextHook::setEnabled(bool t)
{
  d_->enabled = t;
  Ihf::setEnabled(t);
}

bool TextHook::isActive() const
{ return Ihf::isLoaded(); }

void TextHook::start()
{ Ihf::load(); }

void TextHook::stop()
{
  if (!isEmpty())
    clear();
  Ihf::unload();
}

WId TextHook::parentWinId() const { return Ihf::parentWindow(); }
void TextHook::setParentWinId(WId hwnd) { Ihf::setParentWindow(hwnd); }

int TextHook::interval() const
{ return Ihf::messageInterval(); }

void TextHook::setInterval(int msecs)
{ Ihf::setMessageInterval(msecs); }

// - Injection -

void TextHook::clear()
{
  DOUT("enter");
  foreach (ulong pid, d_->pids)
    detachProcess(pid);
  if (!d_->hooks.isEmpty())
    d_->hooks.clear();
  clearThreadWhitelist();
  DOUT("leave");
}

bool TextHook::containsProcess(ulong pid) const { return d_->pids.contains(pid); }
bool TextHook::isEmpty() const { return d_->pids.isEmpty(); }

//QList<ulong> TextHook::attachedProcesses(bool checkActive) const
//{
//  if (isEmpty() || !checkActive)
//    return d_->pids;
//
//  QList<ulong> ret;
//  foreach (ulong pid, d_->pids)
//    if (winapi::IsProcessActiveWithId(pid))
//      ret.append(pid);
//  return ret;
//}

//ulong TextHook::currentProccess() const
//{ return anyAttachedProcess(true); } // check active = true

//ulong TextHook::anyAttachedProcess(bool checkActive) const
//{
//  if (isEmpty())
//    return 0;
// if (!checkActive)
//   return d_->pids.first();
//
//  foreach (ulong pid, d_->pids)
//    if (winapi::IsProcessActiveWithId(pid))
//      return pid;
//  return 0;
//}

//bool TextHook::attachOneProcess(ulong pid, bool checkActive)
//{
//  DOUT("enter: pid =" << pid);
//  DOUT("isAttached =" << containsProcess(pid));
//  if (!isActive())
//    start();
//
//  if (checkActive && !winapi::IsProcessActiveWithId(pid)) {
//    DOUT("leave: ret = false, isActive = false");
//    return false;
//  }
//
//  if (containsProcess(pid)) {
//    DOUT("leave: pid already attached");
//    return true;
//  }
//
//  detachAllProcesses();
//
//  bool ret = Ihf::attachProcess(pid);
//  if (ret && !containsProcess(pid)) {
//    d_->pids.removeAll(pid);
//    d_->pids.append(pid);
//    emit processAttached(pid);
//  }
//
//  DOUT("leave: ret =" << ret);
//  return ret;
//}

bool TextHook::attachProcess(ulong pid, bool checkActive)
{
  DOUT("enter: pid =" << pid << ", isAttached =" << containsProcess(pid));
  if (!isActive())
    start();
  Q_ASSERT(isActive());
  DOUT("isActive =" << isActive());

  if (checkActive && !winapi::IsProcessActiveWithId(pid)) {
    DOUT("leave: ret = false, isActive = false");
    return false;
  }

  bool ret = Ihf::attachProcess(pid);
  if (ret) {
    d_->pids.insert(pid);
    emit processAttached(pid);
  }

  DOUT("leave: ret =" << ret);
  return ret;
}

bool TextHook::detachProcess(ulong pid, bool checkActive)
{
  DOUT("enter: pid =" << pid << ", isAttached =" << containsProcess(pid));
  Q_ASSERT(isActive());

  auto it = d_->pids.find(pid);
  if (it == d_->pids.end()) {
    DOUT("leave: ret = false, not attached");
    return false;
  }
  d_->pids.erase(it);
  d_->hooks.remove(pid);

  if (checkActive && !winapi::IsProcessActiveWithId(pid)) {
    emit processDetached(pid);
    DOUT("leave: ret = false, isActive = false");
    return false;
  }

  bool ret = Ihf::detachProcess(pid);
  //try {
  //  ret = Ihf::detachProcess(pid);
  //} catch (...) {
  //  DOUT("warning: detach exception");
  //}

  emit processDetached(pid);
  DOUT("leave: ret =" << ret);
  return ret;
}

bool TextHook::hijackProcess(ulong pid)
{
  DOUT("enter: pid =" << pid);
  Q_ASSERT(isActive());

  if (!containsProcess(pid)) {
    DOUT("leave: aborted, process not attached");
    return false;
  }

  // 7/12/2015: Function disabled
  return true;

  //bool ret = Ihf::hijackProcess(pid);
  //DOUT("leave: ret =" << ret);
  //return ret;
}

//void TextHook::detachAllProcesses()
//{
//  DOUT("enter");
//  foreach (ulong pid, d_->pids)
//    detachProcess(pid);
//  if (!d_->hooks.isEmpty())
//    d_->hooks.clear();
//  DOUT("leave");
//}

// - Hook -

//bool TextHook::containsHook(ulong pid) const
//{ return d_->hooks.contains(pid); }
//
//bool TextHook::containsHook(ulong pid, const QString &code) const
//{ return processHook(pid) == code; }

bool TextHook::addHookCode(ulong pid, const QString &code, const QString &name, bool verbose)
{
  DOUT("enter: pid =" << pid << ", code =" << code);
  if (isEmpty() || !containsProcess(pid)) {
    DOUT("leave: failed, process not attached");
    return false;
  }
  if (d_->hooks.contains(pid)) {
    DOUT("leave: failed, hook already exists");
    return false;
  }
  bool ok = Ihf::addHook(pid, code,
                         name.isEmpty() ? defaultHookName() : name,
                         verbose);
  if (ok)
    d_->hooks[pid] = code;
  DOUT("leave: ret =" << ok);
  return ok;
}

bool TextHook::verifyHookCode(const QString &code) { return Ihf::verifyHookCode(code); }

bool TextHook::removeHookCode(ulong pid)
{
  DOUT("enter");
  auto p = d_->hooks.find(pid);
  if (p == d_->hooks.end()) {
    DOUT("leave: not hooked");
    return false;
  }

  //DOUT("remove existing hook, THIS SHOULD NOT HAPPEN");
  bool ok = Ihf::removeHook(pid, p.value());
  d_->hooks.erase(p);
  DOUT("leave: ret =" << ok);
  return ok;
}

//QString TextHook::processHook(ulong pid) const
//{
//  auto p = d_->hooks.find(pid);
//  return p == d_->hooks.end() ? QString() : p.value();
//}

bool TextHook::isThreadWhitelistEnabled() const { return Ihf::isWhitelistEnabled(); }

void TextHook::setThreadWhitelistEnabled(bool t) { Ihf::setWhitelistEnabled(t); }

QList<qint32> TextHook::threadWhitelist() const { return Ihf::whitelist(); }

void TextHook::setThreadWhitelist(const QList<qint32> &sigs) { Ihf::setWhitelist(sigs); }

void TextHook::clearThreadWhitelist() { Ihf::clearWhitelist(); }

QString TextHook::keptThreadName() const { return Ihf::keptThreadName(); }

void TextHook::setKeptThreadName(const QString &v) { Ihf::setKeptThreadName(v); }

// EOF

/*
QString
TextHook::guessEncodingForFile(const QString &fileName)
{
  static QHash<QString, QString> db;
  if (db.isEmpty()) {
    db["malie.exe"] = "UTF-16";
  }
  auto p = db.find(fileName);
  return p == db.end() ? QString() : p.value();
}

// - Helpers -

bool
TextHook::isStandardHookName(const QString &name) const
{
  static QSet<uint> hashes;
  if (hashes.isEmpty()) {
#define ADD(_text)  hashes.insert(qHash(QString(_text)))
    ADD("ConsoleOutput");
    ADD("GetTextExtentPoint32A");
    ADD("GetGlyphOutlineA");
    ADD("ExtTextOutA");
    ADD("TextOutA");
    ADD("GetCharABCWidthsA");
    ADD("DrawTextA");
    ADD("DrawTextExA");
    ADD("GetTextExtentPoint32W");
    ADD("GetGlyphOutlineW");
    ADD("ExtTextOutW");
    ADD("TextOutW");
    ADD("GetCharABCWidthsW");
    ADD("DrawTextW");
    ADD("DrawTextExW");
#undef ADD
  }
  uint h = qHash(name);
  return hashes.contains(h);
}

bool
TextHook::isKnownHookForProcess(const QString &hook, const QString &proc) const
{
  // TODO: update database on line periodically
  qDebug() << "qth::isKnownHookForProcess: hook ="  << hook << ", proc =" << proc;

  static QSet<uint> hashes;
  if (hashes.isEmpty()) {
#define ADD(_hook, _proc)  hashes.insert(qHash(QString(_hook) + "\n" + _proc))
    //ADD("Malie", "malie"); // light
    ADD("GetGlyphOutlineA", "STEINSGATE");
    ADD("StuffScriptEngine", "EVOLIMIT");
#undef ADD
  }
  uint h = qHash(hook + "\n" + proc);
  return hashes.contains(h);
}

QString
TextHook::hookNameById(ulong hookId) const
{
  //return Ihf::getHookNameById(hookId);
  // FIXME: supposed to be the engine name, unimplemented
  Q_UNUSED(hookId)
  return QString();
}


*/