Mccranky's Blog

Turn On, Boot Up, Jack In

Lately I’ve been thinking that since Java has an equalsIgnoreCase method, JavaScript must have something similar. This is what I found: the localeCompare method.

Structure

1
str1.localeCompare(str2, [locales], [options])

Note: The Linux commands also has a locale command!

Available locales

language

'en': English
'fr': French
'de': German
'es': Spanish
'ja': Japanese
'zh': Chinese
'ar': Arabic
'ru': Russian

We can also use a combination of language and region subtags to specify a more specific locale. For example:

'en-US': English (United States)
'fr-CA': French (Canada)
'de-DE': German (Germany)
'es-MX': Spanish (Mexico)
'ja-JP': Japanese (Japan)
'zh-CN': Chinese (China)
'ar-EG': Arabic (Egypt)
'ru-RU': Russian (Russia)

Available options

numeric

1
2
3
4
5
console.log("60".localeCompare("40", undefined, { numeric: true })) // 1

console.log("60".localeCompare("60", undefined, { numeric: true })) // 0

console.log("60".localeCompare("100", undefined, { numeric: true })) // -1

numeric can take one of the two values: true and false

1
console.log("60".localeCompare("100", undefined, { numeric: false })) // 1

With false, both 60 and 100 are treated as normal String objects.

sensitivity

sensitivity can take one of the three values: base, accent, case

1
2
3
4
5
console.log("café".localeCompare("Cafe", undefined, { sensitivity: "base" })) // 0

console.log("café".localeCompare("Cafe", undefined, { sensitivity: "accent" })) // 1

console.log("café".localeCompare("Cafe", undefined, { sensitivity: "case" })) // -1

caseFirst

The caseFirst option in the localeCompare() method specifies whether uppercase letters should come before or after lowercase letters in the sort order. It can take one of the following two values:

upper: Sort uppercase letters before lowercase letters.

lower: Sort lowercase letters before uppercase letters.

1
2
3
4
5
console.log("Ba".localeCompare("AB", undefined, { sensitivity: "case" })) // 1

console.log("aB".localeCompare("AB", undefined, { sensitivity: "case" })) // -1

console.log("aB".localeCompare("AB", undefined, { sensitivity: "case", caseFirst: "upper" })) // 1

ignorePunctuation

The ignorePunctuation option in the localeCompare() method specifies whether punctuation and other symbols should be ignored during the comparison.

It can take one of the following two values: true and false

1
console.log("Hello, world!".localeCompare("Hello world", undefined, { ignorePunctuation: true })) // 0

请先阅读 使用 Hexo 生成静态网页博客

准备工作

首先,我们基于完成的博客.md文件生成静态网页:

1
2
3
4
5
cd /path/to/site/
# 跑到项目路径目录

hexo generate
# 生成静态网页,存放与 public 文件夹内

接着,我们进入public文件夹。

.
├── node_modules
├── public
├── scaffolds
├── source
└── themes

因为我们是使用 GitHub Pages 托管,所以我们需要在public文件夹内初始化git

1
2
3
4
5
6
7
git init

git branch -m main

git add .

git commit -m "初始化工作"

其次,我们还需要在 GitHub 上面建立一个远程仓库,格式必须username.github.io,这是硬性要求。

关联和发布

我们现在需要关联本地仓库和远程仓库,用例中我们使用https协议,当然也可以使用ssh密钥:

1
2
3
git remote add origin https://github.com/username/username.github.io.git

git push origin main

我们还需要更改一下远程仓库的 GitHub Pages 的设置,目的是告知它托管文件的地址:

点击 Settings

点击 Settings

点击 Pages

点击 Pages

修改设置

修改设置

最后,我们访问username.github.io就可以来到我们的博客页了 :)

要求

  • Node.js

  • Git

下载

请注意参考个人电脑的操作系统!

进入 Terminal 查看安装的版本:

1
2
3
node --version # v18.16.0

git --version # git version 2.40.0

Quick Start

1
2
3
4
5
6
7
8
9
hexo init my-blog

cd my-blog

# 因为 hexo 是基于 Node.js 完成的框架,所以完成第一步之后无需再执行 `npm init -y` 生成 package.json 文件

npm i

