블록체인2021. 8. 6. 00:41

지난 번에 다양한 개인키를 생성하여 지갑을 추적하는 이더리움 계좌 해킹 글을 게시한 적이 있다. 지금 이 글은 그때 이더리움을 운영하는 중심 프로그램인 geth client 의 사용에 익숙해진 사용자를 대상으로 하므로 이에 대한 사전 지식이 필요하다는 점은 밝혀둔다.

 

해당 기존 블로그 글은 아래와 같다.

https://infoengineer.tistory.com/56 

 

1조원이 묻힌 암호화폐 계좌를 확보할 수 있을까? 엔지니어를 위한 이더리움/비트코인 계좌 해킹

 가끔씩 비트코인 계좌의 비밀번호(혹은 개인키)를 잃어버렸다는 기사를 볼 수 있다. 그러면 누구나 생각하게 된다. 아무도 못찾게된 저 계좌 내가 찾아볼까?  그래서 시작해보았다. 찾아보자.

infoengineer.tistory.com

 

이와 관련하여 이더리움 작업을 자동화하는 script화가 필요하고, 가장 쉬운 방법은 geth 자체의 명령을 geth attach 라는 형태로 script화 하는 것이다. 편의상 여기서는 linux의 bash shell을 통해 geth attach 기능을 사용해 보자.

 

우선 먼저 가정이 필요한데, script 실행하는 같은 서버에 geth가 최소한 lightmode로 작동하고 있다고 가정했다

(물론 full mode도 작동한다)

 

아래 처럼 nohup으로 geth를 light mode로 띄워놓자. nohup을 사용하면 다행스럽게도 terminal을 종료해도 geth가 계속 살아있다.(geth process를 만약 kill해야 한다면, ps -ef geth 명령으로 확인하여 kill PID 명령을 통해 죽이자)

 

$ cd /work/geth

$ more run_main_light_nohup.sh
nohup ./geth --datadir "/work/gethdata" --allow-insecure-unlock --syncmode "light" >> ./result.log 2> ./result2.log &

$ ./run_main_light_nohup.sh

 

 

A. 내 이더리움 geth 등록 계좌(eth.accounts) 전체의 잔액을 확인해보자.

 

 

아래 소스를 통해 가능하다.  https://github.com/neibc/ethereum/blob/main/check_all_balances.sh

geth attach는 bash의 명령어 입력 기능인 "<< EOF ~ EOF" 를 통해 code를 전송하면 된다.

 

$ cd /work/geth

$ more check_all_ballances.sh

#!/bin/bash

# check the balances of all registered accounts on geth

# 2021.08.05 by neibc

/work/geth/geth --datadir "/work/gethdata" attach << EOF

eth.accounts.forEach(function(e,i){console.log("eth.accounts["+i+"]: " + eth.accounts[i] + "\tbalance:" + web3.fromWei(eth.getBalance(eth.accounts[i]),"ether") + " Ether" )});

EOF

 

$ ./check_all_balances.sh
Welcome to the Geth JavaScript console!

instance: Geth/v1.10.6-stable-576681f2/linux-amd64/go1.16.4
at block: 12965696 (Thu Aug 05 2021 15:12:50 GMT+0000 (UTC))
 datadir: /work/gethdata
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 les:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 vflux:1.0 web3:1.0

To exit, press ctrl-d
>
>
..

eth.accounts[8]: 0xae72a48c1a36bd18af168541c53037965d26e4a8     balance:0 Ether
eth.accounts[9]: 0x8fd379246834eac74b8419ffda202cf8051f7a03     balance:0 Ether
eth.accounts[10]: 0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a    balance:1.4972e-14 Ether
eth.accounts[11]: 0x1563915e194d8cfba1943570603f7606a3115508    balance:9.96e-15 Ether
eth.accounts[12]: 0x5cbdd86a2fa8dc4bddd8a8f69dba48572eec07fb    balance:0 Ether
eth.accounts[13]: 0x7564105e977516c53be337314c7e53838967bdac    balance:0 Ether
eth.accounts[14]: 0xe1fae9b4fab2f5726677ecfa912d96b0b683e6a9    balance:0 Ether
eth.accounts[15]: 0xdb2430b4e9ac14be6554d3942822be74811a1af9    balance:0 Ether
eth.accounts[16]: 0x62f94e9ac9349bccc61bfe66ddade6292702ecb6    balance:0 Ether
eth.accounts[17]: 0x0d8e461687b7d06f86ec348e0c270b0f279855f0    balance:1.2e-14 Ether
..

 

