2023年9月26日 星期二

[Linux] OpenSSL AES-128 加解密 @ macOS 13.2.1, OpenSSL 3.1.2 1 Aug 2023

總覺得我是不是以前也寫過這類筆記? 查了下快十年前 XD 用的是 openssl des3 

由於工作中太常接觸 AES-128 加解密了,想要筆記一下方便未來查詢,而透過 openssl command 也方便交叉驗證程式是否正確,這次就仿 openssl des3 筆記:

明文:

% cat /tmp/input.txt
Hello World
% md5 /tmp/input.txt
MD5 (/tmp/input.txt) = e59ff97941044f85df5297e1c302d260
% hexdump /tmp/input.txt 
0000000 6548 6c6c 206f 6f57 6c72 0a64          
000000c

產生加解密的 Key 值:

% dd if=/dev/urandom of=/tmp/password bs=1 count=32
32+0 records in
32+0 records out
32 bytes transferred in 0.000303 secs (105611 bytes/sec)
% md5 /tmp/password 
MD5 (/tmp/password) = 92eeb8e1bec70865650e1f96e5cd1819
% hexdump /tmp/password 
0000000 cf23 5006 70d0 bf1e 0e9e a70c 10f0 ecd6
0000010 dc01 e156 d818 bff2 2e3e f859 28c9 a91d
0000020
% hexdump -v -e '/1 "%02x"' -n 16 /tmp/password 
23cf0650d0701ebf9e0e0ca7f010d6ec

產生加解密的 IV 值(其實同 Key 值產生即可,目前改用另一招):

% date | md5
5d6476c85eca3ec56fda4913f5578b83

使用 OpenSSL AES-128 加密:

% openssl enc -e -aes-128-cbc -in /tmp/input.txt -out /tmp/encrypt.txt -K 23cf0650d0701ebf9e0e0ca7f010d6ec -iv 5d6476c85eca3ec56fda4913f5578b83
% hexdump /tmp/encrypt.txt
0000000 c3d4 cb0a d845 182c 319e afdf b29c c484
0000010

使用 OpenSSL AES-128 解密:

% openssl enc -d -aes-128-cbc -in /tmp/encrypt.txt -out /tmp/output.txt -K 23cf0650d0701ebf9e0e0ca7f010d6ec -iv 5d6476c85eca3ec56fda4913f5578b83
% md5 /tmp/output.txt 
MD5 (/tmp/output.txt) = e59ff97941044f85df5297e1c302d260
% cat /tmp/output.txt 
Hello World

工作上很容易 Key 值是一個 32 bytes 的 binary 檔案,且不加入輸出至檔案的方式,可以立即看解密的內容,連續動作如下:

% openssl enc -d -aes-128-cbc -in /tmp/encrypt.txt -K $(hexdump -v -e '/1 "%02x"' -n 16 /tmp/password) -iv 5d6476c85eca3ec56fda4913f5578b83
Hello World

收工

2023年9月20日 星期三

Windows 開發筆記 - 使用 Command Line / CMD / ssh 與 PHP 8.2 / Composer / Git / VIM 開發環境 @ Windows 11

幫同事看了一下 Windows 開發環境,要在此環境使用 PHP8.2 與 PHP Laravel v10 framework,由於之前採用 xampp 管理套件被環境變數卡住。這些問題描述,瞬間拉回到學生時代在那邊設置 Windows %PATH% 環境變數 XD 我也忘了那時在幹嘛?推論是配置 Java 環境吧

目前就把手邊的 Windows 11 筆電拿來遠端,但懶得打開它。並且實際在 command line 測試會碰到幾個問題。

1. 從 https://windows.php.net/download/ 下載 VS16 x64 Non Thread Safe ,並解壓在 C:\php 目錄中,必須在設置 php.ini 。想要知道自己的 php.ini 位置,可以用 php.exe --ini

