Codebase list osrframework / a008289 osrframework / utils / platforms.py
a008289

Tree @a008289 (Download .tar.gz)

platforms.py @a008289raw · 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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
################################################################################
#
#    Copyright 2015-2020 FĂ©lix Brezo and Yaiza Rubio
#
#    This program is part of OSRFramework. You can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

import json
import random
import re
import urllib

import osrframework.utils.browser as browser
import osrframework.utils.general as general
from osrframework.utils.exceptions import *


class Platform(object):
    """<Platform> class"""
    def __init__(self):
        pass

    def __init__(self, name, tags):
        """Constructor with parameters

        This method permits the developer to instantiate dinamically Platform
        objects."""
        self.platformName = name.lower().title()
        self.tags = tags
        self.modes = {
            "usufy": {
                "debug": False,
                "extra_fields": {
                    "com.i3visio.Location.Birth": "Born: [^<]+",    # Regular expresion to extract the alias
                },
                "needs_credentials": False,
                "not_found_text": "<h1>404</h1>",                   # Text that indicates a missing profile
                "query_validator": ".+",                            # Regular expression that the alias SHOULD match
                "url": "http://demo.demo/user/{placeholder}",       # Target URL where {placeholder} would be modified by the alias
            },
            "searchfy": {
                "debug": False,
                "extra_fields": {
                    "com.i3visio.Alias": "My alias: ([^<]+)",       # Regular expresion to extract the alias
                },
                "needs_credentials": False,
                "not_found_text": "<h1>404</h1>",
                "query_validator": ".+",
                "url": "http://demo.demo/user/{placeholder}",
                # Needed function to extract aliases from the website
                "alias_regexp": "demo.demo/(.+)"
            },
            # Reimplementation needed of check_mailfy
            "mailfy": {},
        }

    def create_url(self, word, mode="phonefy"):
        """Method to create the URL replacing the word in the appropriate URL

        Args:
            word (str): Word to be searched.
            mode (str): Mode to be executed.

        Returns:
            The URL to be queried.
        """
        try:
            return self.modes[mode]["url"].replace("{placeholder}", word)
        except:
            if mode == "base":
                if word[0] == "/":
                    return self.baseURL + word[1:], word
                else:
                    return self.baseURL + word
            else:
                try:
                    return self.url[mode].replace("<"+mode+">", word)
                except:
                    pass
        return None

    def launch_query_for_mode(self, query=None, mode=None):
        """Method that launches an i3Browser to collect data

        Args:
            query: The query to be performed
            mode: The mode to be used to build the query.

        Returns:
            A string containing the recovered data or None."""
        # Creating the query URL for that mode
        qURL = self.create_url(word=query, mode=mode)
        i3Browser = browser.Browser()
        try:
            # Check if it needs creds
            needs_credentials =False
            try:
                # Suport for version 2 of wrappers
                if self.modes[mode]["needs_credentials"]:
                    needs_credentials = True
            except AttributeError as e:
                if self.needsCredentials[mode]:
                    needs_credentials = True

            if needs_credentials:
                self._getAuthenticated(i3Browser, qURL)
                data = i3Browser.recover_url(qURL)
            else:
                # Accessing the resources
                data = i3Browser.recover_url(qURL)
            return data
        except KeyError:
            print(general.error("[*] '{}' is not a valid mode for this wrapper ({}).".format(mode, self.__class__.__name__)))

        return None

    def get_info(self, query=None, mode="phonefy"):
        """Method that checks the presence of a given query

        It recovers the first list of complains.

        Args:
            query (str): Query to verify.
            mode (str): Mode to be executed.

        Returns:
            Python structure for the html processed.

        Raises:
            NoCredentialsException.
            NotImplementedModeError.
            BadImplementationError.
        """
        results = []
        data = ""

        if self._mode_is_valid(mode=mode) and self._is_valid_query(query, mode=mode):
            if mode in ["mailfy", "phonefy", "searchfy", "usufy"]:
                try:
                    results = getattr(self, "do_{}".format(mode))(query)
                except AttributeError as e:
                    raise NotImplementedModeError(str(self), mode)

        return json.dumps(results)

    def _mode_is_valid(self, mode):
        """Verification of whether the mode is a correct option to be used in the platform

        Args:
            mode (str): Mode to be executed.

        Returns:
            bool. True if the mode exists.
        """
        try:
            # Suport for version 2 of wrappers
            return mode in self.modes.keys()
        except AttributeError as e:
            # Legacy for mantaining old wrappers
            return self.isValidMode.get(mode, False)
        return False

    def __str__(self):
        """Function to represent the text when printing the object

        Returns:
            self.platformName
        """
        try:
            return self.parameterName
        except:
            return self.platformName


    def __eq__(self, obj):
        """Function to check if two wrappers are the same based

        Returns:
            True or False
        """
        try:
            return self.platformName == obj.platformName
        except:
            return False
    # ------------------
    # Internal functions
    # ------------------

    def _getAuthenticated(self, browser, url):
        """Getting authenticated

        This method may be overwritten.
        TODO: update to version 2 of the wrappers.

        Args:
            browser: The browser in which the user will be authenticated.
            url: The URL to get authenticated in.

        Returns:
            True or False.

        Raises:
            NoCredentialsException: If no valid credentials have been found.
            BadImplementationError: If an expected attribute is missing.
        """
        # check if we have creds
        try:
            if len(self.creds) > 0:
                # TODO: in choosing a cred there is an uneeded nesting of arrays
                c = random.choice(self.creds)[0]
                # adding the credential
                browser.setNewPassword(c.user, c.password)
                return True
            else:
                raise NoCredentialsException(str(self))
        except AttributeError as e:
            raise BadImplementationError(str(e))


    def _is_valid_query(self, query, mode="phonefy"):
        """Method to verify if a given query is processable by the platform.

        The system looks for the forbidden characters in self.Forbidden list.

        Args:
            query: The query to be launched.
            mode: To be chosen amongst mailfy, phonefy, usufy, searchfy.

        Returns:
            bool.
        """
        try:
            # Suport for version 2 of wrappers
            validator = self.modes[mode].get("query_validator")
            if validator:
                try:
                    compiledRegexp = re.compile(
                        "^{expr}$".format(
                            expr=validator
                        )
                    )
                    return compiledRegexp.match(query)
                except AttributeError as e:
                    return True

        except AttributeError as e:
            # Legacy for mantaining old wrappers
            compiledRegexp = re.compile("^{r}$".format(r=self.validQuery[mode]))
            return compiledRegexp.match(query)


    def _something_found(self, data, mode):
        """Verifying if something was found by trying to fin the not found text

        Args:
            data: Data where the self.notFoundText will be searched.
            mode: Mode to be executed.

        Returns:
            True if exists.
        """
        if data:
            try:
                for text in self.notFoundText[mode]:
                    if text in data:
                        return False
                return True
            except AttributeError as e:
                # Update to version 2 of the wrappers.
                verifier = self.modes.get(mode)
                if verifier:
                    if verifier.get("not_found_text", "") in data:
                        return False
                    else:
                        return True
        return False

    # ---------
    # Verifiers
    # ---------

    def check_mailfy(self, query, **kwargs):
        """Verifying a mailfy query in this platform

        This might be redefined in any class inheriting from Platform. The only
        condition is that any of this should return a dictionary as defined.

        Args:
            query: The element to be searched.
            kwargs: Dictionary with extra parameters. Just in case.

        Returns:
            Returns the collected data if exists or None if not.
        """
        data = self.launch_query_for_mode(query=query, mode="mailfy")
        if self._something_found(data, mode="mailfy"):
            return data
        return None

    def do_mailfy(self, query, **kwargs):
        """Verifying a mailfy query in this platform

        This might be redefined in any class inheriting from Platform. The only
        condition is that any of this should return an equivalent array.

        Args:
            query: The element to be searched.

        Returns:
            A list of elements to be appended. A sample output format is as follows:
            [
              {
                "attributes": [
                  {
                    "attributes": [],
                    "type": "com.i3visio.Email",
                    "value": "[email protected]"
                  },
                  {
                    "attributes": [],
                    "type": "com.i3visio.Alias",
                    "value": "contacto"
                  },
                  {
                    "attributes": [],
                    "type": "com.i3visio.Domain",
                    "value": "i3visio.com"
                  },
                  {
                    "attributes": [],
                    "type": "com.i3visio.Platform",
                    "value": "Twitter"
                  }
                ],
                "type": "com.i3visio.Profile",
                "value": "Twitter - [email protected]"
              }
            ]
        """
        if self.check_mailfy(query, **kwargs):
            expandedEntities = general.expand_entities_from_email(query)
            r = {
                "type": "com.i3visio.Profile",
                "value": self.platformName + " - " + query,
                "attributes": expandedEntities + [
                    {
                        "type": "com.i3visio.Platform",
                        "value": self.platformName,
                        "attributes": []
                    }
                ]
            }
            return [r]
        return []

    def check_searchfy(self, query, **kwargs):
        """Verifying a mailfy query in this platform

        This might be redefined in any class inheriting from Platform. The only
        condition is that any of this should return a dictionary as defined.

        Args:
            query: The element to be searched.
            kwargs: Dictionary with extra parameters. Just in case.

        Returns:
            Returns the collected data if exists or None if not.
        """
        data = self.launch_query_for_mode(query=query, mode="searchfy")
        if self._something_found(data, mode="searchfy"):
            return data
        return None

    def do_searchfy(self, query, **kwargs):
        """Verifying a searchfy query in this platform

        This might be redefined in any class inheriting from Platform.

        Performing additional procesing may be possible by iterating the requested profiles
        to extract more entities from the URI would be slow. Sample code may be:

            if kwargs["process"]:
                r["attributes"] += json.loads(self.get_info(process=True, mode="usufy", qURI=uri, query=i))

        Args:
            query: The element to be searched.

        Returns:
            A list of elements to be appended.
        """
        results = []
        print(f"[*] Launching search using the {self.__class__.__name__} module...")
        test = self.check_searchfy(query, **kwargs)

        if test:
            # Add flexibility
            try:
                try:
                    regexp = self.searchfyAliasRegexp
                    entity_type = "com.i3visio.Alias"
                except AttributeError:
                    regexp = self.searchfyEmailRegexp
                    entity_type = "com.i3visio.Email"

                # Recovering all the found aliases in the traditional way
                ids = re.findall(regexp, test, re.DOTALL)
            except:
                # Version 2 of the wrappers
                verifier = self.modes.get("searchfy")

                if verifier and verifier.get("alias_regexp"):
                    entity_type = "com.i3visio.Alias"
                    ids = re.findall(verifier.get("alias_regexp"), test, re.DOTALL)
                if verifier and verifier.get("email_regexp"):
                    entity_type = "com.i3visio.Email"
                    ids = re.findall(verifier.get("email_regexp"), test, re.DOTALL)

            for j, alias in enumerate(ids):
                r = {
                    "type": "com.i3visio.Profile",
                    "value": self.platformName + " - " + alias,
                    "attributes": []
                }

                # Appending platform name
                aux = {}
                aux["type"] = "com.i3visio.Platform"
                aux["value"] = self.platformName
                aux["attributes"] = []
                r["attributes"].append(aux)

                # Appending the alias
                aux = {}
                aux["type"] = entity_type
                aux["value"] = alias
                aux["attributes"] = []
                r["attributes"].append(aux)

                # Appending the query performed to grab this items
                aux = {}
                aux["type"] = "com.i3visio.Search"
                aux["value"] = query
                aux["attributes"] = []
                r["attributes"].append(aux)

                # Appending platform URI
                try:
                    aux = {}
                    aux["type"] = "com.i3visio.URI"
                    uri = self.create_url(word=alias, mode="base")
                    aux["value"] = uri
                    aux["attributes"] = []
                    r["attributes"].append(aux)
                except AttributeError:
                    aux = {}
                    aux["type"] = "com.i3visio.URI"
                    uri = self.create_url(word=alias, mode="usufy")
                    aux["value"] = uri
                    aux["attributes"] = []
                    r["attributes"].append(aux)

                if entity_type == "com.i3visio.Email":
                    r["attributes"] += general.expand_entities_from_email(alias)

                # Appending the result to results: in this case only one profile will be grabbed"""
                results.append(r)
        return results

    def check_phonefy(self, query, **kwargs):
        """Verifying a mailfy query in this platform

        This might be redefined in any class inheriting from Platform.
        The only condition is that any of this should return a dictionary as defined.

        Args:
            query (str): The element to be searched.
            kwargs (dict): Dictionary with extra parameters. Just in case.

        Returns:
            Returns the collected data if exists or None if not.
        """
        data = self.launch_query_for_mode(query=query, mode="phonefy")
        if self._something_found(data, mode="phonefy"):
            return data
        return None

    def do_phonefy(self, query, **kwargs):
        """Verifying a phonefy query in this platform

        This might be redefined in any class inheriting from Platform.

        Args:
            query (str): The element to be searched.

        Returns:
            A list of elements to be appended.
        """
        results = []

        test = self.check_phonefy(query, **kwargs)

        if test:
            r = {
                "type": "com.i3visio.Phone",
                "value": self.platformName + " - " + query,
                "attributes": []
            }

            try:
                aux = {
                    "type": "com.i3visio.URI",
                    "value": self.create_url(query, mode="phonefy"),
                    "attributes": []
                }
                r["attributes"].append(aux)
            except:
                pass

            aux = {
                "type": "com.i3visio.Platform",
                "value": self.platformName,
                "attributes": []
            }
            r["attributes"].append(aux)

            # V2 of the wrappers
            r["attributes"] += self.process_phonefy(test)
            results.append(r)

        return results

    def process_phonefy(self, data):
        """Method to process and extract the entities of a phonefy

        Args:
            data: The information from which the info will be extracted.

        Returns:
            A list of the entities found.
        """
        mode = "phonefy"

        info = []

        try:
            # v2
            verifier = self.modes.get(mode, {}).get("extra_fields", {})
            for field in verifier.keys():
                regexp = verifier[field]
                values = re.findall(regexp, data)

                for val in values:
                    aux = {}
                    aux["type"] = field
                    aux["value"] = val
                    aux["attributes"] = []
                    if aux not in info:
                        info.append(aux)
        except AttributeError as e:
            # Legacy
            for field in self.fieldsRegExp[mode].keys():
                # Recovering the RegularExpression
                try:
                    # Using the old approach of "Start" + "End"
                    regexp = self.fieldsRegExp[mode][field]["start"]+"([^\)]+)"+self.fieldsRegExp[mode][field]["end"]

                    tmp = re.findall(regexp, data)

                    # Now we are performing an operation just in case the "end" tag is found  in the results, which would mean that the tag selected matches something longer in the data.
                    values = []
                    for t in tmp:
                        if self.fieldsRegExp[mode][field]["end"] in t:

                            values.append(t.split(self.fieldsRegExp[mode][field]["end"])[0])
                        else:
                            values.append(t)
                except:
                    # Using the compact approach if start and end tags do not exist.
                    regexp = self.fieldsRegExp[mode][field]

                    values = re.findall(regexp, data)

                for val in values:
                    aux = {}
                    aux["type"] = field
                    aux["value"] = val
                    aux["attributes"] = []
                    if aux not in info:
                        info.append(aux)
        return info

    def check_usufy(self, query, **kwargs):
        """Verifying a mailfy query in this platform

        This might be redefined in any class inheriting from Platform. The only
        condition is that any of this should return a dictionary as defined.

        Args:
            query: The element to be searched.
            kwargs: Dictionary with extra parameters. Just in case.

        Returns:
            Returns the collected data if exists or None if not.
        """
        data = self.launch_query_for_mode(query=query, mode="usufy")
        if self._something_found(data, mode="usufy"):
            return data
        return None

    def do_usufy(self, query, **kwargs):
        """Verifying a usufy query in this platform

        This might be redefined in any class inheriting from Platform.

        Args:
            query: The element to be searched.

        Returns:
            A list of elements to be appended.
        """
        results = []

        test = self.check_usufy(query, **kwargs)

        if test:
            r = {
                "type": "com.i3visio.Profile",
                "value": self.platformName + " - " + query,
                "attributes": []
            }

            # Appending platform URI
            aux = {}
            aux["type"] = "com.i3visio.URI"
            aux["value"] = self.create_url(word=query, mode="usufy")
            aux["attributes"] = []
            r["attributes"].append(aux)
            # Appending the alias
            aux = {}
            aux["type"] = "com.i3visio.Alias"
            aux["value"] = query
            aux["attributes"] = []
            r["attributes"].append(aux)
            # Appending platform name
            aux = {}
            aux["type"] = "com.i3visio.Platform"
            aux["value"] = self.platformName
            aux["attributes"] = []
            r["attributes"].append(aux)

            r["attributes"] += self.process_usufy(test)

            results.append(r)
        return results

    def process_usufy(self, data):
        """Method to process and extract the entities of a usufy

        Args:
            data: The information from which the info will be extracted.

        Returns:
            A list of the entities found.
        """
        mode = "usufy"
        info = []

        try:
            # v2
            verifier = self.modes.get(mode, {}).get("extra_fields", {})
            for field in verifier.keys():
                regexp = verifier[field]
                values = re.findall(regexp, data)

                for val in values:
                    aux = {}
                    aux["type"] = field
                    aux["value"] = val
                    aux["attributes"] = []
                    if aux not in info:
                        info.append(aux)
        except AttributeError as e:
            # Legacy
            for field in self.fieldsRegExp[mode].keys():
                # Recovering the RegularExpression
                try:
                    # Using the old approach of "Start" + "End"
                    regexp = self.fieldsRegExp[mode][field]["start"]+"([^\)]+)"+self.fieldsRegExp[mode][field]["end"]

                    tmp = re.findall(regexp, data)

                    # Now we are performing an operation just in case the "end" tag is found  in the results, which would mean that the tag selected matches something longer in the data.
                    values = []
                    for t in tmp:
                        if self.fieldsRegExp[mode][field]["end"] in t:

                            values.append(t.split(self.fieldsRegExp[mode][field]["end"])[0])
                        else:
                            values.append(t)
                except:
                    # Using the compact approach if start and end tags do not exist.
                    regexp = self.fieldsRegExp[mode][field]

                    values = re.findall(regexp, data)

                for val in values:
                    aux = {}
                    aux["type"] = field
                    aux["value"] = val
                    aux["attributes"] = []
                    if aux not in info:
                        info.append(aux)
        return info

    def setCredentials(self, cred):
        """Getting the credentials and appending it to self.creds"""
        try:
            self.creds.append(cred)
        except:
            pass