~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to build/win32/crashinject.cpp

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
 
 
5
 
/*
6
 
 * Given a PID, this program attempts to inject a DLL into the process
7
 
 * with that PID. The DLL it attempts to inject, "crashinjectdll.dll",
8
 
 * must exist alongside this exe. The DLL will then crash the process.
9
 
 */
10
 
#include <stdio.h>
11
 
#include <stdlib.h>
12
 
#include <string.h>
13
 
#include <windows.h>
14
 
 
15
 
int main(int argc, char** argv)
16
 
{
17
 
  if (argc != 2) {
18
 
    fprintf(stderr, "Usage: crashinject <PID>\n");
19
 
    return 1;
20
 
  }
21
 
 
22
 
  int pid = atoi(argv[1]);
23
 
  if (pid <= 0) {
24
 
    fprintf(stderr, "Usage: crashinject <PID>\n");
25
 
    return 1;
26
 
  }
27
 
 
28
 
  // find our DLL to inject
29
 
  wchar_t filename[_MAX_PATH];
30
 
  if (GetModuleFileNameW(NULL, filename, sizeof(filename) / sizeof(wchar_t)) == 0)
31
 
    return 1;
32
 
 
33
 
  wchar_t* slash = wcsrchr(filename, L'\\');
34
 
  if (slash == NULL)
35
 
    return 1;
36
 
 
37
 
  slash++;
38
 
  wcscpy(slash, L"crashinjectdll.dll");
39
 
 
40
 
  // now find our target process
41
 
  HANDLE targetProc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION,
42
 
                                  FALSE,
43
 
                                  pid);
44
 
  if (targetProc == NULL) {
45
 
    fprintf(stderr, "Error %d opening target process\n", GetLastError());
46
 
    return 1;
47
 
  }
48
 
 
49
 
  /*
50
 
   * This is sort of insane, but we're implementing a technique described here:
51
 
   * http://www.codeproject.com/KB/threads/winspy.aspx#section_2
52
 
   *
53
 
   * The gist is to use CreateRemoteThread to create a thread in the other
54
 
   * process, but cheat and make the thread function kernel32!LoadLibrary,
55
 
   * so that the only remote data we have to pass to the other process
56
 
   * is the path to the library we want to load. The library we're loading
57
 
   * will then do its dirty work inside the other process.
58
 
   */
59
 
  HMODULE hKernel32 = GetModuleHandleW(L"Kernel32");
60
 
  // allocate some memory to hold the path in the remote process
61
 
  void*   pLibRemote = VirtualAllocEx(targetProc, NULL, sizeof(filename),
62
 
                                      MEM_COMMIT, PAGE_READWRITE);
63
 
  if (pLibRemote == NULL) {
64
 
    fprintf(stderr, "Error %d in VirtualAllocEx\n", GetLastError());
65
 
    CloseHandle(targetProc);
66
 
    return 1;
67
 
  }
68
 
 
69
 
  if (!WriteProcessMemory(targetProc, pLibRemote, (void*)filename,
70
 
                          sizeof(filename), NULL)) {
71
 
    fprintf(stderr, "Error %d in WriteProcessMemory\n", GetLastError());
72
 
    VirtualFreeEx(targetProc, pLibRemote, sizeof(filename), MEM_RELEASE);
73
 
    CloseHandle(targetProc);
74
 
    return 1;
75
 
  }
76
 
  // Now create a thread in the target process that will load our DLL
77
 
  HANDLE hThread = CreateRemoteThread(
78
 
                     targetProc, NULL, 0,
79
 
                     (LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32,
80
 
                                                            "LoadLibraryW"),
81
 
                     pLibRemote, 0, NULL);
82
 
  if (hThread == NULL) {
83
 
    fprintf(stderr, "Error %d in CreateRemoteThread\n", GetLastError());
84
 
    VirtualFreeEx(targetProc, pLibRemote, sizeof(filename), MEM_RELEASE);
85
 
    CloseHandle(targetProc);
86
 
    return 1;
87
 
  }
88
 
  WaitForSingleObject(hThread, INFINITE);
89
 
  // Cleanup, not that it's going to matter at this point
90
 
  CloseHandle(hThread);
91
 
  VirtualFreeEx(targetProc, pLibRemote, sizeof(filename), MEM_RELEASE);
92
 
  CloseHandle(targetProc);
93
 
 
94
 
  return 0;
95
 
}