Tuesday, January 25, 2022

How to install vs code-server in Windows WSL2 and setup firewall rules to access from other LAN machines

(1) Start Ubuntu 20.04 of WSL2 and install code-server
curl -fsSL https://code-server.dev/install.sh | sh

(2) Modify bind IP address port settings and password of the code-server
Get Ubuntu IP address
ifconfig eth0 | grep 'inet '
Edit bind address and password in
~/.config/code-server/config.yaml

(3) start code-server in Ubuntu
code-server

(4) Modify firewall rule and add forward port in Windows using PowerShell with Admin Right. Create the following file vscode_server_port_wsl2.ps1 and run in PowerShell with Admin Right
.\vscode_server_port_wsl2.ps1
vscode_server_port_wsl2.ps1    Select all
# Get network information $network_information = bash.exe -c "ifconfig eth0 | grep 'inet '"; # Define ip address pattern $ip_address_pattern = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"; # Get ip address $ip_address = $network_information -match $ip_address_pattern # Proceed if ip address is found if ($ip_address) { # Get remote address $remote_address = $matches[0]; } # Define rule name $rule_name = "VS Code Server (WSL2)"; # Define port for the VS Code Server - default is 8080 $port = 8080; # Check if rule exists $rule_exists = Get-NetFirewallRule -DisplayName $rule_name 2> $null; # Proceed if rule doesn't exists if (-Not $rule_exists) { # Add new inbound rule New-NetFirewallRule -DisplayName $rule_name -Direction Inbound -LocalPort $port -Protocol TCP -Action Allow } # Define listen address $listen_address = "0.0.0.0"; # Update ip address iex "netsh interface portproxy delete v4tov4 listenport = $port listenaddress = $listen_address"; iex "netsh interface portproxy add v4tov4 listenport = $port listenaddress = $listen_address connectport = $port connectaddress = $remote_address";

Do this in powershell with admin right.

Set-ExecutionPolicy Unrestricted -Force





(5) Test access of code-server from other machine in the LAN
Use browser to access this address and enter password as in
http://<IP address of your windows machine>:8080/

(6) Test access of code-server from VPN connection from Internet.
First use OpenVPN to connect to your home/office network
Then use browser to access
http://<IP address of your windows machine>:8080/

(7) If you have installed docker for Windows, this command will help you to install and run code-server in docker.
command prompt shell    Select all
# Testing on local host machine docker run --name code-server -p 127.0.0.1:8080:8080 -v "$HOME/.config:/home/coder/.config" -v "$HOME/:/home/coder/project" -e "USER=$env:UserName" -e "DOCKER_USER=$env:UserName" codercom/code-server:latest #or publish to the host machine IP address with port no 8081, to allow access from other LAN machines docker run --name code-server -p 8081:8080 -v "$HOME/.config:/home/coder/.config" -v "$HOME/:/home/coder/project" -e "USER=$env:UserName" -e "DOCKER_USER=$env:UserName" codercom/code-server:latest # stop container docker stop code-server # start container docker start code-server
run this command to get the password in config.yaml and then edit it of wanted.
docker exec -it code-server cat .config/code-server/config.yaml
or
type $HOME\.config\code-server\config.yaml
. . .

Thursday, January 13, 2022

Decompile and Recompile an Android APK using apktool

You need these tools to do the jobs

Android Studio (with build tools such as keytool, jarsigner)
apktool download from https://bitbucket.org/iBotPeaches/apktool/downloads/ and extract and rename to apktool.jar
apktool wrapper script https://raw.githubusercontent.com/iBotPeaches/Apktool/master/scripts/windows/apktool.bat
Java SE 8 JDK download from https://www.oracle.com/java/technologies/javase/javase8u211-later-archive-downloads.html
dex2jar download from https://sourceforge.net/projects/dex2jar/
JD_GUI download from http://jd.benow.ca/
smalidea download from https://github.com/JesusFreke/smalidea
Online Decompiler for apk https://appscms.com/apk-decompiler
jadx download from https://github.com/skylot/jadx/releases

apktool — tool for reverse engineering Android apk files. In this case we are using to extract files from apk and rebuild.
keytool — Java tool for creating keys/certs, that comes with the Java SE JDK.
jarsigner Java tool for signing JAR/APK files, that comes with the Java SE JDK.
zipalign — archive alignment tool, that comes with the Android SDK.
JD-GUI — To view java code
dex2jar — Converts Android dex files to class/jar files.
smalidea is a smali language plugin for AS and to edit the file in Android Studio
jadx — convert single smali file to java code to test the modification of smali

For smali syntax, please refer to this doc
https://programmer.help/blogs/smali-introduction-manual.html
or http://source.android.com/devices/tech/dalvik/dalvik-bytecode.html

