获取HANDLE对应的文件路径

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
#include <Windows.h>
#include <psapi.h>
#include <wchar.h>
 
WCHAR *getFilePathByHandle(HANDLE hFile, WCHAR* filePath, int filePathLen)
{
	DWORD dwFileSizeHi = 0;
	DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);
	HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, dwFileSizeLo, NULL);
 
	filePath[0] = 0;
	if (NULL != hFileMap)
	{
		void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
		if (NULL != pMem)
		{
			if (0 == GetMappedFileNameW(GetCurrentProcess(), pMem, filePath, filePathLen))
			{
				// GetLastError
			}
			UnmapViewOfFile(pMem);
		}
		CloseHandle(hFileMap);
	}
	return filePath;
}
 
wchar_t toLower(wchar_t ch)
{
	if ('A' <= ch && ch <= 'Z')
		ch = ch - 'A' + 'a';
	return ch;
}
 
int isNoCaseBeginWith(wchar_t* l, wchar_t* r)
{
	if (*l == 0 && *r == 0)
		return 1;
	if (*l == 0 || *r == 0)
		return 0;
	for (; 0 != *l && 0 != *r; l++, r++)
	{
		if (toLower(*l) != toLower(*r))
			return 0;
	}
	return 1;
}
 
WCHAR* getDosFilePath(WCHAR *dosPath, int len, WCHAR* kernelPath)
{
	wchar_t path[3] = L"C:";
	wchar_t szBuf[MAX_PATH] = { 0 };
	for (wchar_t ch = L'A'; ch <= L'Z'; ch++)
	{
		path[0] = ch;
		QueryDosDevice(path, szBuf, MAX_PATH);
		if (isNoCaseBeginWith(kernelPath, szBuf))
		{
			swprintf_s(dosPath, len, L"%s%s", path, kernelPath + wcslen(szBuf));
			return dosPath;
		}
	}
	dosPath[0] = 0;
	return dosPath;
}
 
int main()
{
	HANDLE hFile = CreateFileW(L"d:\\test.txt",
		GENERIC_READ,
		FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
		NULL,
		OPEN_EXISTING,
		0x00,
		NULL);
 
	if (INVALID_HANDLE_VALUE == hFile)
	{
		return 0;
	}
 
	WCHAR path[1024];
	getFilePathByHandle(hFile, path, 1024);
 
	WCHAR dosPath[1024];
	getDosFilePath(dosPath, 1024, path);
 
	CloseHandle(hFile);
 
	return 0;
}

南北朝年表

五胡十六国

1
2
3
4
5
6
7
8
9
10
11
后赵/328-352/
	石勒/明帝/高鼻深目/诛杀司马氏五十八位王爷
	石虎/武帝/
冉魏/350-352/
	冉闵/平帝/
前秦/351-394/
	苻坚/宣昭帝/
后秦/384-417/
	姚苌/昭武帝/330-393
后燕/384-407/
	慕容垂/武帝/

北朝

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
北魏/386-534/
	拓跋珪/道武帝/371-409/立太子杀生母
	拓跋嗣/明元帝/392-423/
	拓跋焘/太武帝/408-452/灭佛,崔浩(381-450)
	元宏/孝文帝/467-499/冯太后/494年迁都/兰陵公主
	元诩/孝明帝/510-528
	胡充华/元恪之妃/毒死儿子元诩
	尔朱荣/高鼻深目/河阴之变
	元子攸
	尔朱兆/杀皇帝/喜欢高欢
	元修/独孤信
东魏/534-550/
西魏/535-556/
北齐/550-577/
	高欢/神武帝/496-547/大小尔朱皇后
	高澄/文襄帝/521-549
	高洋/文宣帝/529-559
	高殷/闵悼王/545-561
	高演/孝昭帝/535-561
	高湛/武成帝/537-568
	高纬/556-577/兰陵王
	高延宗/
	高恒/570-577
	高绍义
北周/557-581/

南朝

1
2
3
4
5
6
7
8
9
10
11
刘宋/420-479/
	刘裕/武帝/
	刘义隆/文帝/
萧齐/479-502/
	萧道成/高帝/
	萧宝卷/东昏侯/483-501/Masochism
萧梁/502-557/
	萧衍/武帝/
南陈/557-589/
	陈霸先/武帝/
	陈叔宝/后主/

refer to:
http://www.360doc.com/content/18/1206/17/38656042_799787806.shtml
https://baike.baidu.com/item/%E5%8C%97%E9%AD%8F/914008?fr=aladdin
https://baike.baidu.com/item/%E5%8C%97%E9%BD%90/913858?fr=aladdin
老蔡通史

自定义printf输出格式

glibc版,代码可参考

1
2
3
//libstrongswan/utils/printf_hook/printf_hook_glibc.c
register_printf_specifier
register_printf_function

要注意的是,由于gcc在编译时不认识自定义格式,默认会提示warning。
禁止此提示的方法是在gcc命令参数中加入

1
-Wno-format

vstr from http://www.and.org/vstr/
代码可参考

1
2
//libstrongswan/utils/printf_hook/printf_hook_vstr.c
vstr_fmt_add

在win32下,libstrongswan自已写了一个printf,代码在位置在

1
2
//libstrongswan/utils/printf_hook/printf_hook_builtin.c
builtin_vsnprintf

设置线程中止时的回调函数

pthread版

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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
 
