LoginSignup
21
13

More than 1 year has passed since last update.

Linux上で Windowsの*.lnkファイル(ショートカットファイル)の転送先パスを取得する方法

Last updated at Posted at 2016-06-22

Windowsで作られるショートカットファイル(.lnk) の中身のパスをLinux上でみたいという
話があったのですが、なかなか良い方法がなく、ようやく見つけたのでご紹介します。

#そもそも、そういう需要があることはないと思うのでニッチな話ですが、
#自分で調べて、さんざんGoogle先生に聞いても良いのが見つからなかったのでググラビィリティ向上の為に
#記録しておこうと思います。

1. そもそも、lnkファイルとはなにか?

Windows OS上で利用できる、ショートカットファイルと呼ばれるものです。
中には、ファイルとしての属性(作成日、更新日)の他にこのショートカットファイルが指し示す元ファイルのパスが書かれています。

これにより、ショートカットファイルをクリックすると、元ファイルのパスに飛んでいったり、元ファイルを実行できたりします。

このショートカットファイルはバイナリーであり、テキストエディターなどで見ることはできません。

そのフォーマットは公開されています。しかし、お世辞にも分かりやすいものではないですし、パスを抽出したいだけなのに面倒なことはしたくありません。
[MS-SHLLINK]: Shell Link (.LNK) Binary File Format
https://msdn.microsoft.com/en-us/library/dd871305.aspx

Window Shortcut – LNK File Format | iThreats
https://ithreats.net/2009/05/27/window-shortcut-lnk-file-format/

2. Linux上でlnkファイルの中身を確認するには?

いくつか方法がありますが、バイナリーなのでバイナリー的に正規表現を書いたり、perlのパーサーがあったりするようです。

そもそもは Linux上で動く phpでlnkファイルの中身を見たいと思ったところが始まりです。

Extract path from .lnk file using PHP
http://www.witti.ws/blog/2011/02/21/extract-path-lnk-file-using-php
日本語ではパスが取得できませんでした。
また、lnkファイルの形式が変わったりすると取得できない可能性が高く採用できませんでした。

Windows LNK Parsing Utility
https://tzworks.net/prototype_page.php?proto_id=11
Linux上でコンパイルはできましたが、有料のDLLが必要で動きませんでした。

Windows shortcut (.lnk) parser in Java? - Stack Overflow
http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java/25501224#25501224
Javaでしたので選択肢に入りませんでした。
もしかしたら動くかもしれません。

Google Code Archive - Long-term storage for Google Code Project Hosting.
https://code.google.com/archive/p/lnk-parser/downloads
残念ながらLinuxで動かすのを想定していたのでNG。

J.A.F.A.T. - Archive of Forensics Tools
http://jafat.sourceforge.net/files.html
開発が2007年で止まっており、ショートカットファイルの UNCパスが取得できませんでした。

3. 結論

以下のページの
Liblnk - ForensicsWiki
http://forensicswiki.org/wiki/Liblnk
が最終的に利用した方法です。

Github上で今も開発されていて一番よさそうです。

libyal/liblnk: Library and tools to access the Windows Shortcut File (LNK) format
https://github.com/libyal/liblnk/

4. 利用方法

Linux上でコンパイルして利用します。

環境:
CentOS-6(.7)

shell
$ sudo yum install rpm-build python-devel gcc python2-configargparse.noarch
$ cd /usr/bin/
$ ln -s python-config python2-config
$ rpm -ivh http://mirror.yandex.ru/fedora/russianfedora/russianfedora/free/el/releases/6/Everything/x86_64/os/puias-release-6-2.R.noarch.rpm
$ yum install -y python3-devel
$ wget https://github.com/libyal/liblnk/releases/download/20160420/liblnk-alpha-20160420.tar.gz
$ mv liblnk-alpha-20160420.tar.gz liblnk-20160420.tar.gz
$ rpmbuild -ta liblnk-20160420.tar.gz
上記では、rpmbuild時に python2-config がないといわれたので python-configからシンボリックリンクを張っています。
また、rpmbuild時に python3-develがないといわれたので、適当にレポジトリを追加して python3-develを追加しています
(同時に python3も入ってしまっています)。

以下のようにファイルができるので、必要なものをインストールします。

~/rpmbuild/RPMS/<arch>/liblnk-<version>-1.<arch>.rpm
~/rpmbuild/RPMS/<arch>/liblnk-devel-<version>-1.<arch>.rpm
~/rpmbuild/RPMS/<arch>/liblnk-python-<version>-1.<arch>.rpm
~/rpmbuild/RPMS/<arch>/liblnk-tools-<version>-1.<arch>.rpm
~/rpmbuild/SRPMS/liblnk-<version>-1.src.rpm
shell
$ sudo yum localinstall  ~/rpmbuild/RPMS/<arch>/liblnk-tools-<version>-1.<arch>.rpm

使い方:ヘルプ

$ lnkinfo -h
lnkinfo 20160420

Use lnkinfo to determine information about a Windows Shortcut File (LNK).

Usage: lnkinfo [ -c codepage ] [ -hvV ] source

	source: the source file

	-c:     codepage of ASCII strings, options: ascii, windows-874,
	        windows-932, windows-936, windows-949, windows-950,
	        windows-1250, windows-1251, windows-1252 (default),
	        windows-1253, windows-1254, windows-1255, windows-1256
	        windows-1257 or windows-1258
	-h:     shows this help
	-v:     verbose output to stderr
	-V:     print version

$ lnkinfo "<ショートカットファイル>"

パス内に日本語がある場合は、
$ lnkinfo -c windows-932 "<ショートカットファイル>"

とすると良いでしょう。

参考資料:
How can You Open and Edit Windows .lnk Shortcut Files?
http://www.howtogeek.com/190897/how-can-you-open-and-edit-windows-.lnk-shortcut-files/

21
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
13