Instructions:
First, Take any apk file and unpack(decompile) it. This will create an “application” directory with assets, resources, compiled code, etc.
# To decompile an apk
apktool d -r -s my_application.apk
or
apktool d my_application.apk

Then, you can use Android Studio to open the smali file and edit it. Use APKRepacker to test the single smali file conversion.

# To recompile(build) the apk
apktool b -f -d my_application -o my_application2.apk

After recompiling (building) the apk the new apk (my_application2.apk) will be generated in directory.

The APK must be signed before you run on your device. If you want an official key from google play signing key, you should register with https://play.google.com/console/u/0/signup first and pay $25 registration key
Before signing an apk, create a self-signing key if you don't have an existing one from google play. If prompted for a password, create your own password.
These tools are installed withn JDK e.g. in "c:\Program Files\Java\jdk1.8.0_301\bin\". Add this to the environment PATH.

macOS binary are here /Applications/Android Studio.app/Contents/jre/Contents/Home/bin

# To generate a key. And remember the store password
keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

# if want to generate SHA256withRSA keystore (in order to get rid of the warning of security when signing with SHA1), use this command
keytool -genkey -v -keystore my-release-key256.keystore -alias mykey256 -sigalg SHA256withRSA -keyalg RSA -keysize 2048 -validity 10000 -deststoretype pkcs12

Now sign the APK with the key:
# Sign the apk
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore my_application2.apk alias_name

# if Sign with SHA256withRSA (in order to get rid of the warning of security when signing with SHA1)
jarsigner -verbose -sigalg SHA256withRSA -keystore my-release-key256.keystore my_application2.apk mykey256

# Verify apk
jarsigner -verify -verbose -certs -keystore my-release-key.keystore my_application2.apk

# Verify apk with SHA256withRSA
jarsigner -verify -verbose -certs -keystore my-release-key256.keystore my_application2.apk

# Finally, the apk must be aligned for optimal loading:
zipalign is installed with Android SDK e.g. in c:\Users\User\AppData\Local\Android\Sdk\build-tools\28.0.3\zipalign.exe Add this to the environment PATH.
zipalign -v 4 my_application2.apk my_application2-aligned.apk
you have a my_application2-aligned.apk file, which you can install onto your device.


Other useful tools using command prompt under Windows
# Command Prompt to list android virtual devices %USERPROFILE%\AppData\Local\Android\Sdk\emulator\emulator -list-avds
# PowerShell command to list android virtual devices & $env:UserProfile\AppData\Local\Android\Sdk\emulator\emulator -list-avds
# Command Prompt to list android virtual device in background
start /b %USERPROFILE%\AppData\Local\Android\Sdk\emulator\emulator -avd Pixel_3a_API_30_x86 -netdelay none -netspeed full
# Command Prompt to show running emulator devices
%USERPROFILE%\AppData\Local\Android\Sdk\platform-tools\adb devices
# Command Prompt to attach to running emulator device shell
%USERPROFILE%\AppData\Local\Android\Sdk\platform-tools\adb -s emulator-5554 shell
# Command Prompt to install apk to running emulator device
%USERPROFILE%\AppData\Local\Android\Sdk\platform-tools\adb -s emulator-5554 install %USERPROFILE%\Downloads\my_application2-aligned.apk
export ANDROID_HOME="%USERPROFILE%\AppData\Local\Android\Sdk"




Other useful tools using Terminal under macOS
# macOS Terminal to list android virtual devices $HOME/Library/Android/sdk/emulator/emulator -list-avds
# macOS Terminal to show running emulator devices
$HOME/Library/Android/sdk/platform-tools/adb devices
# macOS Terminal to attach to running emulator device shell
$HOME/Library/Android/sdk/platform-tools/adb -s emulator-5554 shell
# macOS Terminal to install apk to running emulator device
$HOME/Library/Android/sdk/platform-tools/adb -s emulator-5554 install ~/Downloads/my_application2-aligned.apk
export ANDROID_HOME="$HOME/Library/Android/sdk" export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/Contents/Home" export PATH=$ANDROID_HOME/build-tools/30.0.3:$ANDROID_HOME/platform-tools:$JAVA_HOME/bin:$PATH