계좌에 있는 잔액이 맨 오른쪽에 표시됨을 알 수 있다.

 

 

B. 이제 ethereum sendTransaction 명령(송금)을 내려보자.

 

 

아래 소스를 통해 가능하다.

https://github.com/neibc/ethereum/blob/main/automatic_transfer_eth.sh

 

...

eth.sendTransaction({from: fromacc, to: toacc, value: transferval, gas: gaslimit, gasPrice: gasprice});

...

 

특정 계정(fromacc)에서 특정 계정(toacc)으로 gas price, gas limit을 정하여 실행한다. 이때 parameter로 사용되는 gas, gas price, gas limit이라는 것을 알아보자.

 

gas는 gas limit을 의미한다. 해당 송금을 요청할때 사용자가 지불할 수 있는 최대 허용 gas를 정의한다.

대개 단순 송금은 21,000이다. 특이한 것은 조금더 올려 잡아도, 이더리움 송금이 최종 완료되고 나서 남으면 돌려준다. gas는 이더리움 송금 외에도 ERC-20 방식 등 별도의 이더리움 기반 코인 등 송금할때도 사용하기 때문에, 복잡한 transaction에는 높은 gas를 부과해서 처리한다.

 

gas price는 사용하는 gas에 곱하여 최종의 송금비용(transaction fee)를 ethereum으로 계산할 수 있다. gas price는 높이 부과해줄수록 더 빠른 송금이 가능하다. 예를들면 일반 이더리움 송금은 21,000 gas를 소모하는데, 대략 gas price는 35~60 gwei(giga wei) 정도 된다. 참고로 wei는 이더리움을 더 작은 단위로 부르는 값인데 아래와 같이 이해하면 된다.

 

10^18 wei = 1 Ether

0.000000000000000001 Ether = 1 wei

1 gwei = 1 giga wei = 1,000,000,000 wei

 

sendTransaction에서 이 gas(gas limit), gasPrice(gas price)를 생략하면 디폴트 값으로 요청되고 다소 해당 값이 작을 수도 있어서 가끔씩 오류가 발생할 수도 있다. 그리고 송금하려는 이더리움과 이 수수료가 모두 잔고에 남아있어야 송금이 성공된다. 잔고가 부족한 경우 잔고 부족 오류가 발생한다.

 

에러 예시>  Error: Insufficient funds for gas * price + value   - 송금지정금액+송금비가 잔액을 초과한 경우

                gas required exceeds allowance - gas limit이 작아서, 더 큰 gas limit 지정이 필요한 경우

 

gas price는 시간이 오래 걸려도 상관없으면 1 gwei를 지정해서 처리하기도 한다. 그러면 지금 이더리움 네트워크의 대략의 gas price는 얼마인지 궁금한가? eth.gasprice 를 사용해도 되고, 사이트에서도 확인할 수 있다. etherscan.io 사이트에서 각 transaction에 사용된 gas price, gas등도 확인이 가능하다