hexo server

接着我们去 localhost:4000 就能看到我们的博客了!

项目树状结构图

我们首先来了解一下我们的项目文件夹下面有些什么:

1
2
3
4
5
6
7
8
9
10
11
12
.
├── _config.landscape.yml
├── _config.yml
├── db.json
├── node_modules
├── package-lock.json
├── package.json
├── public
├── scaffolds
├── source
├── themes
└── yarn.lock

_config.yml:Hexo 博客设置

node_modules:存放启动需要使用的模组

public:存放生成的静态网页

1
2
3
4
5
6
7
8
9
10
11
12
13
public
├── 2023
├── README.md
├── archives
├── assets
├── css
├── fontawesome
├── fonts
├── images
├── index.html
├── js
├── tags
└── webfonts

scaffolds:存放 Hexo 个人博客的 3 种模版:page (白页),post (博客),draft (草稿)

1
2
3
4
scaffolds
├── draft.md
├── page.md
└── post.md

source:存放博客.md文件

themes:存放Custom主题,从npm下载的主题存放在node_modules

第一篇博客

我们先从草稿开始:

1
hexo new draft first-draft

Hexo 会帮我们在/source/_drafts/文件夹里创建我们的第一篇草稿。

如果觉得写得差不多了,我们可以:

1
hexo publish first-draft

Hexo 会自动帮我们把first-draft.md移动到/source/_posts/文件夹里,这样当我们打开服务器的时候就能看到这篇博客了。

当然我们也可以带参数运行hexo server

1
hexo server --draft

这样我们在写草稿时也能看到最终的布局,方便我们对内容排版进行修改。

修改_config.yml文件

我们可以通过修改设置文件来改变 Hexo 的一些初始行为,比如说在 Writing 一栏里改变default_layout变量的参数可以改变hexo new创建的文件类型。

1
2
# Writing
default_layout: post

这样我们就不用每次hexo new时还需要特地指定创建的文件类型了。

Prerequisites

  • Install tree command via Homebrew by running brew install tree

  • run brew to check if Homebrew is currently installed on your computer

  • if not, run the following to install Homebrew:

1
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Basic Syntax

1
tree [directory-path]

[directory-path] is an optional argument that specifies the directory you want to display as a tree structure. If you don’t specify a directory path, tree will display the tree structure for the current directory.

Here are some useful options you can add to the tree command:

-L [level]: Limit the depth of the tree to [level] directories.

-I [pattern]: Exclude files and directories that match the [pattern].

-P [pattern]: [pattern] is used to match files or directories that you want to include in the output.

-a: Show hidden files and directories.

-f: Print the full path prefix for each file or directory.

-h: Print the size of each file or directory in a human-readable format.

Example

1
2
3
4
5
6
$ tree -L 1 -P "source/*" -I "_drafts|_pages" source

source
└── _posts

2 directories, 0 files

You can omit the report on the last line with option --noreport if it bothers you.

要求

  • GreenMail Standalone

  • 本地 Apache 服务器

下载

  1. GreenMail 官网直接下载

  2. 使用命令行指令下载:

1
2
3
cd /home/user/

wget https://repo1.maven.org/maven2/com/icegreen/greenmail-standalone/2.1.0-alpha-1/greenmail-standalone-2.1.0-alpha-1.jar

下载完成以后从终端运行.jar文件:

1
java -Dgreenmail.smtp.timeout=120000 -Dgreenmail.smtp.hostname=0.0.0.0 -Dgreenmail.smtp.port=3025 -Dgreenmail.imap.hostname=0.0.0.0 -Dgreenmail.imap.port=3143 -jar /path/to/file/greenmail-standalone-2.1.0-alpha-1.jar

-Dgreenmail.smtp.timeout:为防止 SMTP 服务器超时,把超时时常设置为 120000 毫秒

-Dgreenmail.smtp.hostname, Dgreenmail.smtp.port:设置 SMTP 地址和端口

-Dgreenmail.imap.hostname, -Dgreenmail.imap.port:设置 IMAP 地址和端口

连接 SMTP 服务器

开启 Telnet 进程,连接到 SMTP 服务器的 3025 端口:

1
2
3
telent localhost 3025