Setup sdk and decompile tools in WSL2 Ubuntu 20.04    Select all
#!/bin/bash sudo apt install -y libarchive-tools export ANDROID_SDK_TOOLS_VERSION=6858069 export ANDROID_SDK_TOOLS_CHECKSUM=87f6dcf41d4e642e37ba03cb2e387a542aa0bd73cb689a9e7152aad40a6e7a08 export ANDROID_HOME="/opt/android-sdk-linux" export ANDROID_SDK_ROOT="/opt/android-sdk-linux" export ANDROID_DECOMPILE_TOOLS="$ANDROID_HOME/Decompile" curl -s https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS_VERSION}_latest.zip > ./tools.zip && echo "$ANDROID_SDK_TOOLS_CHECKSUM ./tools.zip" | sha256sum -c && sudo mkdir -p $ANDROID_HOME && sudo chown $(id -u):$(id -g) $ANDROID_HOME && unzip -qq ./tools.zip -d $ANDROID_HOME && rm -v ./tools.zip sudo mkdir -p $ANDROID_HOME/licenses/ && sudo chown $(id -u):$(id -g) $ANDROID_HOME/licenses && echo "8933bad161af4178b1185d1a37fbf41ea5269c55\nd56f5187479451eabf01fb78af6dfcb131a6481e\n24333f8a63b6825ea9c5514f83c2829b004d1fee" > $ANDROID_HOME/licenses/android-sdk-license && echo "84831b9409646a918e30573bab4c9c91346d8abd\n504667f4c0de7af1a06de9f4b1727b84351f2910" > $ANDROID_HOME/licenses/android-sdk-preview-license --licenses && yes | $ANDROID_HOME/cmdline-tools/bin/sdkmanager --licenses --sdk_root=${ANDROID_SDK_ROOT} curl -OL https://raw.githubusercontent.com/MobileDevOps/android-sdk-image/master/packages.txt $ANDROID_HOME/cmdline-tools/bin/sdkmanager --update --sdk_root=${ANDROID_SDK_ROOT} && while read -r pkg; do PKGS="${PKGS}${pkg} "; done < $HOME/packages.txt && $ANDROID_HOME/cmdline-tools/bin/sdkmanager $PKGS > /dev/null --sdk_root=${ANDROID_SDK_ROOT} curl -OL https://nchc.dl.sourceforge.net/project/dex2jar/dex2jar-2.0.zip && unzip dex2jar-2.0.zip -d $ANDROID_HOME/Decompile && chmod +x $ANDROID_HOME/Decompile/dex2jar-2.0/*.sh && rm -v dex2jar-2.0.zip mkdir -p $ANDROID_DECOMPILE_TOOLS/apktool && pushd $ANDROID_DECOMPILE_TOOLS/apktool && curl -OL https://bitbucket.org/iBotPeaches/apktool/downloads/apktool_2.6.0.jar && ln -s apktool_2.6.0.jar apktool.jar && curl -OL https://raw.githubusercontent.com/iBotPeaches/Apktool/master/scripts/linux/apktool && chmod +x apktool && popd curl -OL https://github.com/skylot/jadx/releases/download/v1.3.3/jadx-1.3.3.zip && unzip jadx-1.3.3.zip -d $ANDROID_DECOMPILE_TOOLS/jadx && rm -v jadx-1.3.3.zip mkdir -p $ANDROID_DECOMPILE_TOOLS/java2smali && curl -OL https://github.com/izgzhen/java2smali/releases/download/1.1/dist.zip && bsdtar xvf dist.zip --strip-components=1 -C $ANDROID_DECOMPILE_TOOLS/java2smali/ && rm -v dist.zip mkdir -p $ANDROID_DECOMPILE_TOOLS/java2smali && pushd $ANDROID_HOME/Decompile/java2smali && curl -OL https://github.com/JesusFreke/smali/releases/download/v2.0b6/smali-2.0b6.jar && curl -OL https://github.com/JesusFreke/smali/releases/download/v2.0b6/baksmali-2.0b6.jar && curl -OL https://raw.githubusercontent.com/JesusFreke/smali/master/scripts/smali && curl -OL https://raw.githubusercontent.com/JesusFreke/smali/master/scripts/baksmali && popd pushd $ANDROID_DECOMPILE_TOOLS/java2smali && ln -s baksmali-2.0b6.jar baksmali.jar && ln -s smali-2.0b6.jar smali.jar && chmod +x baksmali && chmod +x smali && popd # Then append these lines to ~/.profile export ANDROID_HOME="/opt/android-sdk-linux" export ANDROID_SDK_ROOT=$ANDROID_HOME export ANDROID_DECOMPILE_TOOLS=="$ANDROID_HOME/Decompile" export PATH=$ANDROID_HOME/cmdline-tools:$ANDROID_HOME/cmdline-tools/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/build-tools/30.0.3:$ANDROID_DECOMPILE_TOOLS/jadx/bin:$ANDROID_DECOMPILE_TOOLS/java2smali:$ANDROID_DECOMPILE_TOOLS/apktool:$ANDROID_DECOMPILE_TOOLS/dex2jar-2.0:$PATH