(예시> https://etherscan.io/tx/0xe325a7cc9b088301771c85c04b70597fccffcb50b228eea2173898b2c3a64241 )

 

실제 실행 소스 코드를 살펴보자.

-----------------------------------------------------------------------------------------------

#!/bin/bash

# geth attach script while running geth with light mode
# 2021.08.05 by neibc

/work/geth/geth --datadir "/work/gethdata" attach << EOF

var fromacc = "0xYOURSOURCEADDR";
var toacc = "0xYOURTARGETADDR";

console.log("fromacc: " + fromacc);
console.log("toacc: " + toacc);
var gasprice = new BigNumber(web3.toWei('35', 'gwei'));
console.log("gas price: " + gasprice);
var gaslimit = 21000;
var cost = gasprice.mul(gaslimit);
console.log("cost price: " + cost);
var deposit = eth.getBalance(fromacc);
console.log("deposit: " + deposit);
var transferval = eth.getBalance(fromacc).sub(cost);
console.log("transferval: " + transferval);

console.log("Unlock account");
personal.unlockAccount(fromacc,"YOURPASSWORD");

if(transferval > 0) {
  console.log("transfer result");
  eth.sendTransaction({from: fromacc, to: toacc, value: transferval, gas: gaslimit, gasPrice: gasprice});
} else {
  console.log("no balance");
}

EOF

-----------------------------------------------------------------------------------------------

 

참고로 ERC-20 token의 코인도 송금할 수 있다. 해당 토큰의 계약 주소는 etherscan.io 등에서 확인이 가능하다. 여기서는 미네리움을 예시로 들었다. ( https://etherscan.io/token/0x426CA1eA2406c07d75Db9585F22781c096e3d0E0 )

 

소스는 아래를 참조하자.

https://github.com/neibc/ethereum/blob/main/automatic_transfer_erc20token.sh

 

C. 알려진 여러개의 계좌에서 잔액이 입금될때마다 돈이 송금되게 하려면?

 

 

이전에 지적한대로 개인키가 잘 알려진(예> 개인키 1값) 계좌 등에서는 입금하자마자 출금하는 진풍경이 벌어진다.

https://etherscan.io/address/0x7e5f4552091a69125d5dfcb7b8c2659029395bdf

 

이런 스크립트는 어떻게 작성할 수 있을까? 필자는 crontab과 geth attach script를 결합해보았다.

 

이를 테면 계좌를 반복적으로 조회하면서, 잔액이 일정액 이상 남아있으면 송금하는 작업을 반복할 수 있다. 다만, 구동시 에러(peer 접속 중단 등)가 나면 해당 geth attach가 Exception으로 중단되는데, 다시 작업을 이어주기 위해 crontab에 주기적으로 다시 띄워준다. 다만, 해당 script에는 동일한 geth attach script 명령이 기 동작 중이면 실행되지 않고 멈추도록 앞 부분에 "ps -ef"를 활용한 방어 코드를 넣어주었다.

 

https://github.com/neibc/ethereum/blob/main/repeatedtrasfer_eth_from_knownaddr.sh

 

-----------------------------------------------------------------------------------------------

#!/bin/bash

# crontab registration - run every 10 minutes this script
# 10,20,30,40,50 * * * * cd /work/geth;/bin/bash repeatedtrasfer_eth_from_knownaddr.sh

# 2021.08.05 by neibc
#

# run this script only where there is no duplicated attach job
# because it tries to run sendTransaction continuously if there is no error(disconnection or something)
#

ps -ef | grep "[a]ttach" > /dev/null
if [ $? -eq 0 ]; then
echo "duplicated attach job exists"
else
#/work/geth/geth --datadir "/work/gethdata" attach >> /work/geth/joblog.txt 2> /work/geth/joblog_err.txt << EOF
nohup /work/geth/geth --datadir "/work/gethdata" attach >> /work/geth/joblog.txt 2> /work/geth/joblog_err.txt << EOF

var toacc = "0xTARGETADDRESS_FIXME";
console.log("toacc: " + toacc);
var gasprice = new BigNumber(web3.toWei('130', 'gwei'));
console.log("gas price: " + gasprice);
var gaslimit = 21000;
var cost = gasprice.mul(gaslimit);
console.log("cost price: " + cost);
var deposit = 0;
var transferval = 0;

while(true) {
        eth.accounts.forEach(function(e,i){

               deposit = eth.getBalance(eth.accounts[i]);
               transferval = eth.getBalance(eth.accounts[i]).sub(cost);

               if(transferval > 0) {
                      console.log("num:"+i);
                      console.log("addr:"+eth.accounts[i]);
                      console.log("balance:"+deposit);
                      console.log("transfer val:"+transferval);
                      console.log("Unlock account");
                      personal.unlockAccount(eth.accounts[i],"YOURPASSWORD_FIXME");
                      console.log("transfer result");
                      eth.sendTransaction({from: eth.accounts[i], to: toacc, value: transferval,gas: gaslimit, gasPrice:gasprice});
               }
        });
}
EOF

fi

-----------------------------------------------------------------------------------------------

 

다양한 스크립트의 예시로서 위 샘플들이 참고가 되었으면 좋겠다.

반응형
Posted by 작동미학