Ejemplo n.º 1
0
    def setAttribute(self, name, value):
        dom_logging(log, "setAttribute", name, value)
        if not isinstance(name, basestring):
            name = str(name)

        self.tag[name] = value

        if name.lower() in ('src', 'archive'):
            s = urlparse.urlsplit(value)

            handler = getattr(log.SchemeHandler, 'handle_%s' % (s.scheme, ), None)
            if handler:
                handler(self.doc.window, value)
                return
            
            try:
                response = self.doc.window._navigator.fetch(value, redirect_type = "element workaround")
            except:
                return
                
            if response.status_code == 404:
                return

            ctype = response.headers.get('content-type', None)
            if ctype is None:
                return

            handler = log.MIMEHandler.get_handler(ctype)
            if handler:
                handler(self.doc.window.url, response.content)
Ejemplo n.º 2
0
    def setAttribute(self, name, value):
        dom_logging(log, "setAttribute", name, value)
        if not isinstance(name, basestring):
            name = str(name)

        self.tag[name] = value

        if name.lower() in ('src', 'archive'):
            s = urlparse.urlsplit(value)

            handler = getattr(log.SchemeHandler, 'handle_%s' % (s.scheme, ),
                              None)
            if handler:
                handler(self.doc.window, value)
                return

            try:
                response = self.doc.window._navigator.fetch(
                    value, redirect_type="element workaround")
            except:
                return

            if response.status_code == 404:
                return

            ctype = response.headers.get('content-type', None)
            if ctype is None:
                return

            handler = log.MIMEHandler.get_handler(ctype)
            if handler:
                handler(self.doc.window.url, response.content)
Ejemplo n.º 3
0
    def write(self, html):
        dom_logging(log, "document.write", html)
        if self._html:
            self._html.write(html)
            return

        tag = self.current
        parent = tag.parent
        pos = parent.contents.index(tag) + 1

        if not isinstance(html, basestring):
            html = str(html)

        soup = BeautifulSoup.BeautifulSoup(html, "html5lib")
        soup.html.unwrap()
        soup.head.unwrap()
        soup.body.unwrap()

        for tag in soup:
            parent.insert(pos, tag)

            pos += 1

            name = getattr(tag, "name", None)
            if name in ("script", None):
                continue

            try:
                handler = getattr(self._win.doc.DFT, "handle_%s" % (name,), None)
            except:
                handler = getattr(log.DFT, "handle_%s" % (name,), None)

            if handler:
                handler(tag)
Ejemplo n.º 4
0
    def write(self, html):
        dom_logging(log, "document.write", html)
        if self._html:
            self._html.write(html)
            return

        tag = self.current
        parent = tag.parent
        pos = parent.contents.index(tag) + 1

        if not isinstance(html, basestring):
            html = str(html)

        soup = BeautifulSoup.BeautifulSoup(html, "html5lib")
        soup.html.unwrap()
        soup.head.unwrap()
        soup.body.unwrap()

        for tag in soup:
            parent.insert(pos, tag)

            pos += 1

            name = getattr(tag, "name", None)
            if name in ('script', None):
                continue

            try:
                handler = getattr(self._win.doc.DFT, "handle_%s" % (name, ),
                                  None)
            except:
                handler = getattr(log.DFT, "handle_%s" % (name, ), None)

            if handler:
                handler(tag)
