Codebase list python-faraday / debian/2.0.0-0kali3 faraday.py
debian/2.0.0-0kali3

Tree @debian/2.0.0-0kali3 (Download .tar.gz)

faraday.py @debian/2.0.0-0kali3raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#!/usr/bin/env python2.7
'''
Faraday Penetration Test IDE
Copyright (C) 2014  Infobyte LLC (http://www.infobytesec.com/)
See the file 'doc/LICENSE' for the license information

'''

# TODO:
# - Handle requirements dinamically?
# - Additionally parse arguments from file.


import os
import sys
import shutil
import argparse
import platform
import subprocess
import json

from utils.logs import getLogger, setUpLogger
from config.configuration import getInstanceConfiguration
from config.globals import *
from utils.profilehooks import profile
from utils.user_input import query_yes_no


USER_HOME = os.path.expanduser(CONST_USER_HOME)
FARADAY_BASE = os.path.dirname(os.path.realpath(__file__))

FARADAY_USER_HOME = os.path.expanduser(CONST_FARADAY_HOME_PATH)

FARADAY_PLUGINS_PATH = os.path.join(FARADAY_USER_HOME,
                        CONST_FARADAY_PLUGINS_PATH)

FARADAY_PLUGINS_BASEPATH = os.path.join(FARADAY_BASE,
                            CONST_FARADAY_PLUGINS_REPO_PATH)

FARADAY_BASE_IMAGES = os.path.join(FARADAY_BASE, "data",
                            CONST_FARADAY_IMAGES)

FARADAY_USER_CONFIG_XML = os.path.join(FARADAY_USER_HOME,
                            CONST_FARADAY_USER_CFG)

FARADAY_BASE_CONFIG_XML = os.path.join(FARADAY_BASE,
                            CONST_FARADAY_BASE_CFG)

USER_ZSHRC = os.path.expanduser(CONST_USER_ZSHRC)

FARADAY_USER_IMAGES = os.path.join(FARADAY_USER_HOME,
                            CONST_FARADAY_IMAGES)

FARADAY_USER_ZSHRC = os.path.join(FARADAY_USER_HOME, CONST_FARADAY_ZSHRC)
FARADAY_USER_ZSH_PATH = os.path.join(FARADAY_USER_HOME, CONST_ZSH_PATH)
FARADAY_BASE_ZSH = os.path.join(FARADAY_BASE, CONST_FARADAY_ZSH_FARADAY)

FARADAY_VERSION_FILE = os.path.join(FARADAY_BASE, CONST_VERSION_FILE)
FARADAY_CONFIG = os.path.join(FARADAY_BASE, CONST_CONFIG)
FARADAY_REQUIREMENTS_FILE = os.path.join(FARADAY_BASE, CONST_REQUIREMENTS_FILE)

REQUESTS_CA_BUNDLE_VAR = "REQUESTS_CA_BUNDLE"
FARADAY_DEFAULT_PORT_XMLRPC = 9876
FARADAY_DEFAULT_PORT_REST = 9977
FARADAY_DEFAULT_HOST = "localhost"


