備忘録

備忘録

Packer Detectorまとめ

Ⅰ. はじめに

解析対象のバイナリがどの Executable Packer を使っているかを自動的に判定するツールです。

Ⅱ. Packer Detector 一覧

XAPKDetector

https://github.com/horsicq/XAPKDetector
Androidアプリ用です

XPEViewer

https://github.com/horsicq/XPEViewer

Exeinfo PE

http://exeinfo.atwebpages.com/

Detect It Easy

http://ntinfo.biz/

PEiD

http://www.peid.info/
2017/05/07時点 公式サイトにアクセス出来ませんでした
それなりに古くからあります

RDG Packer Detector

http://www.rdgsoft.net/
それなりに優秀ですがよくフリーズします

PE Detective

http://ntcore.com/pedetective.php
マルウェア解析用のVMにインストールされているツール

Nauz File Detector

https://github.com/horsicq/Nauz-File-Detector

その他

C++ で全ての例外を try catch 出来るようにする方法

Ⅰ. はじめに

Visual Studioのデフォルト設定ではWindowsが発行する「システム的な例外」を捉えることはできません。

  • 無効なメモリアドレスへのアクセス
  • 0での割り算

等が「システム的な例外」に該当します。

また、「『システム的な例外』をプログラム上で扱う事」を構造化例外処理(SEH: Structured Exception Handling)といいます。
SEH は Windows のみで有効です。

Ⅱ. やり方

1. プロジェクトのプロパティを開く

2. 構成プロパティ → C/C++ → コード生成

C++ の例外を有効にする」を「はい - SEH 例外あり(/EHa)」に変更する。

f:id:kagasu:20170504223041p:plain

3. メモリアクセス違反が発生するサンプルプログラムを実行する

#include <iostream>

using namespace std;

int main()
{
  try
  {
    *(int *)0 = 0;
  }
  catch (...)
  {
    cout << "catch exception" << endl;
  }

  return 0;
}

実行結果

f:id:kagasu:20170504223215p:plain

C++で複数プロセスから読み書き可能な共有メモリを作る

Ⅰ. はじめに

あるプロセスのメモリ空間には他のプロセスからアクセスする事ができません。
OpenProcess してアクセス権を得てもメモリ上のどのアドレスに何のデータが保存されているか簡単に分かりません。
この問題は複数プロセスで共有して使えるメモリ空間(共有メモリ)を利用することで解決できます。

Ⅱ. サンプル

  • 1. 1秒毎に共有メモリにデータを書き込むだけのプログラム
  • 2. 1秒毎に共有メモリからデータを読み込むだけのプログラム

上記2つのサンプルです。

1. 共有メモリにデータを書き込むだけのプログラム
#include <windows.h>
#include <iostream>

using namespace std;

void main()
{
  auto name = "hoge";
  auto size = 4;

  HANDLE hSharedMemory = CreateFileMapping(NULL, NULL, PAGE_READWRITE, NULL, size, name);
  auto pMemory = (int*)MapViewOfFile(hSharedMemory, FILE_MAP_ALL_ACCESS, NULL, NULL, size);

  for (int i = 0; i < 10; i++)
  {
    *pMemory = i;
    cout << i << endl;
    Sleep(1000);
  }

  UnmapViewOfFile(pMemory);
  CloseHandle(hSharedMemory);
}
2. 共有メモリからデータを読み込むだけのプログラム
#include <windows.h>
#include <iostream>

using namespace std;

void main()
{
  auto name = "hoge";
  auto size = 4;

  HANDLE hSharedMemory = CreateFileMapping(NULL, NULL, PAGE_READWRITE, NULL, size, name);
  auto pMemory = (int*)MapViewOfFile(hSharedMemory, FILE_MAP_ALL_ACCESS, NULL, NULL, size);

  for (int i = 0; i < 10; i++)
  {
    cout << *pMemory << endl;
    Sleep(1000);
  }

  UnmapViewOfFile(pMemory);
  CloseHandle(hSharedMemory);
}

Ⅲ. カーネルモードについて

カーネルモードとユーザーモードからアクセス可能な領域を作成するには名前を工夫する必要があります。
カーネルモード側はそのままで問題ありませんが、ユーザーモード側の名前を

auto name = "Global\\hoge";

とする必要があります。
また、場合によっては実行するユーザーをSYSTEMまで引き上げる必要があります。

参考
http://nahitafu.cocolog-nifty.com/nahitafu/2013/03/expartan-6twind.html
http://kagasu.hatenablog.com/entry/2017/09/27/191922

Ⅳ. 実行結果

f:id:kagasu:20170503003512g:plain

その他

以下記事の方法でC#でも同じメモリ空間を利用できます。
https://kagasu.hatenablog.com/entry/2021/10/16/132558

C++でShift-JIS, UTF-8, UTF-16 BOM有無とエンディアンを考慮してファイルの読み込みをする

Ⅰ. はじめに

タイトルの通り「C++でShift-JIS, UTF-8, UTF-16 BOM有無を考慮してファイルの読み込みをする」方法です。
参考のURLには重要な内容が含まれているので目を通してください。

Ⅱ. 環境

Ⅲ. プログラム

ファイルの内容を全て読み込みむサンプルです。

Shift-JIS

#include <iostream>
#include <fstream>
#include <string>

ifstream ifs("shift-jis.txt");
string str((istreambuf_iterator<char>(ifs)), istreambuf_iterator<char>());

cout << str << endl;

UTF-8 (BOM なし)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-8N.txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf8<wchar_t>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl;

UTF-8 (BOM あり)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-8(BOM).txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf8<wchar_t, 0x10ffff, consume_header>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl

UTF-16トルエンディアン (BOM なし)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-16LEN.txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf16<wchar_t, 0x10ffff, little_endian>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl

UTF-16トルエンディアン (BOM あり)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-16LE(BOM).txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf16<wchar_t, 0x10ffff, consume_header>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl;

UTF-16 ビッグエンディアン (BOM なし)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-16BEN.txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf16<wchar_t>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl;

UTF-16 ビッグエンディアン (BOM あり)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-16BE(BOM).txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf16<wchar_t, 0x10ffff, consume_header>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl;

C++ ファイルを全て読み込む

Ⅰ. はじめに

STL を使ってファイルを全て読み込む(全行読み込む)方法です。

※追記
C++でShift-JIS, UTF-8, UTF-16 BOM有無とエンディアンを考慮してファイルの読み込みをする - 備忘録

Ⅱ. サンプル

test.txt

aiueo

Source.cpp

#include <iostream>
#include <fstream>
#include <string>

int main()
{
  std::ifstream ifs("test.txt");
  std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
  std::cout << str << std::endl;

  return 0;
}

Ⅲ. 実行結果

f:id:kagasu:20170501215610p:plain