forked from mdsecactivebreach/LinkedInt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLinkedInt.py
executable file
·748 lines (646 loc) · 24.2 KB
/
LinkedInt.py
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#!/usr/bin/env python3
# LinkedInt
# Scrapes LinkedIn without using LinkedIn API
# Original scraper by @DisK0nn3cT (https://github.com/DisK0nn3cT/linkedin-gatherer)
# Modified by @vysecurity
# - Additions:
# --- UI Updates
# --- Constrain to company filters
# --- Addition of Hunter for e-mail prediction
import sys
import re
import time
import requests
import subprocess
import json
import argparse
import os
import urllib.request, urllib.parse, urllib.error
import math
import string
from bs4 import BeautifulSoup
import csv as csv_module
import pdb
import ssl
import importlib
from requests.packages.urllib3.exceptions import InsecureRequestWarning
""" Setup Argument Parameters """
parser = argparse.ArgumentParser(description="Discovery LinkedIn")
parser.add_argument("-u", "--keywords", help="Keywords to search")
parser.add_argument("-o", "--output", help="Output file (do not include extentions)")
parser.add_argument("-e", "--email", help="Domain used for email address")
parser.add_argument(
"-c", "--company", help="Restrict to company filter", action="store_true"
)
parser.add_argument("-i", "--id", help="Company ID to use")
parser.add_argument("-f", "--format", help='Email format. "auto" to search Hunter')
parser.add_argument(
"--login",
help="Login for LinkedIn",
)
parser.add_argument(
"--password",
help="Password for LinkedIn",
)
parser.add_argument(
"--apikey",
help="API Key for HunterIO",
)
parser.add_argument(
"--li_at", help="Provide li_at cookie (session cookie) instead of login"
)
parser.add_argument("--proxy", help="Use proxy server")
args = parser.parse_args()
if not ((args.login and args.password) or (args.li_at)):
print(f"Error: Either login/password or li_at cookie are required.")
# pdb.set_trace()
sys.exit(1)
api_key = args.apikey # Hunter API key
username = args.login # enter username here
password = args.password # enter password here
if args.proxy:
proxies = {"https": args.proxy, "http": args.proxy}
else:
proxies = {} # {'https':'127.0.0.1:8080'}
# silence all url warnings
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
def create_graphql(start=0, count=50, companyId=None, title=None, search_cluster=None):
query_string = (
f"(start:{start},count:{count},origin:COMPANY_PAGE_CANNED_SEARCH,query:"
)
query_string += "(flagshipSearchIntent:SEARCH_SRP,queryParameters:List("
params = []
if companyId:
params.append(
f"(key:currentCompany,value:List({companyId})),(key:resultType,value:List(PEOPLE))"
)
if title:
params.append(f"(key:title,value:List({title}))")
query_string += ",".join(params)
query_string += "),includeFiltersInResponse:false))&queryId=" + search_cluster
return query_string
"""
(start:0,origin:FACETED_SEARCH,query:(flagshipSearchIntent:SEARCH_SRP,queryParameters:
List((key:currentCompany,value:List(5470,2297043)),(key:resultType,value:List(PEOPLE)),
(key:title,value:List(engineer))),includeFiltersInResponse:false))
(start:0,origin:COMPANY_PAGE_CANNED_SEARCH,query:(flagshipSearchIntent:SEARCH_SRP,queryParameters:
List((key:currentCompany,value:List({companyID})),(key:resultType,value:List(PEOPLE))
),includeFiltersInResponse:false))&queryId=voyagerSearchDashClusters.9bce173fbce5f0cf146dac911d840d99
"""
def login():
s = requests.Session()
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/114.0"
}
res = s.get(
"https://www.linkedin.com/uas/login",
proxies=proxies,
verify=False,
headers=headers,
)
csrf = res.text.split('loginCsrfParam" value="')[1].split('"')[0]
page_instance = res.text.split('pageInstance" content="')[1].split('"')[0]
# data = res.text[res.text.find("<form"):res.text.find("</form")]
login_data = {}
# for c in data.split('input type')[1:]:
# login_data[c.split('name="')[1].split('"')[0]] = c.split('value="')[1].split('"')[0]
login_data["session_key"] = username
login_data["session_password"] = password
login_data["csrfToken"] = res.cookies["JSESSIONID"]
login_data["loginCsrfParam"] = csrf
login_data["ac"] = "0"
login_data["parentPageKey"] = "d_checkpoint_lg_consumerLogin"
login_data["pageInstance"] = page_instance
login_data["trk"] = ""
login_data["authUUID"] = ""
login_data["session_redirect"] = ""
login_data["_d"] = "d"
login_data["showGoogleOneTapLogin"] = "true"
login_data["controlId"] = "d_checkpoint_lg_consumerLogin-login_submit_button"
res = s.post(
"https://www.linkedin.com/checkpoint/lg/login-submit",
data=login_data,
proxies=proxies,
verify=False,
headers=headers,
)
return s.cookies["li_at"]
def loadPage(client, url, data=None):
try:
response = client.open(url)
except:
print("[!] Cannot load main LinkedIn page")
try:
if data is not None:
response = client.open(url, data)
else:
response = client.open(url)
return "".join(response.readlines())
except:
sys.exit(0)
def get_search():
body = ""
csv = []
css = """<style>
#employees {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#employees td, #employees th {
border: 1px solid #ddd;
padding: 8px;
}
#employees tr:nth-child(even){background-color: #f2f2f2;}
#employees tr:hover {background-color: #ddd;}
#employees th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
</style>
"""
header = """<center><table id=\"employees\">
<tr>
<th>Photo</th>
<th>Name</th>
<th>Possible Email:</th>
<th>Job</th>
<th>Location</th>
</tr>
"""
# Do we want to automatically get the company ID?
if bCompany:
if bAuto:
# Automatic
# Grab from the URL
companyID = 0
url = (
"https://www.linkedin.com/voyager/api/typeahead/hits?q=blended&query=%s"
% search
)
headers = {
"Csrf-Token": "ajax:0397788525211216808",
"X-RestLi-Protocol-Version": "2.0.0",
}
cookies["JSESSIONID"] = "ajax:0397788525211216808"
r = requests.get(url, cookies=cookies, headers=headers)
content = json.loads(r.text)
firstID = 0
for i in range(0, len(content["elements"])):
try:
companyID = content["elements"][i]["hitInfo"][
"com.linkedin.voyager.typeahead.TypeaheadCompany"
]["id"]
if firstID == 0:
firstID = companyID
print("[Notice] Found company ID: %s" % companyID)
except:
continue
companyID = firstID
if companyID == 0:
print(
"[WARNING] No valid company ID found in auto, please restart and find your own"
)
else:
# Don't auto, use the specified ID
companyID = bSpecific
print()
print("[*] Using company ID: %s" % companyID)
# Get the search clusters
# Fetch the initial page to get results/page counts
r = requests.get(
"https://www.linkedin.com/",
# params=params,
cookies=cookies,
verify=False,
proxies=proxies,
)
script_url = r.text[: r.text.find('data-fastboot-src="/assets/vendor.js"')].split(
'"'
)[-2]
r = requests.get(script_url, cookies=cookies, verify=False, proxies=proxies)
search_cluster = r.text[: r.text.find('",name:"search-cluster-collection"')].split(
'"'
)[-3]
# pdb.set_trace()
url = f"https://www.linkedin.com/voyager/api/graphql?variables=" + create_graphql(
start=0,
count=10,
companyId=companyID,
title=search,
search_cluster=search_cluster,
)
headers = {
"Csrf-Token": "ajax:0397788525211216808",
"X-RestLi-Protocol-Version": "2.0.0",
}
cookies["JSESSIONID"] = "ajax:0397788525211216808"
# print url
# s = requests.Session()
# req = requests.Request(method="GET", url="https://www.linkedin.com")
# prep = req.prepare()
# prep.url = url
r = requests.get(
url,
# params=params,
cookies=cookies,
headers=headers,
verify=False,
proxies=proxies,
)
content = json.loads(r.text)
# pdb.set_trace()
paging_data = content["data"]["searchDashClustersByAll"]["paging"]
data_total = paging_data["total"]
# Calculate pages off final results at 40 results/page
pages = int(data_total / 50)
# if pages == 0:
# pages = 1
# if data_total % 40 == 0:
# # Becuase we count 0... Subtract a page if there are no left over results on the last page
# pages = pages - 1
# if pages == 0:
# print("[!] Try to use quotes in the search name")
# sys.exit(0)
print("[*] %i Results Found" % data_total)
# if data_total > 1000:
# pages = 25
# print(
# "[*] LinkedIn only allows 1000 results. Refine keywords to capture all data"
# )
print("[*] Fetching %i Pages" % pages)
print()
csvfile = open("{}.csv".format(outfile), "w")
csvwriter = csv_module.writer(
csvfile, delimiter=",", quotechar='"', quoting=csv_module.QUOTE_MINIMAL
)
for p in range(0, data_total, 50):
# Request results for each page using the start offset
url = (
f"https://www.linkedin.com/voyager/api/graphql?variables="
+ create_graphql(
start=p,
count=50,
companyId=companyID,
title=search,
search_cluster=search_cluster,
)
)
# if bCompany == False:
# url = (
# "https://www.linkedin.com/voyager/api/search/cluster?count=40&guides=List()&keywords=%s&origin=OTHER&q=guided&start=%i"
# % (search, p * 40)
# )
# else:
# url = f"https://www.linkedin.com/voyager/api/graphql?variables=(start:{p},count:50,origin:COMPANY_PAGE_CANNED_SEARCH,query:(flagshipSearchIntent:SEARCH_SRP,queryParameters:List((key:currentCompany,value:List({companyID})),(key:resultType,value:List(PEOPLE))),includeFiltersInResponse:false))&queryId=voyagerSearchDashClusters.9bce173fbce5f0cf146dac911d840d99"
# # url = (
# # "https://www.linkedin.com/voyager/api/search/cluster?count=40&guides=List(v->PEOPLE,facetCurrentCompany->%s,title->%s)&origin=OTHER&q=guided&start=%i"
# # % (companyID, search, p * 40)
# # )
# # print url
r = requests.get(
url,
cookies=cookies,
headers=headers,
verify=False,
proxies=proxies,
# params=params,
)
content = r.text.encode("UTF-8")
content = json.loads(content)
items = []
for d in content["data"]["searchDashClustersByAll"]["elements"]:
for item in d["items"]:
if item["item"]["entityResult"]:
items.append(item)
print("[*] Fetching page %i with %i results" % ((p / 50), len(items)))
for c in items:
# if (
# "com.linkedin.voyager.search.SearchProfile" in c["hitInfo"]
# and c["hitInfo"]["com.linkedin.voyager.search.SearchProfile"][
# "headless"
# ]
# == False
# ):
if 1 == 1:
# try:
# data_industry = c["hitInfo"][
# "com.linkedin.voyager.search.SearchProfile"
# ]["industry"]
# except:
data_industry = ""
name = c["item"]["entityResult"]["title"]["text"]
data_firstname = name.split(" ")[0]
data_lastname = (
" ".join(name.split(" ")[1:]) if name.count(" ") > 0 else ""
)
data_slug = c["item"]["entityResult"]["navigationContext"]["url"]
data_occupation = c["item"]["entityResult"]["primarySubtitle"]["text"]
data_location = c["item"]["entityResult"]["secondarySubtitle"]["text"]
# pdb.set_trace()
try:
data_picture = c["item"]["entityResult"]["image"]["attributes"][0][
"detailData"
]["nonEntityProfilePicture"]["vectorImage"]["artifacts"][0][
"fileIdentifyingUrlPathSegment"
]
except:
print(
"[*] No picture found for %s %s, %s"
% (data_firstname, data_lastname, data_occupation)
)
data_picture = ""
# incase the last name is multi part, we will split it down
# Also trying to strip out anything after a comma, and any
# word that is all caps, since those are probably certs
# (CPA, CFA, CISSP, etc, etc, etc)
parts = []
for p in data_lastname.split(",")[0].split(" "):
if p.upper() != p:
parts.append(p)
name = data_firstname + " " + data_lastname
fname = ""
mname = ""
lname = ""
if len(parts) == 1:
fname = data_firstname.split(" ")[0]
mname = "?"
lname = parts[0]
elif len(parts) == 2:
fname = data_firstname.split(" ")[0]
mname = parts[0]
lname = parts[1]
elif len(parts) >= 3:
fname = data_firstname.split(" ")[0]
lname = parts[0]
else:
fname = data_firstname.split(" ")[0]
lname = "?"
fname = re.sub("[^A-Za-z]+", "", fname)
mname = re.sub("[^A-Za-z]+", "", mname)
lname = re.sub("[^A-Za-z]+", "", lname)
if len(fname) == 0 or len(lname) == 0:
# invalid user, let's move on, this person has a weird name
continue
# come here
if prefix == "full":
user = "{}{}{}".format(fname, mname, lname)
if prefix == "firstlast":
user = "{}{}".format(fname, lname)
if prefix == "firstmlast":
user = "{}{}{}".format(fname, mname[0], lname)
if prefix == "flast":
user = "{}{}".format(fname[0], lname)
if prefix == "first.last":
user = "{}.{}".format(fname, lname)
if prefix == "fmlast":
user = "{}{}{}".format(fname[0], mname[0], lname)
if prefix == "lastfirst":
user = "{}{}".format(lname, fname)
if prefix == "first":
user = "{}".format(fname)
if prefix == "firstl":
user = "{}{}".format(fname, lname[0])
email = "{}@{}".format(user, suffix)
body += (
"<tr>"
'<td><a href="%s"><img src="%s" width=200 height=200></a></td>'
'<td><a href="%s">%s</a></td>'
"<td>%s</td>"
"<td>%s</td>"
"<td>%s</td>"
"<a>"
% (
data_slug,
data_picture,
data_slug,
name,
email,
data_occupation,
data_location,
)
)
csv.append(
'"%s","%s","%s","%s","%s", "%s"'
% (
data_firstname,
data_lastname,
name,
email,
data_occupation,
data_location.replace(",", ";"),
)
)
foot = "</table></center>"
f = open("{}.html".format(outfile), "w")
f.write(css)
f.write(header)
f.write(body)
f.write(foot)
f.close()
csvwriter.writerow(
[
data_firstname,
data_lastname,
name,
email,
data_occupation,
data_location.replace(",", ";"),
]
)
else:
print("[!] Headless profile found. Skipping")
print()
csvfile.close()
def banner():
print(
"""
██╗ ██╗███╗ ██╗██╗ ██╗███████╗██████╗ ██╗███╗ ██╗████████╗
██║ ██║████╗ ██║██║ ██╔╝██╔════╝██╔══██╗██║████╗ ██║╚══██╔══╝
██║ ██║██╔██╗ ██║█████╔╝ █████╗ ██║ ██║██║██╔██╗ ██║ ██║
██║ ██║██║╚██╗██║██╔═██╗ ██╔══╝ ██║ ██║██║██║╚██╗██║ ██║
███████╗██║██║ ╚████║██║ ██╗███████╗██████╔╝██║██║ ╚████║ ██║
╚══════╝╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝╚═════╝ ╚═╝╚═╝ ╚═══╝ ╚═╝
"""
)
# print("\033[1;31m%s\033[0;0m" % data)
# print("\033[1;34mProviding you with Linkedin Intelligence")
# print("\033[1;32mAuthor: Vincent Yiu (@vysec, @vysecurity)\033[0;0m")
# print("\033[1;32mOriginal version by @DisK0nn3cT\033[0;0m")
def authenticate():
try:
session = login()
if len(session) == 0:
sys.exit("[!] Unable to login to LinkedIn.com")
print("[*] Obtained new session")
cookies = dict(li_at=session)
except Exception as e:
sys.exit("[!] Could not authenticate to linkedin. %s" % e)
return cookies
if __name__ == "__main__":
banner()
# Prompt user for data variables
search = (
args.keywords
if args.keywords != None
else input("[*] Enter search Keywords (use quotes for more precise results)\n")
)
print()
outfile = (
args.output
if args.output != None
else input("[*] Enter filename for output (exclude file extension)\n")
)
print()
while True:
if args.company:
bCompany = "y"
args.company = None
else:
bCompany = input("[*] Filter by Company? (Y/N): \n")
if bCompany.lower() == "y" or bCompany.lower() == "n":
break
else:
print("[!] Incorrect choice")
if bCompany.lower() == "y":
bCompany = True
else:
bCompany = False
bAuto = True
bSpecific = 0
prefix = ""
suffix = ""
print()
if bCompany:
while True:
if args.id:
if args.id == "auto":
bSpecific = ""
else:
bSpecific = args.id
args.id = None
else:
bSpecific = input(
"[*] Specify a Company ID (Provide ID or leave blank to automate): \n"
)
if bSpecific != "":
bAuto = False
if bSpecific != 0:
try:
int(bSpecific)
break
except:
print(
"[!] Incorrect choice, the ID either has to be a number or blank"
)
else:
print(
"[!] Incorrect choice, the ID either has to be a number or blank"
)
else:
bAuto = True
break
print()
while True:
if args.email:
suffix = args.email.lower()
args.email = None
else:
suffix = input("[*] Enter e-mail domain suffix (eg. contoso.com): \n")
suffix = suffix.lower()
if "." in suffix:
break
else:
print("[!] Incorrect e-mail? There's no dot")
print()
while True:
if args.format:
prefix = args.format.lower()
args.format = None
else:
prefix = input(
"[*] Select a prefix for e-mail generation (auto,full,firstlast,firstmlast,flast,first.last,fmlast,lastfirst): \n"
)
prefix = prefix.lower()
print()
if (
prefix == "full"
or prefix == "firstlast"
or prefix == "firstmlast"
or prefix == "flast"
or prefix == "first"
or prefix == "first.last"
or prefix == "fmlast"
or prefix == "lastfirst"
or prefix == "firstl"
):
break
elif prefix == "auto":
# if auto prefix then we want to use hunter IO to find it.
print("[*] Automaticly using Hunter IO to determine best Prefix")
url = (
"https://hunter.io/trial/v2/domain-search?offset=0&domain=%s&format=json"
% suffix
)
r = requests.get(url)
content = json.loads(r.text)
if "status" in content:
print("[!] Rate limited by Hunter IO trial")
url = "https://api.hunter.io/v2/domain-search?domain=%s&api_key=%s" % (
suffix,
api_key,
)
# print url
r = requests.get(url)
content = json.loads(r.text)
if "status" in content:
print("[!] Rate limited by Hunter IO Key")
continue
# print content
prefix = content["data"]["pattern"]
print("[!] %s" % prefix)
if prefix:
prefix = prefix.replace("{", "").replace("}", "")
if (
prefix == "full"
or prefix == "firstlast"
or prefix == "firstmlast"
or prefix == "flast"
or prefix == "first"
or prefix == "first.last"
or prefix == "fmlast"
or prefix == "lastfirst"
or prefix == "firstl"
):
print("[+] Found %s prefix" % prefix)
break
else:
print(
"[!] Automatic prefix search failed, please insert a manual choice"
)
continue
else:
print(
"[!] Automatic prefix search failed, please insert a manual choice"
)
continue
else:
print(
"[!] Incorrect choice, please select a value from (auto,full,firstlast,firstmlast,flast,first.last,fmlast)"
)
print()
# URL Encode for the querystring
if bCompany:
search = urllib.parse.quote(search)
else:
search = urllib.parse.quote_plus(search)
if args.li_at:
cookies = {"li_at": args.li_at}
else:
cookies = authenticate()
# Initialize Scraping
get_search()
print("[+] Complete")