def getParserArgs():
    """Parser setup for faraday launcher arguments.

    """

    parser = argparse.ArgumentParser(
        description="Faraday's launcher parser.",
        fromfile_prefix_chars='@')

    parser_connection = parser.add_argument_group('connection')
    parser_profile = parser.add_argument_group('profiling')

    parser_connection.add_argument('-n', '--hostname', action="store",
        dest="host",
        default=None,
        help="The hostname where both server APIs will listen (XMLRPC and RESTful). \
        Default = localhost")

    parser_connection.add_argument('-px', '--port-xmlrpc', action="store", dest="port_xmlrpc", default=None, type=int,
        help="Sets the port where the api XMLRPCServer will listen. Default = 9876")
    parser_connection.add_argument('-pr', '--port-rest', action="store", dest="port_rest",
        default=None, type=int,
        help="Sets the port where the api RESTful server will listen. Default = 9977")

    parser.add_argument('-d', '--debug', action="store_true", dest="debug",
        default=False,
        help="Enables debug mode. Default = disabled")

    parser_profile.add_argument('--profile', action="store_true",
        dest="profile",
        default=False,
        help="Enables application profiling. When this option is used \
         --profile-output and --profile-depth can also be used. \
         Default = disabled")

    parser_profile.add_argument('--profile-output', action="store",
        dest="profile_output",
        default=None,
        help="Sets the profile output filename. If no value is provided, \
        standard output will be used")

    parser_profile.add_argument('--profile-depth', action="store",
        dest="profile_depth", type=int,
        default=500,
        help="Sets the profile number of entries (depth). Default = 500")

    parser.add_argument('--disable-excepthook', action="store_true",
        dest="disable_excepthook",
        default=False,
        help="Disable the application exception hook that allows to send error \
        reports to developers.")

    parser.add_argument('--dev-mode', action="store_true", dest="dev_mode",
        default=False,
        help="Enable dev mode. This will use the user config and plugin folder.")

    parser.add_argument('--ignore-deps', action="store_true",
        dest="ignore_deps",
        default=False,
        help="Ignore python dependencies resolution.")

    parser.add_argument('--update', action="store_true", dest="update",
        default=False,
        help="Update Faraday IDE.")

    parser.add_argument('--cert', action="store", dest="cert_path",
        default=None,
        help="Path to the valid CouchDB certificate")

    parser.add_argument('--gui', action="store", dest="gui",
        default="gtk",
        help="Select interface to start faraday. Supported values are "
              "gtk and 'no' (no GUI at all). Defaults to GTK")

    parser.add_argument('--cli', action="store_true",
        dest="cli",
        default=False,
        help="Set this flag to avoid gui and use faraday as a cli.")

    parser.add_argument(
        '-w', '--workspace', action="store",
        dest="workspace",
        default=None,
        help="Workspace to be opened")

    parser.add_argument(
        '-r', '--report', action="store",
        dest="filename",
        default=None,
        help="Report to be parsed by the cli")

    #args = parser.parse_args(['@parser_args.cfg'])
    return parser.parse_args()


def query_user_bool(question, default=True):
    """Returns a boolean based on user input.

    "question" is a string that is presented to the user.
    "default" is the presumed answer if the user just hits <Enter>.
        It must be True (the default), False or None (meaning
        an answer is required of the user).

    The "answer" return value is one of True or False.

    """

    valid_yes_ans = ["yes", "y"]
    valid_no_ans = ["no", "n"]

    if default is None:
        prompt = " [y/n] "
    elif default:
        prompt = " [Y/n] "
    else:
        prompt = " [y/N] "

    while True:
        sys.stdout.write(question + prompt)
        choice = raw_input().lower()

        if default is not None and choice == '':
            return default

        if choice in valid_yes_ans:
            return True

        if choice in valid_no_ans:
            return False

        sys.stdout.write("Please respond with 'yes' or 'no' "
                         "(or 'y' or 'n').\n")


def checkDependencies():
    """Dependency resolver based on a previously specified CONST_REQUIREMENTS_FILE.

    Currently checks a list of dependencies from a file and asks for user
    confirmation on whether to install it with a specific version or not.

    """

    if not args.ignore_deps:
        try:
            import pip
            modules = []
            f = open(FARADAY_REQUIREMENTS_FILE)
            for line in f:
                if not line.find('#'):
                    break
                else:
                    modules.append(line.strip('\n'))
            f.close()
            pip_dist = [dist.project_name.lower() for dist in pip.get_installed_distributions()]
            for module in modules:
                if module.lower() not in pip_dist:
                    try:
                        __import__(module)
                    except ImportError:
                        if query_user_bool("Missing module %s."
                            " Do you wish to install it?" % module):
                            pip.main(['install', "%s" %
                                     module, '--user'])

                        else:
                            return False
        except ImportError:
            pass

    return True


def startProfiler(app, output, depth):
    """Profiler handler.

    Will start a profiler on the given application in a specified output with
    a custom depth.

    TODO: Check if it's necessary to add a dummy in case o failed import.

    """

    logger.warning("[!] Faraday will be started with a profiler attached."
                   "Performance may be affected.")

    start = profile(app,
                    filename=output,
                    entries=depth)
    return start


def setConf():
    """User configuration management and instantiation.

    Setting framework configuration based either on previously user saved
    settings or default ones.

    """

    logger.info("Setting configuration.")

    CONF = getInstanceConfiguration()
    CONF.setDebugStatus(args.debug)

    host = CONF.getApiConInfoHost() if str(CONF.getApiConInfoHost()) != "None" else FARADAY_DEFAULT_HOST
    port_xmlrpc = CONF.getApiConInfoPort() if str(CONF.getApiConInfoPort()) != "None" else FARADAY_DEFAULT_PORT_XMLRPC
    port_rest = CONF.getApiRestfulConInfoPort() if str(CONF.getApiRestfulConInfoPort()) != "None" else FARADAY_DEFAULT_PORT_REST

    host = args.host if args.host else host
    port_xmlrpc = args.port_xmlrpc if args.port_xmlrpc else port_xmlrpc
    port_rest = args.port_rest if args.port_rest else port_rest

    CONF.setApiConInfoHost(host)
    CONF.setApiConInfoPort(port_xmlrpc)
    CONF.setApiRestfulConInfoPort(port_rest)


