Run mitmdump from crontab - solved

Howdy!

I have a lovely python script to parse data and do some stuff, namely post some data to a couchdb server. When I run it (Ubuntu Server 16.04.1) from bash, it runs perfectly, does everything as I expect.

When I set it to run as a root crontab task @reboot, I get yucky errors:

Loading script: /mitm/blabla.py
Proxy server listening at http://0.0.0.0:8080
169.1.59.145:56973: clientconnect
169.1.59.145:56973: CONNECT clients4.google.com:443
 << Cannot establish TLS with clients4.google.com:443 (sni: clients4.google.com): TlsException("SSL handshake error: Error([('', 'osrandom_rand_bytes', 'getrandom() initialization failed.')],)",)
169.1.59.145:56973: clientdisconnect
169.1.59.145:48064: clientconnect
169.1.59.145:48064: CONNECT clients4.google.com:443
 << Cannot establish TLS with clients4.google.com:443 (sni: clients4.google.com): TlsException("SSL handshake error: Error([('', 'osrandom_rand_bytes', 'getrandom() initialization failed.')],)",)
169.1.59.145:48064: clientdisconnect
169.1.59.145:35554: clientconnect
169.1.59.145:35554: CONNECT clients4.google.com:443
 << Cannot establish TLS with clients4.google.com:443 (sni: clients4.google.com): TlsException("SSL handshake error: Error([('', 'osrandom_rand_bytes', 'getrandom() initialization failed.')],)",)
169.1.59.145:35554: clientdisconnect
169.1.59.145:59950: clientconnect
169.1.59.145:59950: Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/proxy/server.py", line 119, in handle
    root_layer()
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/proxy/modes/http_proxy.py", line 9, in __call__
    layer()
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/proxy/protocol/tls.py", line 383, in __call__
    layer()
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/proxy/protocol/http1.py", line 72, in __call__
    layer()
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/proxy/protocol/http.py", line 176, in __call__
    if not self._process_flow(flow):
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/proxy/protocol/http.py", line 245, in _process_flow
    return self.handle_regular_connect(f)
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/proxy/protocol/http.py", line 194, in handle_regular_connect
    layer()
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/proxy/protocol/tls.py", line 378, in __call__
    self._establish_tls_with_client()
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/proxy/protocol/tls.py", line 466, in _establish_tls_with_client
    cert, key, chain_file = self._find_cert()
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/proxy/protocol/tls.py", line 594, in _find_cert
    return self.config.certstore.get_cert(host, list(sans))
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/certs.py", line 349, in get_cert
    sans),
  File "/usr/local/lib/python3.5/dist-packages/mitmproxy/certs.py", line 113, in dummy_cert
    cert.sign(privkey, "sha256")
  File "/usr/local/lib/python3.5/dist-packages/OpenSSL/crypto.py", line 1083, in sign
    _openssl_assert(sign_result > 0)
  File "/usr/local/lib/python3.5/dist-packages/OpenSSL/_util.py", line 61, in openssl_assert
    exception_from_error_queue(error)
  File "/usr/local/lib/python3.5/dist-packages/OpenSSL/_util.py", line 48, in exception_from_error_queue
    raise exception_type(errors)
OpenSSL.crypto.Error: [('', 'osrandom_rand_bytes', 'getrandom() initialization failed.'), ('rsa routines', 'RSA_setup_blinding', 'BN lib'), ('rsa routines', 'RSA_EAY_PRIVATE_ENCRYPT', 'internal error'), ('asn1 encoding routines', 'ASN1_item_sign_ctx', 'EVP lib')]

169.1.59.145:59950: clientdisconnect
169.1.59.145:49076: clientconnect

Can anyone help?

Hi,

This seems to be a duplicate of https://github.com/mitmproxy/mitmproxy/issues/2013?

Aha, now that’s useful information!
So since it works when I run it manually, but not when I add to to an @reboot cron job, I suspect this is the problem: https://bugs.python.org/issue26839

“tracked it down to python3.5 hanging for a long time when it gets
called before the kernel initializes its RNG”

So perhaps all I need is a delay!

I’ll try this and update with what happens.

Well! I’ll be honest, I didn’t expect that to work:
@reboot sleep 60 && /path/to/startup.sh

Thank you for your help, sir!