Webhacking.kr old-10

Challenge

Move the target link to the goal position.

将目标链接移动到终点位置。

1
https://webhacking.kr/challenge/code-1/

Analysis

hackme 元素的 onclick 处理器每次把 style.left 增加 1px;当结果恰好为 1600px 时,脚本才把 href 设置为 ?go=1600px:

1
2
this.style.left = parseInt(this.style.left, 10) + 1 + "px";
if (this.style.left == "1600px") this.href = "?go=" + this.style.left;

直接访问 ?go=1600px 会触发 no hack,因为服务端还需要这次真实的 click 流程。把元素放到终点前 1px,再触发一次 click,可以同时满足位置和链接生成条件。

Solution

在题目页面的浏览器开发者工具 Console 中运行完整脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(function moveTargetToGoal() {
const target = document.getElementById("hackme");
if (!target) {
throw new Error("#hackme was not found");
}

target.style.left = "1599px";
target.click();

if (target.style.left !== "1600px") {
throw new Error(`unexpected position: ${target.style.left}`);
}
if (target.getAttribute("href") !== "?go=1600px") {
throw new Error(`unexpected href: ${target.getAttribute("href")}`);
}
})();