def startFaraday():
    """Application startup.

    Starts a MainApplication with the previously parsed arguments, and handles
    a profiler if requested.

    Returns application status.

    """
    from model.application import MainApplication

    logger.info("All done. Opening environment.")
    #TODO: Handle args in CONF and send only necessary ones.

    main_app = MainApplication(args)

    if not args.disable_excepthook:
            logger.info("Main application ExceptHook enabled.")
            main_app.enableExceptHook()

    if args.profile:
        logger.info("Starting main application with profiler.")
        start = startProfiler(main_app.start,
                              args.profile_output,
                              args.profile_depth)
    else:
        logger.info("Starting main application.")
        start = main_app.start
    from colorama import Fore, Back, Style
    import string
    couchURL = getInstanceConfiguration().getCouchURI()
    if couchURL:
        url = "%s/reports/_design/reports/index.html" % couchURL
        print(Fore.WHITE + Style.BRIGHT + \
            "\n*" + string.center("faraday ui is ready", 53 - 6) )
        print(Fore.WHITE + Style.BRIGHT + \
                """Make sure you got couchdb up and running.\nIf couchdb is up, point your browser to: \n[%s]""" % url)
    else:
        print(Fore.WHITE + Style.BRIGHT + \
                """Please config Couchdb for fancy HTML5 Dashboard (https://github.com/infobyte/faraday/wiki/Couchdb)""")

    print(Fore.RESET + Back.RESET + Style.RESET_ALL)

    exit_status = start()

    return exit_status


def setupPlugins(dev_mode=False):
    """Checks and handles Faraday's plugin status.

    When dev_mode is True, the user enters in development mode and the plugins
    will be replaced with the latest ones.

    Otherwise, it checks if the plugin folders exists or not, and creates it
    with its content.

    TODO: When dependencies are not satisfied ask user if he wants to try and
    run faraday with a inestability warning.

    """

    if dev_mode:
        logger.warning("Running under plugin development mode!")
        logger.warning("Using user plugins folder")
    else:
        if os.path.isdir(FARADAY_PLUGINS_PATH):
            logger.info("Removing old plugins folder.")
            shutil.rmtree(FARADAY_PLUGINS_PATH)
        else:
            logger.info("No plugins folder detected. Creating new one.")

        shutil.copytree(FARADAY_PLUGINS_BASEPATH, FARADAY_PLUGINS_PATH)

def setupZSH():
    """Cheks and handles Faraday's integration with ZSH.

    If the user has a .zshrc file, it gets copied and integrated with
    faraday's zsh plugin.

    """

    if os.path.isfile(USER_ZSHRC):
        shutil.copy(USER_ZSHRC, FARADAY_USER_ZSHRC)
    else:
        open(FARADAY_USER_ZSHRC, 'w').close()

    with open(FARADAY_USER_ZSHRC, "r+") as f:
        content = f.read()
        f.seek(0, 0)
        f.write('ZDOTDIR=$OLDZDOTDIR' + '\n' + content)
    with open(FARADAY_USER_ZSHRC, "a") as f:
        f.write("source \"%s\"" % FARADAY_BASE_ZSH)
    shutil.copy(FARADAY_BASE_ZSH, FARADAY_USER_ZSH_PATH)

def setupXMLConfig():
    """Checks user configuration file status.

    If there is no custom config the default one will be copied as a default.
    """

    if not os.path.isfile(FARADAY_USER_CONFIG_XML):
        logger.info("Copying default configuration from project.")
        shutil.copy(FARADAY_BASE_CONFIG_XML, FARADAY_USER_CONFIG_XML)
    else:
        logger.info("Using custom user configuration.")

def setupImages():
    """ Copy png icons
    """
    if os.path.exists(FARADAY_USER_IMAGES):
        shutil.rmtree(FARADAY_USER_IMAGES)
    shutil.copytree(FARADAY_BASE_IMAGES, FARADAY_USER_IMAGES)