# 因为是模拟握手,所以服务器地址就是本地域名
$ telnet localhost 3025
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 /0:0:0:0:0:0:0:1 GreenMail SMTP Service v2.1.0-alpha-1 ready
421 Service shutting down and closing transmission channel (socket timeout, SO_TIMEOUT: 30000ms)

握手成功了!接下里可以输入四字指令和 SMTP 服务器交流:

C: HELO localhost
S: 250-/0:0:0:0:0:0:0:1
   250 AUTH PLAIN LOGIN
C: MAIL FROM: <example@email.com>
S: 250 OK
C: RCPT TO: <example@email.com>
S: 250 OK
C: DATA
S: 354 Start mail input; end with <CRLF>.<CRLF>
C: HELLO THERE
   THIS IS USER SPEAKING
   .
S: 250 OK
C: QUIT
S: 221 /0:0:0:0:0:0:0:1 Service closing transmission channel
   Connection closed by foreign host.

解释一下这些缩写是什么意思:

HELO: hello

RCPT: recipient

CRLF: carriage return & line feed


连接 IMAP 服务器

认证阶段

  2023-10-09 14:59:25,387 INFO
  user.UserManager| Created user login example@email.com for address example@email.com with password
  example@email.com because it didn't exist before.

  $ telnet localhost 3110
  +OK POP3 server ready
  user example@email.com
  +OK
  pass example@email.com
  +OK user successfully logged on

交易阶段

  C: list
  S: 1 498
  S: 2 912
  S: .
  C: retr 1
  S: (blah blah ...
  S: .................
  S: ..........blah)
  S: .
  C: dele 1
  C: retr 2
  S: (blah blah ...
  S: .................
  S: ..........blah)
  S: .
  C: dele 2
  C: quit
  S: +OK POP3 server signing off

用户在这个阶段可以给邮件添加标记,被添加删除标记的邮件将在最后的更新阶段被删除

简要概述 CTF OverTheWire Bandit Level 25

题目

bandit25登录到bandit26,用户bandit26shell不是/bin/bash,而是其他东西。找出它是什么,它是如何工作的,以及如何打破它。


先利用ssh连接到bandit.labs.overthewire.org的服务器,端口号2220,为此我们需要bandit25的密码。

1
2
ssh bandit25@bandit.labs.overthewire.org -p 2220
# p7TaowMYrmu23Ol8hiZh9UvD0O9hpx8d

bandit25进入 Level 25 之后先列举/home/bandit25有什么可以帮助通关的东西:

1
find . -maxdepth 1 -exec file {} 2>/dev/null \;
    .: directory
    ./.bash_logout: ASCII text
    ./.bashrc: ASCII text
    ./bandit26.sshkey: PEM RSA private key
    ./.profile: ASCII text
    ./.pin: ASCII text, with no line terminators
    ./.bandit24.password: ASCII text

find: 查找指令

.

-maxdepth 1: 类似于lodash.js中的flattenDepth的功能,用于拓展平铺树状结构,1代表一层

-exec <...> \;: 代表执行,和pskillidstat属于一类指令,多用于管理系统进程,这里我图方便用来替代for用一下。注意一定要在运行完<...>;结束进程,;前面用\取消原本的意义

file: 查询文件类型,{}placeholder,和 HTML 里的那个placeholder一个意思

2>/dev/null: 2是报错信息的status code/dev/null是系统的bit dump,因此这句的完整的意思就是不让报错信息输出到stdout

很明显bandit26.sshkey有用,是bandit26的 SSH 密钥。

我们就用它来连接bandit26:

1
ssh -i bandit26.sshkey -p 2220 bandit26@localhost

-i代表identifier,就是身份验证

接着,在handshake阶段选’yes’