C:\php>php.exe --ini 
Configuration File (php.ini) Path: 
Loaded Configuration File:         (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

接著把 C:\php\php.ini-development 複製到 C:\php\php.ini 使用:

C:\php>copy php.ini-development php.ini     
複製了         1 個檔案。

後續就編輯 php.ini 開啟一些項目,在此就靠 C:\cygwin64\bin\vim.exe 當編輯器(若碰到滑鼠圈選文字難複製,記得關掉滑鼠模式 :set mouse-=a),主要打開一些 php.ini 註解:

; Directory in which the loadable extensions (modules) reside.
; https://php.net/extension-dir
;extension_dir = "./"
; On windows:
extension_dir = "ext"
; ...
extension=curl
extension=fileinfo
extension=gd
extension=intl
extension=mbstring
extension=exif
extension=mysqli
extension=openssl
extension=sqlite3

C:\php>php.exe --ini 
Configuration File (php.ini) Path: 
Loaded Configuration File:         C:\php\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

2. 下載 composer 後,預設會失敗:

C:\php>php.exe composer.phar self-update

In Factory.php line 648:
                                                                                                                          The openssl extension is required for SSL/TLS protection but is not available. If you can not enable the openssl extension, you can   
   disable this error, at your own risk, by setting the 'disable-tls' option to true.                          

self-update [-r|--rollback] [--clean-backups] [--no-progress] [--update-keys] [--stable] [--preview] [--snapshot] [--1] [--2] [--2.2] [--set-channel-only] [--] [<version>]

設置後:

C:\php>php.exe composer.phar self-update 
You are already using the latest available Composer version 2.6.3 (stable channel).

3. 在管理專案時,透過 C:\php\php.exe composer.phar install 時,會需要 GIT 指令,解法就是去官方安裝一下,安裝完的目錄位置在 C:\Program Files\Git 位置

C:\>"C:\Program Files\Git\bin\git.exe" --version
git version 2.42.0.windows.2

4. 回過頭來,更新環境變數 %PATH% 

C:\>git
'git' 不是內部或外部命令、可執行的程式或批次檔。
C:\>echo %PATH%
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Docker\Docker\resources\bin;C:\WINDOWS\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\Users\user\AppData\Local\Microsoft\WindowsApps;
C:\>set PATH=%PATH%;C:\Program Files\Git\bin\
C:\>git --version
git version 2.42.0.windows.2

如此在 Windows 的 command line (PowerShell) 環境下,也可以靠純指令做一點事了

2023年9月14日 星期四

Docker 開發筆記 - 建立私有的 DockerHub 服務 / My Private Docker Registry

對於公司內的 Docker 使用,當然就不能把一堆程式碼都擺在外頭的 dockerhub 來管理,所幸的 Docker Registry 也可以透過 Docker 快速建立,並且把資料儲存那段跟指定的機器儲存結合,瞬間立馬建置完畢:

Docker Registry - docs.docker.com/registry/

操作:

% docker run -d -p 5000:5000 -v /tmp/registry-stoarge:/var/lib/registry --name registry registry:2 
Unable to find image 'registry:2' locally
2: Pulling from library/registry
...: Pull complete 

% docker container list
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                    NAMES
1ab332dd4bb5   registry:2   "/entrypoint.sh /etc…"   21 seconds ago   Up 20 seconds   0.0.0.0:5000->5000/tcp   registry

將 image 發佈到指定的 Docker Registry server:

% docker image list
REPOSITORY             TAG       IMAGE ID       CREATED        SIZE
my/test                1.0.0     9b400be021d8   25 hours ago   457MB
registry               2         0030ba3d620c   5 weeks ago    24.1MB

% docker tag my/test:1.0.0 localhost:5000/my/test
% docker push localhost:5000/my/test
Using default tag: latest
The push refers to repository [localhost:5000/my/test]
............: Pushed 
............: Pushed 
............: Pushed 
latest: digest: sha256:...........
 size: ...

列出指定的 Docker Registry server 上的 image list:

% curl localhost:5000/v2/_catalog
{"repositories":["my/test"]}

列出指定的 Docker Registry server 上的 image 上的 tag list:

% curl localhost:5000/v2/my/test/tags/list
{"name":"my/test","tags":["latest"]}

查看一下 /tmp/registry-stoarge 內已經儲存的資料結構:

% tree -L 7 /tmp/registry-stoarge
/tmp/registry-stoarge
└── docker
    └── registry
        └── v2
            ├── blobs
            │   └── sha256
            │       ├── ...
            │       │   └── ...
            │       └── ...
            │           └── ...
            └── repositories
                └── my
                    └── test
                        ├── _layers
                        ├── _manifests
                        └── _uploads

30 directories, 0 files

最後,使用 Docker Registry server 上的 image 做事:

% docker run -it localhost:5000/my/test
root@.....:/# ls

2023年9月7日 星期四

Windows 開發筆記 - 開機自動啟動 SSH Reverse Tunnel / autossh @ Windows 11, cygwin

視窗鍵 + R 輸入 shell:startup

想說要善加利用 Windows 筆電,就想到把它規劃成算力單位後,以及思考如何自動化叫他做事。在資安角度上就可採用 SSH Reverse Tunnel 方案,讓筆電開機啟動後,建立一個連線到指定的機器候命。如此,在 Windows 筆上安裝了一些服務後,就可以用遠端 Port Forwarding 的方式存取到。

首先,要先找到 autossh 這種用法,不然單建立一個 ssh 掛了很麻煩的。雖然有一些 github 的可挑,但整體上要找一個夠信任的來源,最後選擇知名的 cygwin ,就順便安裝 autossh, tmux, vim, wget, lftp, git, zip, unzip 等,如此 autossh.exe 就搞定,位置在 C:\cygwin64\bin\autossh.exe

之前的文章已提到,我在 Windows 11 下已啟用 OpenSSH server 了,那我可以測試把 openssh server 服務建立反向的連線(須留意 cygwin 認定的使用者家目錄跟 Windows PowerShell 的不一樣,需要先建立好 keypair 等資料)

C:\cygwin64\bin\autossh.exe -M 0 -N -R 10022:localhost:22 ServerUser@RemotServer

如此,在指定的機器(RemoteServer)上,就可以測試連線:

$ telnet localhost 10022
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
SSH-2.0-OpenSSH_for_Windows_8.6
^C^C
Connection closed by foreign host.

對於 cygwin 方面,目前採用 Windows 內建的 OpenSSH server 方案,連入後是 Windows Powershell 環境,若想切換到 cygwin ,可以多打一下:

Microsoft Windows [版本 10.0.22621.2215]

(c) Microsoft Corporation. 著作權所有,並保留一切權利。


user@WINDOWS-DESKTOP C:\Users\user>c:\cygwin64\Cygwin.bat


user@windows-desktop ~

$ 


對於 cygwin 用法,正規的安裝軟體還是透過原本的 setup.exe 去擴充,然而,可以去下載 github.com/transcode-open/apt-cyg 來使用,他可以提供一些便利的 command line 安裝套件的方式(但實務上不幸踩過失敗),當作一個備用方式:

user@windows-desktop ~

$ curl -s https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg > apt-cyg


user@windows-desktop ~

$ chmod 700 ./apt-cyg 


user@windows-desktop ~

$ ./apt-cyg 

NAME

  apt-cyg - package manager utility


SYNOPSIS

  apt-cyg [operation] [options] [targets]


DESCRIPTION

  apt-cyg is a package management utility that tracks installed packages on a   

  Cygwin system. Invoking apt-cyg involves specifying an operation with any     

  potential options and targets to operate on. A target is usually a package    

  name, file name, URL, or a search string. Targets can be provided as command  

  line arguments.


OPERATIONS

  install

    Install package(s).


  remove

    Remove package(s) from the system.


  update

    Download a fresh copy of the master package list (setup.ini) from the       

    server defined in setup.rc.


  download

    Retrieve package(s) from the server, but do not install/upgrade anything.   


  show

    Display information on given package(s).


  depends

    Produce a dependency tree for a package.


  rdepends

    Produce a tree of packages that depend on the named package.


  list

    Search each locally-installed package for names that match regexp. If no    

    package names are provided in the command line, all installed packages will 

    be queried.


  listall

    This will search each package in the master package list (setup.ini) for    

    names that match regexp.


  category

    Display all packages that are members of a named category.


  listfiles

    List all files owned by a given package. Multiple packages can be specified 

    on the command line.


  search

    Search for downloaded packages that own the specified file(s). The path can 

    be relative or absolute, and one or more files can be specified.


  searchall

    Search cygwin.com to retrieve file information about packages. The provided 

    target is considered to be a filename and searchall will return the

    package(s) which contain this file.


  mirror

    Set the mirror; a full URL to a location where the database, packages, and  

    signatures for this repository can be found. If no URL is provided, display 

    current mirror.


  cache

    Set the package cache directory. If a file is not found in cache directory, 

    it will be downloaded. Unix and Windows forms are accepted, as well as      

    absolute or regular paths. If no directory is provided, display current     

    cache.


OPTIONS

  --nodeps

    Specify this option to skip all dependency checks.


  --version

    Display version and exit.


下一步回歸到正題 - 開機自動執行,則是可以參考微軟官方文件 - 新增的應用程式以在 Windows 10 啟動時自動執行。此例在 Windows 11 上,透過 視窗鍵+R 執行 shell:startup 可快速開啟對應的目錄位置 ( %HOME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup ),只需在此目錄添加幾個 bat 檔案,開啟就會幫我運行這指令了,如:

00_ssh-reverse-tunnel_openssh-server.bat

內容:

C:\cygwin64\bin\autossh.exe -M 0 -N -R 10022:localhost:22 ServerUser@RemotServer

開啟後,他會佔一個小視窗。由於那台筆電本身就是閒置的,所以佔著小視窗反而更好觀測 autossh 運作是否正常

收工!

2023年9月5日 星期二

掰了 小米便攜相片印表機 白燈閃爍 電源燈閃爍


在 2021年夏天疫情期間買了這小玩意,當初想說拿來列印出貼紙給小孩把玩會是滿有趣的小品,殊不知印個不到10張就被冰封起來 XD 接著 2023年夏天,剛好想拍小孩的畫,在輸出成小貼紙時,發現這設備異常,想說可能被閒置一年了,起手式先充個電,只是充不到一小時後,看到充電孔旁的燈一直閃一直閃,查閱說明書的 "電量指示燈" 也沒這個狀況,查詢良久後,在某個商店的描述才看到「閃爍白燈---電量不足(建議至少3個月需充電一次)」... 最好這些東西不用的時候還會記得拿出來充...就這樣設備就殘了。


只能在上班時間致電小米電話客服(還找不太到寄信問),客服也只能像 NPC 一樣回已知的資訊,如回應保固內可換貨,保固後就沒了,晚上心一橫就用力拆機,總共有三顆螺絲,一開始只能拆一顆,拆完外殼才能在拆兩顆,最後蠻力拔起來才看到鋰電池的資訊等等


看來這產品因為鋰電池的狀態廢了 Orz 把電池接線拆掉後,直接接電源可以正常啟動、連上藍芽、在米家App可連上、升韌體等等,很可惜要列印照片時,挑選完照片準備列印,接著處理到一半就 GG 自動關機,不知是不是瞬間電力需求太大?

原本想說不插鋰電池還可以當個正常工具用,看來都得放棄了

發個文紀念它吧!這是一個完全不屬於我的設備,要價也超過一千五台幣...