def checkConfiguration(gui_type):
    """Checks if the environment is ready to run Faraday.

    Checks different environment requirements and sets them before starting
    Faraday. This includes checking for plugin folders, libraries,
    and ZSH integration.
    """
    logger.info("Checking configuration.")
    logger.info("Setting up plugins.")
    setupPlugins(args.dev_mode)
    logger.info("Setting up ZSH integration.")
    setupZSH()
    logger.info("Setting up user configuration.")
    setupXMLConfig()
    logger.info("Setting up icons for GTK interface.")
    setupImages()


def setupFolders(folderlist):
    """Checks if a list of folders exists and creates them otherwise.

    """

    for folder in folderlist:
        fp_folder = os.path.join(FARADAY_USER_HOME, folder)
        checkFolder(fp_folder)


def checkFolder(folder):
    """Checks whether a folder exists and creates it if it doesn't.

    """

    if not os.path.isdir(folder):
        if logger:
            logger.info("Creating %s" % folder)
        os.makedirs(folder)



def printBanner():
    """Prints Faraday's ascii banner.

    """
    from colorama import Fore, Back, Style
    print (Fore.RED + """
  _____                           .___
_/ ____\_____  ____________     __| _/_____   ___.__.
\   __\ \__  \ \_  __ \__  \   / __ | \__  \ <   |  |
 |  |    / __ \_|  | \// __ \_/ /_/ |  / __ \_\___  |
 |__|   (____  /|__|  (____  /\____ | (____  // ____|
             \/            \/      \/      \/ \/
    """)

    print(Fore.WHITE + Back.RED + Style.BRIGHT + \
    "[*[       Open Source Penetration Test IDE       ]*]")
    print(Back.RESET + "            Where pwnage goes multiplayer")
    print(Fore.RESET + Back.RESET + Style.RESET_ALL)
    logger.info("Starting Faraday IDE.")


def update():
    """Updates Faraday IDE.

    Deletes every .pyc file and does a git pull to the official repository.

    """
    if args.update:
        from updates.updater import Updater
        Updater().doUpdates()
        logger.info("Update process finished with no errors")
        logger.info("Faraday will start now.")

def checkUpdates():
    import requests
    uri = getInstanceConfiguration().getUpdatesUri()
    resp = u"OK"
    try:
        f = open(FARADAY_VERSION_FILE)

        getInstanceConfiguration().setVersion(f.read().strip())
        getInstanceConfiguration().setAppname("Faraday - Penetration Test IDE Community")
        parameter = {"version": getInstanceConfiguration().getVersion()}

        f.close()
        resp = requests.get(uri, params=parameter, timeout=1, verify=True)
        resp = resp.text.strip()
    except Exception as e:
        logger.error(e)
    if not resp == u'OK':
        logger.info("You have available updates. Run ./faraday.py --update to catchup!")
    else:
        logger.info("No updates available, enjoy Faraday.")


def checkCouchUrl():
    import requests
    try:
        requests.get(getInstanceConfiguration().getCouchURI(), timeout=5)
    except requests.exceptions.SSLError:
        print """
        SSL certificate validation failed.
        You can use the --cert option in Faraday
        to set the path of the cert
        """
        sys.exit(-1)
    except Exception as e:
        # Non fatal error
        pass

def checkVersion():
    try:
        f = open(FARADAY_VERSION_FILE)
        f_version = f.read().strip()
        if not args.update:
            if getInstanceConfiguration().getVersion() != None and getInstanceConfiguration().getVersion() != f_version:
                logger.warning("You have different version of Faraday since your last run.\nRun ./faraday.py --update to update configuration!")
                if query_yes_no('Do you want to close Faraday?', 'yes'):
                    sys.exit(-1)

        getInstanceConfiguration().setVersion(f_version)
        f.close()

    except Exception as e:
        getLogger("launcher").error("It seems that something's wrong with your version\nPlease contact customer support")
        sys.exit(-1)


def init():
    """Initializes what is needed before starting.

    For now we initialize logger and arguments setup.

    """

    global args
    global logger
    logger = None

    args = getParserArgs()
    setupFolders(CONST_FARADAY_FOLDER_LIST)
    setUpLogger(args.debug)
    logger = getLogger("launcher")


def main():
    """Main.

    Main function for launcher.

    """
    os.chdir(FARADAY_BASE)

    init()
    if checkDependencies():
        printBanner()
        logger.info("Dependencies met.")
        if args.cert_path:
            os.environ[REQUESTS_CA_BUNDLE_VAR] = args.cert_path
        checkConfiguration(args.gui)
        setConf()
        checkCouchUrl()
        checkVersion()
        update()
        checkUpdates()
        startFaraday()
    else:
        logger.error("Dependencies not met. Unable to start Faraday.")


if __name__ == '__main__':
    main()