Ejemplo n.º 5
0
    def insertBefore(self, newChild, refChild):
        dom_logging(log, 'insertBefore', str(newChild.tag))
        if not newChild:
            raise DOMException(DOMException.HIERARCHY_REQUEST_ERR)
        
        if not isinstance(newChild, Node):
            raise DOMException(DOMException.HIERARCHY_REQUEST_ERR)

        # If refChild is null, insert newChild at the end of the list 
        # of children
        if not refChild:
            return self.appendChild(newChild)

        if not isinstance(refChild, Node):
            raise DOMException(DOMException.HIERARCHY_REQUEST_ERR)

        #index = self.findChild(refChild)
        #if index < 0 and not self.is_text(refChild):
        #    raise DOMException(DOMException.NOT_FOUND_ERR)

        # If the newChild is already in the tree, it is first removed
        if getattr(newChild, 'tag', None) and newChild.tag in self.tag.contents:
            newChildHash = hash(newChild.tag._node)

            for p in self.tag.contents:
                if getattr(p, '_node', None) is None:
                    continue

                if newChildHash == hash(p._node):
                    p.extract()

            #self.tag.contents.remove(newChild.tag)

        index = self.findChild(refChild)
        if index < 0 and not self.is_text(refChild):
            raise DOMException(DOMException.NOT_FOUND_ERR)

        if self.is_text(newChild):
            self.tag.insert(index, newChild.data.output_ready(formatter = lambda x: x))
            return newChild

        if newChild.nodeType in (Node.DOCUMENT_FRAGMENT_NODE, ):
            # self.tag.insert(index, newChild.tag.findChild())
            node = None

            for p in newChild.tag.find_all_next():
                if node is None:
                    self.tag.insert(index, p)
                else:
                    node.append(p)

                node = p
                    
            return newChild

        self.tag.insert(index, newChild.tag)
        return newChild
Ejemplo n.º 6
0
 def setter(self, text):
     if tag == 'innerHTML':
         dom_logging(log, "set innerHTML to", text)
     #if self.tag.string:
     #    self.tag.contents[0] = BeautifulSoup.NavigableString(text)
     #else:
     #   self.tag.append(text)
     #
     #self.tag.string = self.tag.contents[0]
     self.tag.string = text
Ejemplo n.º 7
0
    def setNamedItem(self, attr):
        oldattr = self.parent.getAttributeNode(attr.name)

        attr.parent = self.parent

        dom_logging(log, "setNamedItem", attr.value)
        self.parent.tag[attr.name] = attr.value

        if oldattr:
            oldattr.parent = None

        return oldattr
Ejemplo n.º 8
0
    def setNamedItem(self, attr):
        oldattr = self.parent.getAttributeNode(attr.name)

        attr.parent = self.parent

        dom_logging(log, "setNamedItem", attr.value)
        self.parent.tag[attr.name] = attr.value

        if oldattr:
            oldattr.parent = None

        return oldattr
Ejemplo n.º 9
0
    def appendChild(self, newChild):
        dom_logging(log, "appendChild", str(newChild.tag))
        # NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly
        if self.is_readonly(self):
            raise DOMException(DOMException.NO_MODIFICATION_ALLOWED)

        if self.is_text(self):
            raise DOMException(DOMException.HIERARCHY_REQUEST_ERR)

        if not newChild:
            raise DOMException(DOMException.HIERARCHY_REQUEST_ERR)

        if not isinstance(newChild, Node):
            raise DOMException(DOMException.HIERARCHY_REQUEST_ERR)

        # If the newChild is already in the tree, it is first removed
        if getattr(newChild, 'tag', None) and newChild.tag in self.tag.contents:
            newChildHash = hash(newChild.tag._node)

            for p in self.tag.contents:
                if getattr(p, '_node', None) is None:
                    continue

                if newChildHash == hash(p._node):
                    p.extract()
            #self.tag.contents.remove(newChild.tag)

        if self.is_text(newChild):
            self.tag.append(newChild.data.output_ready(formatter = lambda x: x))
            return newChild

        if newChild.nodeType in (Node.DOCUMENT_FRAGMENT_NODE, ):
            #self.tag.append(newChild.tag.findChild())
            node = self.tag
            for p in newChild.tag.find_all_next():
                node.append(p)
                node = p

            return newChild

        self.tag.append(newChild.tag)
        return newChild
