• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cisagov / gophish-tools / 4812977103

pending completion
4812977103

Pull #134

github

GitHub
Merge be94355f4 into d859c2be0
Pull Request #134: Lineage pull request for: skeleton

141 of 473 branches covered (29.81%)

Branch coverage included in aggregate %.

298 of 1270 relevant lines covered (23.46%)

1.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/tools/gophish_test.py
1
"""Send a duplicate assessment from Gophish to custom targets as a test.
2

3
Usage:
4
  Gophish-test [--log-level=LEVEL] ASSESSMENT_ID SERVER API_KEY
5
  Gophish-test (-h | --help)
6
  Gophish-test --version
7

8
Options:
9
  API_KEY                   Gophish API key.
10
  ASSESSMENT_ID             ID of the assessment to test.
11
  SERVER                    Full URL to Gophish server.
12
  -h --help                 Show this screen.
13
  --version                 Show version.
14
  -l --log-level=LEVEL      If specified, then the log level will be set to
15
                            the specified value.  Valid values are "debug", "info",
16
                            "warning", "error", and "critical". [default: info]
17

18
NOTE:
19
  * The test assessment is an exact copy of the real assessment that will be immediately sent
20
  to the custom targets provided in this tool.
21
"""
22

23
# Standard Python Libraries
24
import logging
×
25
import sys
×
26
from typing import Dict
×
27

28
# Third-Party Libraries
29
from docopt import docopt
×
30

31
# No type stubs exist for gophish, so we add "type: ignore" to tell mypy to
32
# ignore this library
33
from gophish.models import SMTP, Campaign, Group, Page, Template, User  # type: ignore
×
34
import urllib3
×
35

36
# cisagov Libraries
37
from tools.connect import connect_api
×
38
from util.input import get_input
×
39
from util.validate import validate_email
×
40

41
from ._version import __version__
×
42

43
# Disable "Insecure Request" warning: Gophish uses a self-signed certificate
44
# as default for https connections, which can not be  verified by a third
45
# party; thus, an SSL insecure request warning is produced.
46
urllib3.disable_warnings()
×
47

48

49
def get_campaigns(api, assessment_id):
×
50
    """Return a list of all campaigns in an assessment."""
51
    logging.info("Gathering Campaigns")
×
52
    allCampaigns = api.campaigns.get()
×
53
    assessmentCampaigns = list()
×
54

55
    for campaign in allCampaigns:
×
56
        if campaign.name.startswith(assessment_id):
×
57
            assessmentCampaigns.append(campaign)
×
58

59
    # Sets err to true if assessmentCampaigns has 0 length.
60
    logging.debug("Num Campaigns: %d", len(assessmentCampaigns))
×
61
    if not len(assessmentCampaigns):
×
62
        logging.warning("No Campaigns found for %s", assessment_id)
×
63

64
    return assessmentCampaigns
×
65

66

67
def add_group(api, assessment_id):
×
68
    """Create a test group."""
69
    logging.info("Adding Test Group")
×
70

71
    newGroup = Group()
×
72

73
    newGroup.name = "Test-" + assessment_id
×
74

75
    # Holds list of Users to be added to group.
76
    targets = list()
×
77

78
    target = User()
×
79
    target.first_name = get_input("Enter First Name: ")
×
80
    # Receives the file name and checks if it exists.
81
    while target.first_name != "done" or target.first_name == "":
×
82
        target.last_name = get_input("Enter Last Name: ")
×
83

84
        while True:
85
            target.email = get_input("Enter Email: ")
×
86
            if not validate_email(target.email):
×
87
                print("In Valid Email")
×
88
            else:
89
                break
×
90

91
        target.position = get_input("Enter Org: ")
×
92

93
        targets.append(target)
×
94

95
        target = User()
×
96
        target.first_name = get_input("Enter First Name or 'done': ")
×
97

98
    newGroup.targets = targets
×
99

100
    newGroup = api.groups.post(newGroup)
×
101

102
    return newGroup.name
×
103

104

105
def campaign_test(api, assessmentCampaigns, assessment_id):
×
106
    """Create test campaigns."""
107
    tempGroups = [Group(name=add_group(api, assessment_id))]
×
108

109
    for campaign in assessmentCampaigns:
×
110
        tempUrl = campaign.url
×
111
        tempName = "Test-" + campaign.name
×
112
        tempPage = Page(name=campaign.page.name)
×
113
        tempTemplate = Template(name=campaign.template.name)
×
114
        tempSmtp = SMTP(name=campaign.smtp.name)
×
115

116
        postCampaign = Campaign(
×
117
            name=tempName,
118
            groups=tempGroups,
119
            page=tempPage,
120
            template=tempTemplate,
121
            smtp=tempSmtp,
122
            url=tempUrl,
123
        )
124

125
        postCampaign = api.campaigns.post(postCampaign)
×
126
        logging.debug("Test Campaign added: %s", postCampaign.name)
×
127

128
    logging.info("All Test campaigns added.")
×
129

130
    return True
×
131

132

133
def main() -> None:
×
134
    """Set up logging, connect to API, load all test data."""
135
    args: Dict[str, str] = docopt(__doc__, version=__version__)
×
136

137
    # Set up logging
138
    log_level = args["--log-level"]
×
139
    try:
×
140
        logging.basicConfig(
×
141
            format="\n%(levelname)s: %(message)s", level=log_level.upper()
142
        )
143
    except ValueError:
×
144
        logging.critical(
×
145
            '"%s" is not a valid logging level.  Possible values are debug, info, warning, and error.',
146
            log_level,
147
        )
148
        sys.exit(1)
×
149

150
    # Connect to API
151
    try:
×
152
        api = connect_api(args["API_KEY"], args["SERVER"])
×
153
        logging.debug("Connected to: %s", args["SERVER"])
×
154
    except Exception as e:
×
155
        logging.critical(e.args[0])
×
156
        sys.exit(1)
×
157

158
    assessmentCampaigns = get_campaigns(api, args["ASSESSMENT_ID"])
×
159

160
    if len(assessmentCampaigns) > 0:
×
161
        campaign_test(api, assessmentCampaigns, args["ASSESSMENT_ID"])
×
162

163
    # Stop logging and clean up
164
    logging.shutdown()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc