Giter VIP home page Giter VIP logo

karl's Introduction

Karl

Obsolete, not maintained anymore, don't install it, don't use it, you were warned!

License: MIT CircleCI Codacy Badge PyPI Code style: black Maintainability Rating

A monitor for smart contracts that checks for security vulnerabilities.

Karl Vreski

Video presentation

DefCon 27

Install

Get latest version of Karl.

$ pip install --user karl

Install Ganache with npm if you want Karl to test the found vulnerabilities in a sandbox (--sandbox=true, disabled by default), to reduce false positives.

$ npm i -g ganache-cli

Description

Karl will allow you to monitor a blockchain for vulnerable smart contracts that are being deployed.

It connects to the blockchain, monitors for new blocks and runs mythril for every new smart contract deployed.

The output can be displayed in the console, saved in files in a folder or POSTed to a URL.

Output can be:

  • stdout just posting the results to standard output
  • folder create a file for each vulnerable contract in a folder
  • posturl POST the results to an http endpoint

Help message

$ karl --help
usage: karl [-h] [--rpc https://mainnet.infura.io/v3/12312312312312312312312312312312] [--rpc-tls RPC_TLS] [--block NUMBER] [--output Can be one of: stdout, posturl, folder]
            [--posturl POSTURL] [--folder-output FOLDER_OUTPUT] [--sandbox SANDBOX] [--timeout SECONDS] [--loop-bound LOOP_BOUND] [--tx-count NUMBER]
            [--modules [MODULES [MODULES ...]]] [--onchain-storage ONCHAIN_STORAGE] [--verbose] [--version]

Smart contract monitor using Mythril to find exploits

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit

RPC options:
  --rpc https://mainnet.infura.io/v3/12312312312312312312312312312312
                        Custom RPC settings (default: None)
  --rpc-tls RPC_TLS     RPC connection over TLS (default: False)
  --block NUMBER        Start from this block, otherwise start from latest (default: None)

Output:
  --output Can be one of: stdout, posturl, folder
                        Where to send results (default: stdout)
  --posturl POSTURL     Send results to a RESTful url [when using `--output posturl`] (default: None)
  --folder-output FOLDER_OUTPUT
                        Save files to this folder [when using `--output folder`] (default: None)

Sandbox:
  --sandbox SANDBOX     Test found transactions in a Ganache sandbox (default: False)

Scan options:
  --timeout SECONDS     Scan timeout per contract (default: 600)
  --loop-bound LOOP_BOUND
                        Maximum number of loop iterations (default: 3)
  --tx-count NUMBER     Maximum number of transactions (default: 3)
  --modules [MODULES [MODULES ...]]
                        Modules to use for scanning (default: ['ether_thief', 'suicide'])
  --onchain-storage ONCHAIN_STORAGE
                        Whether onchain access should be done or not (default: True)

Verbosity:
  --verbose, -v         Set verbose (default: 4)

Examples

Running against the mainnet

$ karl --rpc https://mainnet.infura.io/
Stdout initialized
Running
Scraping block 6745471
Scraping block 6745472
Scraping block 6745473
Analyzing 0xf8c065bB1DafC99eE5476a2b675FAC4a036a4B07
Scraping block 6745474
Analyzing 0xC9e044D76f211E84bA651b30BBA86758ca8017c7
Scraping block 6745475
Scraping block 6745476
Scraping block 6745477
Analyzing 0x19427b8FD32dfEc78393517Da416bC5C583E6065

Running against ganache with stdout enabled

$ karl --rpc http://localhost:8545 --output=stdout
INFO:mythril.mythril:Using RPC settings: ('localhost', 8545, False)
INFO:mythril.analysis.modules.suicide:Suicide module: Analyzing suicide instruction
POSSIBLE VULNERABILITY!
Initial balance = 100000000000000000000, final balance = 100999999999999985722

Type = VulnerabilityType.KILL_AND_WITHDRAW
Description = Looks line anyone can kill this contract and steal its balance.
Transactions = [{'from': '0x1dF62f291b2E969fB0849d99D9Ce41e2F137006e', 'to': '0x2F2B2FE9C08d39b1F1C22940a9850e2851F40f99', 'data': '0xcbf0b0c0bebebebebebebebebebebebe1dF62f291b2E969fB0849d99D9Ce41e2F137006e', 'value': 0}]

Running against ganache with posturl enabled

$ karl --rpc [ganache](http://localhost:8545) --output=posturl --posturl=http://localhost:8080
Posturl initialized
Running
Scraping block 5
Analyzing 0x4b8e80acaE3F0db32e5d35925EfaA97D477dBb70

And it will send this to the listening service

$ nc -l 8080
POST / HTTP/1.1
Accept-Encoding: identity
Content-Type: application/x-www-form-urlencoded
Content-Length: 725
Host: localhost:8080
User-Agent: Python-urllib/3.7
Connection: close

{
    "error": null,
    "issues": [{
        "address": 722,
        "contract": "0x4b8e80acaE3F0db32e5d35925EfaA97D477dBb70",
        "debug": "Transaction Sequence: {'1': {'calldata': '0x56885cd8', 'call_value': '0x0', 'caller': '0xaaaaaaaabbbbbbbbbcccccccddddddddeeeeeeee'}, '4': {'calldata': '0x6c343ffe', 'call_value': '0x0', 'caller': '0xaaaaaaaabbbbbbbbbcccccccddddddddeeeeeeee'}}",
        "description": "Arbitrary senders other than the contract creator can withdraw ETH from the contract account without previously having sent an equivalent amount of ETH to it. This is likely to be a vulnerability.",
        "function": "withdrawfunds()",
        "max_gas_used": 1749,
        "min_gas_used": 1138,
        "swc-id": "105",
        "title": "Ether thief",
        "type": "Warning"
    }],
    "success": true
}

Running against the mainnet with folder output enabled

$ karl --rpc karl --rpc https://mainnet.infura.io/ --output folder

Demo

Running locally with a specially crafted vulnerable contract:

asciicast

Running on the main net using Infura:

asciicast

Troubleshooting

OpenSSL

If you get this error

  #include <openssl/aes.h>
          ^~~~~~~~~~~~~~~
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

You must install the openssl source code libraries

Ubuntu

$ sudo apt-get install libssl-dev

Credits

This tool is inspired by Bernhard's initial prototyping and it heavily uses his project Myth.

karl's People

Contributors

cleanunicorn avatar codacy-badger avatar dmuhs avatar pyup-bot avatar stickler-ci avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

karl's Issues

Ganache crashes on contract 0x900AE25f29C4801f546A684dAD2271EcfB292539

When transactions fail Ganache isn't killed

block 7151766
contract 0x900AE25f29C4801f546A684dAD2271EcfB292539

DEBUG:karl.sandbox.sandbox:Feeding eth to contractDEBUG:karl.sandbox.sandbox:Transaction = {'from': '0xACa94ef8bD5ffEE41947b4585a84BdA5a3d3DA6E', 'data': '0x6080604052604051602080604c83398101806040526020811015602157600080fd5b81019080805190602001909291905050508073ffffffffffffffffffffffffffffffffffffffff16fffe000000000000000000000000900ae25f29c4801f546a684dad2271ecfb292539', 'value': 1000000000000000000}
DEBUG:karl.sandbox.sandbox:Fed ether in receipt = AttributeDict({'transactionHash': HexBytes('0x89f8f9a4f6a1e671041f99853d08c26dbe206418c54032c7cc1bf0f1d7138813'), 'transactionIndex': 0, 'blockHash': HexBytes('0xa6211aacce7d8d04a32559d0e6343403f1d2d72477250017767963d2a074b362'), 'blockNumber': 7152457, 'from': '0xaca94ef8bd5ffee41947b4585a84bda5a3d3da6e', 'to': None, 'gasUsed': 40655, 'cumulativeGasUsed': 40655, 'contractAddress': '0x2F2B2FE9C08d39b1F1C22940a9850e2851F40f99', 'logs': [], 'status': 1, 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'v': '0x1c', 'r': '0xb4f144c40023fa799ba3a68061d4648d406a516c4609a9d6a110d25f58cef215', 's': '0x5b8e2710886ce2990d04279157f1dea5d26b2f89f2aeda8f9b9a3e4aae3aa786'})DEBUG:karl.sandbox.sandbox:Sending transactionDEBUG:karl.sandbox.sandbox:Transaction = {'from': '0x1dF62f291b2E969fB0849d99D9Ce41e2F137006e', 'to': '0x900AE25f29C4801f546A684dAD2271EcfB292539', 'data': '0x3f579f420800000100002000000000201dF62f291b2E969fB0849d99D9Ce41e2F137006e0108020420042004010208100410400880021008020401100100100240080404', 'value': 0}
ERROR:Karl:Exception: {'message': 'VM Exception while processing transaction: out of gas', 'code': -32000, 'data': {'stack': 'o: VM Exception while processing transaction: out of gas\n    at Function.o.fromResults (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:82120)\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:126489\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:2220779\nat /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26246\n    at i (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:41301)\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:1582060\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:105207\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:32:392\n    at c (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:32:5407)\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:32:317\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:2239241\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:23359\n    at o (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26768)\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26246\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:2232242\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:2230105\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:2257354\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:23359\n    at o (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26768)\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:26246\n    at /usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:5439\n    at FSReqWrap.oncomplete (fs.js:135:15)', 'name': 'o'}}
<traceback object at 0x7fb3ee8e2b88>                   

ERROR:Karl:Exception:

INFO:Karl:Processing block 12001909
INFO:Karl:Processing block 12001910
INFO:Karl:Processing block 12001911
ERROR:Karl:Exception: init() got an unexpected keyword argument 'use_onchain_data'
<traceback object at 0x7f78f6103cc8>

Limit Requests to Avoid 429 Exception

At times, the requests that karl makes to infura cause the following error:

ERROR:Karl:Exception: 429 Client Error: Too Many Requests for url: https://mainnet.infura.io/
<traceback object at 0x7f0edc560688> 

If karl determined the minimum time it could send requests to infura without triggering the 429 error, it could work within that bound and still make progress while not halting the execution of the program entirely due to a 429 error.

Initial Update

The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.

BSC

How can i get it to work on BSC ?

ERROR:Karl:Exception: unable to open database file

Hello,

when running karl I'm getting this error:

karl --rpc infura-mainnet --rpc_tls true
INFO:Karl:Starting scraping process
INFO:Karl:Processing block 8091013
INFO:Karl:Processing block 8091014
INFO:Karl:Processing block 8091015
ERROR:Karl:Exception: unable to open database file
<traceback object at 0x1128f0c48>
INFO:Karl:Processing block 8091016
INFO:Karl:Processing block 8091017
INFO:Karl:Processing block 8091018
ERROR:Karl:Exception: unable to open database file
<traceback object at 0x1128f0f88>
INFO:Karl:Processing block 8091019
INFO:Karl:Processing block 8091020
INFO:Karl:Processing block 8091021
INFO:Karl:Processing block 8091022
INFO:Karl:Processing block 8091023
INFO:Karl:Processing block 8091024
INFO:Karl:Processing block 8091025
INFO:Karl:Processing block 8091026
INFO:Karl:Processing block 8091027
INFO:Karl:Processing block 8091028
ERROR:Karl:Exception: unable to open database file

Did I miss a setup step? Or is this a bug?

Thank you,
:panos

ERROR:Karl:Exception: 'NoneType' object is not subscriptable

Upon testing in Ganache, certain findings trigger the error **ERROR:Karl:Exception: 'NoneType' object is not subscriptable. An example smart contract on the mainenet that causes this is 0x1260ef3c37Cc4Bedf21D9f0AF4107F67F8E8297D.

Command ran:

./karl --rpc [Infura node] --sandbox=true --output folder --folder-output folder &

Console output:

INFO:Karl:Analyzing 0x1260ef3c37Cc4Bedf21D9f0AF4107F67F8E8297D 
[snip of code blocks]
INFO:Karl:Found 2 issue(s) 
INFO:Karl:Firing up sandbox tester 
**ERROR:Karl:Exception: 'NoneType' object is not subscriptable
<traceback object at 0x7f6226748880>** 

Findings file:

==== Integer Underflow ====
SWC ID: 101
Severity: High
Contract: 0x1260ef3c37Cc4Bedf21D9f0AF4107F67F8E8297D
Function name: setApprovalForAll(address,bool)
PC address: 5845
Estimated Gas Usage: 8138 - 29150
The binary subtraction can underflow.
The operands of the subtraction operation are not sufficiently constrained. The subtraction could therefore result in an integer underflow. Prevent the underflow by checking inputs or ensure sure that the underflow is caught by an assertion.

Initial State:

Account: [ATTACKER], balance: 0x0, nonce:0, storage:{}
Account: [SOMEGUY], balance: 0x0, nonce:0, storage:{}

Transaction Sequence:

Caller: [CREATOR], function: setApprovalForAll(address,bool), txdata: 0xa22cb465000000000000000000000000b00100000000000700000000000000ff500204010000000000000000000000000000000000000000000000000000000000000000, value: 0x0

==== Integer Underflow ====
SWC ID: 101
Severity: High
Contract: 0x1260ef3c37Cc4Bedf21D9f0AF4107F67F8E8297D
Function name: link_classic_internal(uint64,int64)
PC address: 8646
Estimated Gas Usage: 3453 - 4678
The binary subtraction can underflow.
The operands of the subtraction operation are not sufficiently constrained. The subtraction could therefore result in an integer underflow. Prevent the underflow by checking inputs or ensure sure that the underflow is caught by an assertion.

Initial State:

Account: [ATTACKER], balance: 0x0, nonce:0, storage:{}
Account: [SOMEGUY], balance: 0x0, nonce:0, storage:{}

Transaction Sequence:

Caller: [CREATOR], function: link_classic_internal(uint64,int64), txdata: 0x95d89b41, value: 0x0

Add --once to scan only one block

Sometimes one block should be scanned and that should be the end of the scan.

Add a --once flag that only scans the specified/most recent block.

Warning About Interger Underflow In Solidity >=8

Well, I was not able to find resources for knowing more about this, so I guess this is the best place.

I've encountered two high severity warnings on a specific contract I am developing for production. The warning is about integer underflow, even though it seems this issue is gone on versions of Solidity that are higher than and including 0.8.0.

So, I do not know if this is an issue with the code itself or if it is some issue related to solidity versioning on KARL.

Any guidance on that would be really helpful. Also, I have not found documentation for interpreting the errors - and I love that they are shown in the terminal, but some of them seem very abstract. For example, in one of the images below, there is a warning about an integer underflow in a string public name variable that receives a value in the constructor of the contract and does nothing else rather than being a view function so that NFT marketplaces can call on it to know the name of the collection. The other function with an issue is the setApprovalForAll - with no changes from the ERC1155 standard.

image

image

Thank you guys for this amazing masterpiece and wait on your response.

unable to run karl

Hello i tried to install karl using

pip install --user karl

The installation worked well but when i try $ karl --help it didnt work and i get this error
Command 'karl' not found, did you mean:

I'm on the latest kali linux version so debian x64...

How can i solve this ?

Add support for testnetworks (rinkeby/kovan)

I tried testing karl on my testing contract on rinkeby, it returned "extraData" too long.
I have a fix for that as seen on ethereum-etl/issue-178

from web3.middleware import geth_poa_middleware
self.web3.middleware_stack.inject(geth_poa_middleware, layer=0)

And it works like a charm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.