def close(self): """ close session """ comm.sendpacket(self, op=conn.ccdlib.OP_LOGOUT) if isinstance(self.sock, socket): self.sock.close()
def use_group(client, groupid, pluginid=None): """ parse input parameters and switch to new connection group. There a basically two types of group usage: 1) groups act as default group for the hole ccd and 2) a group is assigned to a plugin/project combination. The project id is a member variable of the client, so no arguments are needed here. input: client the client instance groupid id of the group to use/active pluginid id of the plugin that might be associated with the group """ updateConnGroupList(client) # group id if not groupid in client.connection_groups: raise Exception("Invalid group id!") pld = dict(grpid=groupid) # plugin and project if pluginid and not client.project: raise Exception("No project set! Please execute 'set project <pid>'" " before!") pld["plg_id"] = pluginid pld["pid"] = client.project # send request comm.sendpacket(client, op=comm.ccdlib.OP_USEGROUP, pld=pld)
def set_project(self, projectid): """ the user must select a project """ logger.debug('Setting project %d' % projectid) pld = dict(pid=projectid) comm.sendpacket(self, op=conn.ccdlib.OP_SETPROJ, pld=pld) self.project = projectid self.toRootDir() self.resume()
def flush(client, args): """ send flush command to submit chain to server """ if not args: raise Exception("Not enough arguments!") try: chain = client.proxychains[args[0]] comm.sendpacket(client, op=comm.ccdlib.OP_FLUSHPCHAIN, pld=chain.as_dict()) except (KeyError, IndexError): raise Exception("Invalid chain id (%s)!" % args[0])
def flush(client, args): """ send flush command to submit group to server """ if not args: raise Exception("Not enough arguments!") try: group = client.connection_groups[args[0]] comm.sendpacket(client, op=comm.ccdlib.OP_FLUSHGROUP, pld=group.as_dict()) except (KeyError, IndexError): raise Exception("Invalid group id (%s)!" % args[0])
def show_projects(self, projectid="0"): """ return a list of projects, the user is in. If pid is 0, we get the projects the user is joined currently. Though the ccd response is a list of projects. input: pid project id to get information from output: result returns a string containg the information """ result = "" if projectid == "all": pld = dict(pid="") elif projectid: pld = dict(pid=projectid) else: pld = "" resp_t = comm.sendpacket(self, op=conn.ccdlib.OP_SHOWPROJ, pld=pld) for project in resp_t[-1]: result += "project %d:\n" % project["pid"] for k, v in project.items(): result += "\t%s:%s\n" % (k, v) return result
def update_user_pwd(self, userid, old, new): """ create a new user """ old = conn.ccdCrypto.hashPassword(old) new = conn.ccdCrypto.hashPassword(new) pld = dict(uid=userid, old=old, new=new) resp_t = comm.sendpacket(self, op=conn.ccdlib.OP_UPDATEUSERPWD, pld=pld) return resp_t[-1]
def del_chain(client, chainid): """ delete proxy chain input: chainid the id of the chain to delete output: payload of server response """ logger.debug("deleting chain %s", chainid) # build request if not chainid in client.proxychains: raise KeyError() pld = dict(pcid=chainid) # send request resp_t = comm.sendpacket(client, op=comm.ccdlib.OP_DELPCHAIN, pld=pld) # delete from cache del client.proxychains[pld["pcid"]] return resp_t[-1]
def _reload(self): """ reload the category's content. To do so, call comm.sendpacket and parse response. input: comm.sendpacket callback function to call for ccd request """ content = [] resp_t = comm.sendpacket(self.session, op=comm.ccdlib.OP_GETCAT, cat=self.id) try: for rc in resp_t[-1]: # remote.plugin if rc["isplugin"]: content += [remote.plugin.RemotePlugin( id=rc["id"], grpid=rc["conngroup"], session=self.session, help=rc["help"], name=(rc["name"] + remote.plugin.RemotePlugin.EXTENSION) )] # category else: content += [RemoteCategory(id=rc["id"], name=rc["name"], session=self.session)] except KeyError: raise Exception("Unprober json format for remote " "categories' content") self._content = content
def new_proxy(client, protocol, ip, port, description): """ create a new proxy input: protocol protocol the proxy is addressable with ip ip or domain of the proxy port port the proxy listens to description short description of the proxy output: proxy id """ try: pld = dict(ip=ip, port=port, protocol=protocol, description=description) except (KeyError, ValueError): return ("Invalid input. Example: new proxy protocol://" "ip:port proxy_name") # send request resp_t = comm.sendpacket(client, op=comm.ccdlib.OP_NEWPROXY, pld=pld) # create proxy object proxy = Proxy(pxid=resp_t[-1], **pld) # cache client.proxies[proxy.pxid] = proxy return proxy.pxid
def new_project(self, name, ptype, description=""): """ create a new project """ validate_name(name) validate_description(description) pld = dict(name=name, tid=ptype, description=description) resp_t = comm.sendpacket(self, op=conn.ccdlib.OP_NEWPROJ, pld=pld) return resp_t[-1]
def toRootDir(self): """ sends a request to get the id of root category. sets root cat. """ resp_t = comm.sendpacket(self, op=conn.ccdlib.OP_GETROOTCAT) _id = resp_t[-1]["id"] _name = resp_t[-1]["name"] self.root_category = category.RemoteCategory(id=_id, name=_name, session=self) self.current_category = self.root_category self.history = []
def killWindow(session, wid, force): """ kills window and sends kill request to ccd """ logger.debug("killing window %d (force=%s)", wid, force) win = get_win(session, wid) if win.status == RUNNING and not force: print("Nope, the plugin is still running. " "Use -f if you want to kill it anyways.") return None if win.wid > 0: comm.sendpacket(session, op=conn.ccdlib.OP_KILL, mth=conn.ccdlib.MTH_FORCE, plg=win.plg_id, pld=dict(rid=win.rid)) print("killed plugin.") win.close()
def show_user_in_project(self, projectid): """ get user information """ p = "" pld = dict(pid=projectid) resp_t = comm.sendpacket(self, op=conn.ccdlib.OP_SHOWUSER, pld=pld) for u in resp_t[-1]: p += "user %d:\n" % u["uid"] for k, v in u.items(): p += "\t%s:%s\n" % (k, v) return p
def getReport(self, fmt, filename, template): """ get database report in specified format [pdf, tex, csv] """ resp = comm.sendpacket(self, op=conn.ccdlib.OP_REPORT, mth=conn.ccdlib.MTH_NOP, pld={ 'fmt': fmt, 'filename': filename, 'template': template, 'pid': self.project, 'uid': 2 }) if resp[0] == conn.ccdlib.OP_SUCCESS: print('Report saved as %s' % resp[-1]) else: print('Error while generating Report')
def updateProxyList(client): """ ask server for configured proxies """ resp_t = comm.sendpacket(client, op=comm.ccdlib.OP_SHOWPROXY) for p in resp_t[-1]: try: proxy = Proxy(pxid=p["pxid"], ip=p["ip"], port=int(p["port"]), protocol=p["protocol"], description=p["description"]) client.proxies[proxy.pxid] = proxy except (KeyError, ValueError): logger.warning("Received invalid json packet!")
def updateProxyChainList(client): """ ask server for configured proxies. first, flush all local chains """ # reset client.proxychains = {} # and get all information resp_t = comm.sendpacket(client, op=comm.ccdlib.OP_SHOWPCHAIN) for p in resp_t[-1]: try: chain = ProxyChain(pcid=p["pcid"], description=p["description"]) chain.set_proxies(proxies=p["proxies"]) client.proxychains[chain.pcid] = chain except (KeyError, ValueError): logger.warning("Received invalid json packet!")
def workgroup_remove_member(session, workgroupid, userid): """ remove a member from workgroup input: session client session workgroupid id of the workgroup to update userid id of the plugin to remove output: payload of server response """ pld = dict(wid=workgroupid, uid=userid) resp_t = comm.sendpacket(session, op=comm.ccdlib.OP_DELWGROUPMEMBER, pld=pld) return resp_t[-1]
def view_data(self, columns=[], tables=[]): """ view information from project database """ logger.debug("viewing columns=%s, tables=%s", columns, tables) tables_str = ",".join(tables) columns_str = ",".join(columns) resp = comm.sendpacket(self, op=conn.ccdlib.OP_REPORT, mth=conn.ccdlib.MTH_VIEW, pld={ 'pid': self.project, 'columns': columns_str, 'tables': tables_str, }) if resp[0] != conn.ccdlib.OP_SUCCESS: print('Error while viewing information from database') else: return resp[-1]
def delete_workgroup(session, workgroupid): """ delete a workgroup input: session client session workgroupid id of the workgroup to delete output: payload of server response """ # verify that workgroup exists pld = dict(wid=workgroupid) resp_t = comm.sendpacket(session, op=comm.ccdlib.OP_DELWGROUP, pld=pld) return resp_t[-1]
def workgroup_remove_plugin(session, workgroupid, pluginid): """ remove a plugin from workgroup input: session client session workgroupid id of the workgroup to update pluginid id of the plugin to remove output: payload of server response """ validate.validate_pluginid(pluginid) pld = dict(wid=workgroupid, plg_id=pluginid) resp_t = comm.sendpacket(session, op=comm.ccdlib.OP_DELWGROUPPLG, pld=pld) return resp_t[-1]
def update_workgroup(session, workgroupid, name): """ update an existing workgroup input: session client session workgroupid id of the workgroup to update name new name to set output: server response payload """ validate.validate_name(name) pld = dict(wid=workgroupid, name=name) resp_t = comm.sendpacket(session, op=comm.ccdlib.OP_UPDATEWGROUP, pld=pld) return resp_t[-1]
def updateConnGroupList(client): """ ask server for configured connection groups """ # reset client.connection_groups = {} # and get all information resp_t = comm.sendpacket(client, op=comm.ccdlib.OP_SHOWGROUP) for p in resp_t[-1]: try: group = ConnectionGroup(grpid=p["grpid"], description=p["description"], default=p["default"]) group.set_chains(p["chains"]) client.connection_groups[group.grpid] = group except (KeyError, ValueError): logger.warning("Received invalid json packet!")
def new_workgroup(session, name, description=""): """ create a new workgroup input: session client session name name of the workgroup description descriptive words output: payload of server response. in case of success workgroup id """ validate.validate_name(name) validate.validate_description(description) pld = dict(name=name, description=description) resp_t = comm.sendpacket(session, op=comm.ccdlib.OP_NEWWGROUP, pld=pld) return resp_t[-1]
def new_user(self, name, mail, password, description=""): """ create a new user """ MAX_LEN_PASSWORD = 255 enc_password = conn.ccdCrypto.hashPassword(password) if len(enc_password) > MAX_LEN_PASSWORD: raise errors.InputError("Password exceeds limit of %d chars." % MAX_LEN_PASSWORD) validate_name(name) validate_mail(mail) validate_description(description) pld = dict(name=name, mail=mail, password=enc_password, description=description) resp_t = comm.sendpacket(self, op=conn.ccdlib.OP_NEWUSER, pld=pld) return resp_t[-1]
def workgroup_add_member(session, workgroupid, userid, roleid): """ add a member to workgroup input: session client session workgroupid id of the workgroup to update userid id of the plugin to add roleid id of the user's role. output: payload of server response """ logger.debug("adding member(uid:%s) to workgroup(wid:%s)", userid, workgroupid) pld = dict(wid=workgroupid, uid=userid, role_id=roleid) resp_t = comm.sendpacket(session, op=comm.ccdlib.OP_ADDWGROUPMEMBER, pld=pld) return resp_t[-1]
def del_group(client, groupid): """ delete connection group input: groupid id of the connection group to delete """ if not groupid in client.connection_groups: raise Exception("Invalid group id!") # send request pld = dict(grpid=groupid) resp_t = comm.sendpacket(client, op=comm.ccdlib.OP_DELGROUP, pld=pld) # delete from cache del client.connection_groups[pld["grpid"]] print(resp_t[-1])
def new_group(client, description): """ parse input parameters and create a new connection group. return group's id inpu: description short notes stored along with the conn group """ pld = dict(description=description) resp = comm.sendpacket(client, op=comm.ccdlib.OP_NEWGROUP, pld=pld)[-1] try: grpid = resp["grpid"] except KeyError: raise Exception("Invalid json format!") group = ConnectionGroup(grpid, description) client.connection_groups[group.grpid] = group print(group.grpid)
def show_user_in_workgroup(session, workgroupid): """ get user information input: session client session workgroupid id of the workgroup to show members of output: users information of workgroup members """ p = "" pld = dict(wid=workgroupid) resp_t = comm.sendpacket(session, op=comm.ccdlib.OP_SHOWUSER, pld=pld) for u in resp_t[-1]: p += "user %d:\n" % u["uid"] for k, v in u.items(): p += "\t%s:%s\n" % (k, v) return p
def new_chain(client, description): """ create a new proxy chain input: description some short description of the proxy chain output: id of newly created chain """ pld = dict(description=description) resp = comm.sendpacket(client, op=comm.ccdlib.OP_NEWPCHAIN, pld=pld)[-1] try: pcid = resp["pcid"] except KeyError: raise Exception("Invalid json format!") pc = ProxyChain(pcid, description) client.proxychains[pc.pcid] = pc return pc.pcid
def execute(self, args): """ execute plugin input: args plugin arguments sent to ccd output: rid, pld request id and response payload """ resp_t = comm.sendpacket(self.session, op=comm.ccdlib.OP_EXEC, mth=comm.ccdlib.MTH_EXEC, plg=self.id, pld=args) rid = resp_t[2] pld = resp_t[-1] return rid, pld
def get_workgroups(session, workgroupid): """ return the list of existing workgroups input: session client session workgroupid workgroup output: result string to contain the requested information """ if workgroupid == "all": pld = "" elif workgroupid: pld = dict(wid=workgroupid) else: pld = "" resp_t = comm.sendpacket(session, op=comm.ccdlib.OP_SHOWWGROUP, pld=pld) return resp_t[-1]
def save_data(self, columns=[], tables=[], filename='', output='', group_by='', lang=''): """ view information from project database """ if (tables or columns) and output == 'pdf': print('Warning: pdf format does currently not support table/column' 'selection') tables_str = ",".join(tables) columns_str = ",".join(columns) resp = comm.sendpacket(self, op=conn.ccdlib.OP_REPORT, mth=conn.ccdlib.MTH_SAVE, pld={ 'pid': self.project, 'columns': columns_str, 'tables': tables_str, 'filename': filename, 'group_by': group_by, 'fmt': output }) print repr(resp) if resp[0] != conn.ccdlib.OP_SUCCESS: print('Error while saving information from database') else: print('data saved as %s' % resp[-1])
def resume(self): """ request current state of plugins and create windows """ logger.debug("getting state of plugins that I once started") # get current running plugins and fds resp_t = comm.sendpacket(self, op=conn.ccdlib.OP_GETSTATE) # delete cached windows window.reset_all_win(self) # recreate all windows for running in resp_t[-1]: try: op = conn.ccdlib.OP_PLUGIN sid = self.sid rid = running["rid"] cat = 0 plg_id = running["plg_id"] gid = self.current_group_gid mth = conn.ccdlib.MTH_OUTPUT req_t = (op, sid, rid, cat, plg_id, gid, mth, resp_t[-1]) win = window.createWindow(self, winname=running["command"], req_t=req_t) if not running["state"]: win.set_state(2) else: win.set_state(running["state"]) except KeyError: raise Exception("Got invalid response from server!")
def del_proxy(client, proxyid): """ delete proxy input: proxyid id of the proxy output: payload of server response """ # build request if not proxyid in client.proxies: raise KeyError() pld = dict(pxid=proxyid) # send request resp_t = comm.sendpacket(client, op=comm.ccdlib.OP_DELPROXY, pld=pld) # delete from cache del client.proxies[pld["pxid"]] return resp_t[-1]