Ejemplo n.º 10
0
    def replaceChild(self, newChild, oldChild):
        dom_logging(log, "replaceChild", str(newChild.tag))
        # NO_MODIFICATION_ALLOWED_ERR: Raised if this node or the parent of 
        # the new node is readonly.
        if self.is_readonly(self):
            raise DOMException(DOMException.NO_MODIFICATION_ALLOWED)

        parent = getattr(newChild, 'parentNode', None)
        if parent:
            if self.is_readonly(parent):
                raise DOMException(DOMException.NO_MODIFICATION_ALLOWED)

        if not newChild or not oldChild:
            raise DOMException(DOMException.HIERARCHY_REQUEST_ERR)

        if not isinstance(newChild, Node) or not isinstance(oldChild, Node):
            raise DOMException(DOMException.HIERARCHY_REQUEST_ERR)

        index = self.findChild(oldChild)
        if index < 0 and not self.is_text(refChild):
            raise DOMException(DOMException.NOT_FOUND_ERR)

        if self.is_text(newChild):
            self.tag.contents[index] = newChild.data.output_ready(formatter = lambda x: x)
            return oldChild

        if newChild.nodeType in (Node.DOCUMENT_FRAGMENT_NODE, ):
            #self.tag.contents[index] = newChild.tag.findChild()
            node = None

            for p in newChild.tag.find_all_next():
                if node is None:
                    self.tag.contents[index] = p
                else:
                    node.append(p)

                node = p

            return oldChild

        self.tag.contents[index] = newChild.tag
        return oldChild
Ejemplo n.º 11
0
    def evalScript(self, script, tag = None):
        dom_logging(log, 'eval script', script)
        result = 0

        try:
            log.JSClassifier.classify('[Local analysis]' if log.ThugOpts.local else self.url,
                                      script)
        except:
            pass

        if tag:
            self.doc.current = tag
        else:
            try:
                body = self.doc.body
            except:
                # This code is for when you are desperate :)
                body = self.doc.getElementsByTagName('body')[0]

            if body and body.tag.contents:
                self.doc.current = body.tag.contents[-1]
            else:
                self.doc.current = self.doc.doc.contents[-1]

        with self.context as ctxt:
            try:
                ast = AST(self, script)
            except:
                log.debug(traceback.format_exc())
                return result

            if log.ThugOpts.Personality.isIE():
                cc = CCInterpreter()
                script = cc.run(script)

            shellcode = Shellcode.Shellcode(self, ctxt, ast, script)
            result    = shellcode.run()

        return result
Ejemplo n.º 12
0
    def evalScript(self, script, tag=None):
        dom_logging(log, 'eval script', script)
        result = 0

        try:
            log.JSClassifier.classify(
                '[Local analysis]' if log.ThugOpts.local else self.url, script)
        except:
            pass

        if tag:
            self.doc.current = tag
        else:
            try:
                body = self.doc.body
            except:
                # This code is for when you are desperate :)
                body = self.doc.getElementsByTagName('body')[0]

            if body and body.tag.contents:
                self.doc.current = body.tag.contents[-1]
            else:
                self.doc.current = self.doc.doc.contents[-1]

        with self.context as ctxt:
            try:
                ast = AST(self, script)
            except:
                log.debug(traceback.format_exc())
                return result

            if log.ThugOpts.Personality.isIE():
                cc = CCInterpreter()
                script = cc.run(script)

            shellcode = Shellcode.Shellcode(self, ctxt, ast, script)
            result = shellcode.run()

        return result
Ejemplo n.º 13
0
 def _navigate(self, location):
     dom_logging(log, "Window.navigate {}".format(location))
     self.location = location
     return 0
Ejemplo n.º 14
0
 def setLocation(self, location):
     dom_logging(log, "Window.setLocation", location)
     self._location.href = location
Ejemplo n.º 15
0
 def setAttributeNode(self, attr):
     dom_logging(log, "setAttributeNode", attr.name, attr.value)
     self.tag[attr.name] = attr.value
Ejemplo n.º 16
0
 def setAttributeNode(self, attr):
     dom_logging(log, "setAttributeNode", attr.name, attr.value)
     self.tag[attr.name] = attr.value
Ejemplo n.º 17
0
 def setLocation(self, location):
     dom_logging(log, "Window.setLocation", location)
     self._location.href = location
Ejemplo n.º 18
0
 def setNodeValue(self, value):
     dom_logging(log, 'setNodeValue', str(value))
     pass
Ejemplo n.º 19
0
 def _navigate(self, location):
     dom_logging(log, "Window.navigate {}".format(location))
     self.location = location
     return 0