然后,我们就被踢出来了。。。

  _                     _ _ _   ___   __  
 | |                   | (_) | |__ \ / /  
 | |__   __ _ _ __   __| |_| |_   ) / /_  
 | '_ \ / _` | '_ \ / _` | | __| / / '_ \ 
 | |_) | (_| | | | | (_| | | |_ / /| (_) |
 |_.__/ \__,_|_| |_|\__,_|_|\__|____\___/ 
Connection to localhost closed.

不急,去看看bandit26的用户信息。

在类Unix系统中(包括GNU - GNU’s Not Unix,也就是Linux),用户信息在系统文件/etc/passwd里 - 这是常识,要记住!

1
2
3
cat /etc/passwd | grep "bandit26"

# bandit26:x:11026:11026:bandit level 26:/home/bandit26:/usr/bin/showtext

先不用管别的,我们要找的是最后一列,也就是代表每个用户的初始shell

一开始有说过,bandit26用的shell不是bash,是别的东西。众所周知,因为shell负责翻译所有的指令成机码并输出给kernel执行,所以shell是开启终端进程时运行的第一个程序。因此,我们登陆不上bandit26肯定和这个用户修改了他的default shell有关。

cat /usr/bin/showtext看一下他的初始shell是什么。

1
2
3
4
5
6
7
#!/bin/sh

export TERM=linux

exec more ~/text.txt
exit 0

好家伙,改了个参数,打印了个图标就退出了,怪不得登录不上。

不过因为bandit26用的是more指令,而more指令如果在一个窗口里打印不出文本就会卡壳,我们可以钻这个漏洞,就是把窗口缩到特别小,然后再尝试登陆。

完美,它卡了。。。

1
2
3
4
5
6
#
# _ _ _ _ ___
# __
# | | | (_) | |__ \
# / /
# --More--(33%)

我们可以在more的窗口里用!加任何指令开启一个subshell,也可以直接v打开vim编辑器。

我们选择后者。

进入vim之后按老规矩先ESC进入普通模式,然后按:键,跟在后面输入指令就可以改shell

1
2
3
4
5
6
7
set shell=/bin/bash

# 改成bash

shell

# 刷新并重新登陆

我们现在应该是以bandit26的身份登陆了。

然后需要做的就是去取下一关的密码了。

1
2
cat /etc/bandit_pass/bandit26
# c7GvcKlw9mC7aUQaPx7nwFstuAIBw1o1

简要概述 CTF OverTheWire Bandit Level 24

题目

在端口 30002 上有一个守护进程正在监听,如果提供了 bandit24 的密码和一个秘密的 4 位数的数字密码,它将为您提供 bandit25 的密码。除了通过穷举全部 10000 种组合的方式以外,没有其他获得 pincode 的方法,这被称为暴力破解。

大致思路如下:

  • 先写一个script,按要求进行排版输出到stdout
  • 把输出的 10000 种组合pipenetcat监视的localhost:30002进行测试
  • 直接输出到stdout可能会timeout打断进程,所以我们输出到output.txt
  • 最后cat文本内容拿到密码

先利用ssh连接到bandit.labs.overthewire.org的服务器,端口号2220,为此我们需要bandit24的密码。

1
2
ssh bandit24@bandit.labs.overthewire.org -p 2220
# VAfGXJ1PBSsPSnvsjI8p759leLZ9GGar

bandit24进入 Level 24 之后先到系统文件夹/tmp下建立本关的临时文件夹。

1
2
3
4
5
6
7
cd /tmp; mktemp -d

# /tmp/tmp.u4oQlmpmWL

cd /tmp/tmp.u4oQlmpmWL

# bandit24@bandit:/tmp/tmp.u4oQlmpmWL$

-d代表directory,即告诉mktemp指令创建的临时文件属性为文件夹

1
2
3
4
5
touch script.sh

chmod 777 script.sh

vim script.sh

touch: 创建文件

chmod - change mode: 改变文档rwx权限

  • r - read: 读取权限status code为 4
  • w - write: 书写权限status code为 2
  • x - execute: 执行权限status code为 1

进入文本编辑器之后,选定”插入“模式,按i

首行书写shebang行,选定使用哪个shell来执行该script - 我们选用bash

然后写我们的entity body的代码:

1
2
3
4
5
6
7
8
9
#!/bin/bash

for i in {0000..9999}; do
echo $i
printf "%s %s\n" "VAfGXJ1PBSsPSnvsjI8p759leLZ9GGar" "$i"
done

exit 0

除了语法上微小的一点差异,该题的syntaxC的语法很相像,所以不赘述了。

大致格式就是for [ test statement]; do <command>; done

编辑结束按ESC键回到普通模式,按:,然后输入指令wq,回车保存并退出。

执行script.sh,并把结果pipenetcat:

1
./script.sh | nc localhost 30002 > output.txt

然后cat output.txt读取文本内容,这是其中的一小部分:

        Fail! You did not supply enough data. Try again.
        Wrong! Please enter the correct pincode. Try again.
        Fail! You did not supply enough data. Try again.
        Wrong! Please enter the correct pincode. Try again.
        Fail! You did not supply enough data. Try again.
        Wrong! Please enter the correct pincode. Try again.
        Fail! You did not supply enough data. Try again.
        Wrong! Please enter the correct pincode. Try again.
        Fail! You did not supply enough data. Try again.
        Wrong! Please enter the correct pincode. Try again.
        Fail! You did not supply enough data. Try again.
        Wrong! Please enter the correct pincode. Try again.
        Fail! You did not supply enough data. Try again.
        Wrong! Please enter the correct pincode. Try again.
        Fail! You did not supply enough data. Try again.
        Wrong! Please enter the correct pincode. Try again.
        Fail! You did not supply enough data. Try again.
        Correct!
        The password of user bandit25 is p7TaowMYrmu23Ol8hiZh9UvD0O9hpx8d

        Exiting.

使用 Node.js 的 Express.js 模组 Host 自己的静态网页

首先创建项目文件夹:

1
2
3

mkdir project01; cd project01

然后创建app.js文件:

1
2
3

touch app.js; sudo nano app.js

差点忘了,没下载Node.js的先去这个地址下载一下:https://nodejs.org/en/download/

下载完初始化一下:

1
2
3

node init -y

到生成的package.json文件里 script object 里加个参数:

1
2
3

‘start':‘node app’

好的,非常完美。

然后下载express:

1
2
3

npm i express --save

这里不建议把它下载为–global,因为会和别的模组冲突。

面对空白页,先导入express模组并初始化app(关于http模组的基础不做赘述,因为用那个太麻烦了):

1
2
3

const app = require('express')()

然后省略几千字,直接上代码,不懂看注释 : )

1
2
3
4
5
6
7
8
9
10
11

app.use(express.static('./public')) // 把静态网页需要的资源全扔 public 文件夹里就好了

app.all('*', (req, res) => {
res.status(404).send('resource not found')
}) // 这个是用来处理404 Bad Request 的

app.listen(5000, () => {
console.log('server is listening on port 5000....')
}) // 这个是选择端口

最后在 console 里输入:

1
2
3

npm start

完成!唯一的问题是我们用的还是 localhost, 也就是只有自己可以在端口 5000 看到自己的网页。

想让别人看到的话就去使用 Git Pages 的免费服务。

是不是很简单,开自己动手试一试吧!

使用Git VCS的一些小技巧

初始化步骤我就大段省略了,大致就是去 Git 官网下载 Binary Dependencies 然后安装就好了,或者是去安装xcode-command-line-tools,其中git自动包含在里面了。

说白了 Git 就是一个 Distributional VCS ,大体就是结合了 Local VCS和Centralized VCS,因此基本步骤就是本地建立一个 Local Repository,再在 Gitea 上建立一个 Remote Repository,然后把两个库连接一下,最后把本地的改动push到 Remote Repository 就可以了。

先说如何建 Local Repository。

选一处“风水宝地”,一般选择/User/home。然后跑到系统默认的 Terminal Emulator 去( Windows:PowerShell ),输入一下指令:

1
2
3
4
5
6
mkdir temp # 建立temp

cd temp # 进入

git init # 初始化 Local Repository

做完最后一步会看到 Git 生成了一个.git的隐藏文件夹。千万不要把自己的项目文件扔进去!

初始化完成后,你就可以开始做项目了。等你做完第一个版本,你就可以把项目addstaging area。用如下指令:

1
git add file.. # add 后可以加入多个文件

然后用git status查看 Local Repository 的状态。如果你看到所有想要staged的文件已经在staging area里面了,就可以上传了。

eg:

1
git commit -m “message"

最后,我们需要创建一个 Remote Repository,过程如下:

1
git remote add origin git@example.git

然后,我们push Local Repo 到 Remote Repository,过程如下:

1
git push origin temp
0%