1// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include "sandbox/win/sandbox_poc/pocdll/exports.h"
7#include "sandbox/win/sandbox_poc/pocdll/utils.h"
8
9// This file contains the tests used to verify the security of the system by
10// using some spying techniques.
11
12void POCDLL_API TestSpyKeys(HANDLE log) {
13  HandleToFile handle2file;
14  FILE *output = handle2file.Translate(log, "w");
15
16  if (RegisterHotKey(NULL, 1, 0, 0x42)) {
17    fprintf(output, "[GRANTED] successfully registered hotkey\r\n");
18    UnregisterHotKey(NULL, 1);
19  } else {
20    fprintf(output, "[BLOCKED] Failed to register hotkey. Error = %d\r\n",
21            ::GetLastError());
22  }
23
24  fprintf(output, "[INFO] Logging keystrokes for 15 seconds\r\n");
25  fflush(output);
26  std::wstring logged;
27  DWORD tick = ::GetTickCount() + 15000;
28  while (tick > ::GetTickCount()) {
29    for (int i = 0; i < 256; ++i) {
30      if (::GetAsyncKeyState(i) & 1) {
31        if (i >=  VK_SPACE && i <= 0x5A /*VK_Z*/) {
32          logged.append(1, static_cast<wchar_t>(i));
33        } else {
34          logged.append(1, '?');
35        }
36      }
37    }
38  }
39
40  if (logged.size()) {
41    fprintf(output, "[GRANTED] Spyed keystrokes \"%S\"\r\n",
42            logged.c_str());
43  } else {
44    fprintf(output, "[BLOCKED] Spyed keystrokes \"(null)\"\r\n");
45  }
46}
47
48void POCDLL_API TestSpyScreen(HANDLE log) {
49  HandleToFile handle2file;
50  FILE *output = handle2file.Translate(log, "w");
51
52  HDC screen_dc = ::GetDC(NULL);
53  COLORREF pixel_color = ::GetPixel(screen_dc, 0, 0);
54
55  for (int x = 0; x < 10; ++x) {
56    for (int y = 0; y < 10; ++y) {
57      if (::GetPixel(screen_dc, x, y) != pixel_color) {
58        fprintf(output, "[GRANTED] Read pixel on screen\r\n");
59        return;
60      }
61    }
62  }
63
64  fprintf(output, "[BLOCKED] Read pixel on screen. Error = %d\r\n",
65          ::GetLastError());
66}
67