WARGAME/web

[드림핵] BISC Safe

lucykorea414 2024. 5. 8. 22:04
728x90

 

 


web3 문제를 풀기 전에 일단 MetaMask chrome extension을 다운로드 받고 계정을 생성해야한다!!

 

다 했다면 문제를 한번 풀어보자~~

 

일단 서버를 생성하구 웹사이트에 들어가보쟈!!

웹사이트에 들어가면 일단 무슨 컨트랙트랑 연결됨!!

 

 

그러면 금고를 열어달라는 문제가 있는데, 열기 버튼을 눌러도 안열리고 다음과 같이 문구가 뜬다!!

 

 

일단 문제 파일을 다운로드 받아보자.

 

 

이제 다음과 같은 코드가 있는데, 이걸 보면 owner (즉, 이 컨트렉트의 작성자인것 같음..) 와 msg.sender (즉, 지금 문제를 푸는 유저)가 일치 하는 경우에만 플래그값이 반환되는 것을 확인 할 수 있다!!

 

한번 크롬 개발자도구로 더 자세히 알아보자.

 

이 스크립트 있는 부분을 자세히 보자!!

    const start = async () => {
    if(window.ethereum !== "undefined") {
            const accounts = await ethereum.request({method: "eth_requestAccounts"});
            account = accounts[0];
            console.log(`my contract: ${account}`);
    }
      const ABI = [
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "_owner",
				"type": "address"
			}
		],
		"name": "changeOwner",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [],
		"stateMutability": "nonpayable",
		"type": "constructor"
	},
	{
		"inputs": [],
		"name": "opensafe",
		"outputs": [
			{
				"internalType": "string",
				"name": "",
				"type": "string"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "owner",
		"outputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			}
		],
		"stateMutability": "view",
		"type": "function"
	}
];
      const ADDRESS = "0x5e992854Bd912ae170b7b5b8a64323e4e5E0feAF";
      window.web3 = await new Web3(window.ethereum);
      window.contract = new web3.eth.Contract(ABI, ADDRESS);
      console.log(`safe contract: ${window.contract.options.address}`);
  }
  start();
  async function openSafe() {
    try {
      const result = await window.contract.methods.opensafe().call({from: account});
  
      if (result === "Your not owner!!") {
        document.getElementById('result').innerText = result;
      } else {
        document.getElementById('result').innerText = result;
        var door = document.getElementById('door');
        door.style.transform = 'rotateY(-90deg)';
      }
    } catch (error) {
      console.error(error);
    }
  }

 

마지막 openSafe() 부분이 좀 중요한데, 여기서는 account(즉, 유저 계정)으로 함수를 call 하고 있다!

 

그리고 나의 컨트랙트와 현재 진행되는 컨트랙트의 주소가 콘솔창에 뜬다!!

 

일단 그러면 콘솔창에서 owner의 address를 출력해보자! 

 

이 주소를 사용해서 openSafe() 를 call 해보자!!

 

 

성공~

728x90