#define WhThreadHandle pthread_t
#define WH_THREAD_DEF(_proc, _arg) void *_proc(void *_arg)
#define whThreadCreate(_handle, _loop, _param) pthread_create(&_handle, NULL, _loop, _param)
 
void cleanup(void *p)
{
	printf("thread killed.\n");
}
 
WH_THREAD_DEF(func1, arg)
{
	int oldState;
	pthread_cleanup_push(cleanup, NULL);
 
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldState);
	while (1)
		sleep(1); // here must call sleep, or else cleanup will not be called.
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
 
	pthread_cleanup_pop(0); // here if want cleanup called, set param to non-zero.
	return NULL;
}
 
WhThreadHandle threadHandle;
 
void onSignal(int sign)
{
    switch (sign)
    {
    case SIGINT:
	pthread_cancel(threadHandle);
        printf("process ctrl+c pressed.\n");
        break;
    }
}
 
int main()
{
	signal(SIGINT, onSignal);
 
	whThreadCreate(threadHandle, func1, 0);
 
	while (1)
		;
	return 1;
}

linux驱动备忘录

驱动签名

1
2
3
4
5
CONFIG_MODULE_SIG=y
# CONFIG_MODULE_SIG_FORCE is not set
CONFIG_MODULE_SIG_ALL=y
 
${KERNEL_SRC}/scripts/sign-file sha512 ${KERNEL_SRC}/certs/signing_key.pem ${KERNEL_SRC}/certs/signing_key.x509 hello.ko

驱动调试

debugee

1
2
3
4
5
6
7
MOD_NAME=simple_mod
insmod ./${MOD_NAME}.ko
MOD_TEXT=`cat /sys/module/${MOD_NAME}/sections/.text`
MOD_DATA=`cat /sys/module/${MOD_NAME}/sections/.data`
MOD_BSS=`cat /sys/module/${MOD_NAME}/sections/.bss`
MOD_ADD="add-symbol-file /path/to/${MOD_NAME}.ko ${MOD_TEXT} -s .data ${MOD_DATA} -s .bss ${MOD_BSS}"
echo ${MOD_ADD}

debugger

1
(gdb) ${MOD_ADD}

refer to:
https://www.cnblogs.com/rivsidn/p/9481037.html
https://www.cnblogs.com/powerrailgun/p/12161295.html
https://blog.csdn.net/chdhust/article/details/8820628

利用vmware调试kernel

在vmware虚拟机A上

1
2
apt install libssl-dev
apt install libncurses-dev

从文后链接中下载kernel源码,比如版本4.15.18,解压编译

1
2
3
4
make menuconfig
make
make modules_install
make install

A关机,克隆A为B,A的虚拟机设置中增加串口

1
2
3
使用命名管道:\\.\pipe\com_1
该端是服务器。
另一端是虚拟机。

B的虚拟机设置中增加串口

1
2
3
使用命名管道:\\.\pipe\com_1
该端是客户端。
另一端是虚拟机。

A以新编译的内核引导,可能事先要加大内存;B以旧内核引导。在B中运行

1
cat < /dev/ttyS1

在A中运行

1
echo Helloworld > /dev/ttyS1

如果B中回显消息,说明串口连通。
编辑A中的/boot/grub/grub.cfg,找到新编译内核启动项,在handoff后加入kgdbwait kgdboc=ttyS1,115200 nokaslr,如

1
linux /boot/vmlinuz-4.15.18 root=UUID=7ccc722d-2cbd-4597-a367-e0635333ddbf ro quiet splash $vt_handoff kgdbwait kgdboc=ttyS1,115200 nokaslr

B退出cat程序,A重启以新编译的内核引导到kdb等待状态。在B中kernel源码根目录运行

1
2
3
4
5
gdb vmlinux
set serial baud 115200
target remote /dev/ttyS1
lx-symbols
c

A中进入系统后可用下面语句触发调试

1
2
3
#echo 1 > /proc/sys/kernel/sysrq
#echo kms,kbd > /sys/module/kgdboc/parameters/kgdboc
echo g > /proc/sysrq-trigger

vmware串口调试提速的方法为,关掉此虚拟机,编辑启动文件.vmx,加入

1
serial1.pipe.charTimePercent = "25"

其中serial1要因时而变,要查找同文件中是否有同样名称。
运行后很稳定的话,还可将25再改小再试。

注1:因为新内核的KASLR机制,如果在内核启动命令行中不加入nokaslr,调试时看到不到堆栈也下不了断点。Cannot insert breakpoint。

注2:因为源地址无法访问了,有个未尝试的点摘抄下来

1
2
3
在没有编译内核的情况下,还可以直接通过修改虚拟机的启动文件.vmx,添加:
debugStub.listen.guest32 = "TRUE"
然后在调试机器中通过:target remote ip:8832(8864)来调试,ip为真实机器的IP

refer to:
https://mirrors.edge.kernel.org/pub/linux/kernel/
https://stackoverflow.com/questions/49360506/in-kgdb-i-cannot-set-the-breakpoint
https://askubuntu.com/questions/964540/gdb-qemu-cant-put-break-point-on-kernel-function-kernel-4-10-0-35
https://www.cnblogs.com/xiaofool/p/5377737.html
https://docs.vmware.com/en/VMware-Workstation-Pro/12.0/com.vmware.ws.using.doc/GUID-B285C62D-1E7E-49E8-81D6-77910B2024A6.html