Skip to content

Commit 4973e46

Browse files
committed
List URLs found in the strings
1 parent c928dc5 commit 4973e46

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

PeAnalyzer.py

+13
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,19 @@ def searchAllStrings(self):
12411241
self.strings.add(s)
12421242
s = ""
12431243

1244+
def findURLS(self):
1245+
if self.strings is None:
1246+
self.searchAllStrings()
1247+
1248+
# Adapted from https://gist.github.com/uogbuji/705383
1249+
GRUBER_URLINTEXT_PAT = re.compile(r'(?i)\b((?:(https?|ftp|mailto|file|data|irc)://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
1250+
urls = []
1251+
for s in self.strings:
1252+
url = GRUBER_URLINTEXT_PAT.findall(s)
1253+
if len(url) > 0:
1254+
urls.append(url[0][0])
1255+
return urls
1256+
12441257
def getBlacklistedStrings(self, printToConsole = True):
12451258
if self.strings is None:
12461259
self.searchAllStrings()

pestudio.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def parseCommandLineArguments():
3535
parser.add_argument("--relocations", help="Show the relocations.", action="store_true", dest="relocations")
3636
parser.add_argument("-s", "--signatures", help="Check for known signatures (e.g. packers).", action="store_true", dest="signatures")
3737
parser.add_argument("--strings", help="Check the strings in the PE file.", action="store_true", dest="strings")
38+
parser.add_argument("-u", "--urls", help="List all URLs found in the PE file", action="store_true", dest="urls")
3839
parser.add_argument("-x", "--xml", help="Format output as xml.", action="store_true", dest="xml")
3940
parser.add_argument("-j", "--json", help="Format output as JSON.", action="store_true", dest="json")
4041
parser.add_argument("--interactive", help="Use the tool in interactive mode.", action="store_true", dest="interactive")
@@ -230,7 +231,14 @@ def interactiveMode(file = None):
230231
print("Entering interactive mode...")
231232
if file is None:
232233
print("Please specify file to analyze or type help")
233-
234+
else:
235+
file = file.replace("~", os.path.expanduser("~"))
236+
if not os.path.isfile(file):
237+
print(constants.BLUE + "Could not find the specified file %s" % file + constants.RESET)
238+
else:
239+
peAnalyzer = PeAnalyzer(file)
240+
matcher = SignatureMatcher(file)
241+
vt = VirusTotalClient(file)
234242

235243
def complete(text, state):
236244
text = text.replace("~", os.path.expanduser("~"))
@@ -309,6 +317,14 @@ def complete(text, state):
309317
collectIndicators(vt, peAnalyzer, matcher)
310318
elif user_in == "indicators -a":
311319
collectIndicators(vt, peAnalyzer, matcher, all)
320+
elif user_in == "urls" or user_in == "u":
321+
urls = peAnalyzer.findURLS()
322+
if len(urls) > 0:
323+
print("The following (maybe non-malicious) URLs have been found:")
324+
for url in urls:
325+
print("\t" + url)
326+
else:
327+
print("No URL found in the file's strings")
312328
else:
313329
if user_in != "help":
314330
print("Command '" + user_in + "' is unknown.")
@@ -329,6 +345,7 @@ def complete(text, state):
329345
print("\tsections - show all sections in the file")
330346
print("\tstrings -a - show all strings we can find in the PE file")
331347
print("\tstrings -b - show blacklisted strings we can find in the PE file")
348+
print("\tu/urls - list all URLs found in the PE file")
332349
print("\thelp - print this help text")
333350

334351
no_user_in = True
@@ -455,6 +472,22 @@ def checkFile(args):
455472
else:
456473
print(constants.GREEN + "No packer signature was found in the PE file" + constants.RESET)
457474

475+
if args.urls:
476+
urls = peAnalyzer.findURLS()
477+
if args.xml:
478+
urlsXml = ET.SubElement(root, "URLs")
479+
for url in urls:
480+
ET.SubElement(urlsXml, "url").text = url
481+
elif args.json:
482+
jsonDict["URLs"] = urls
483+
else:
484+
if len(urls) > 0:
485+
print("The following (maybe non-malicious) URLs have been found:")
486+
for url in urls:
487+
print("\t" + url)
488+
else:
489+
print("No URL found in the file's strings")
490+
458491
if not args.yara is None:
459492
if args.xml:
460493
root = checkYara(args.file, args.yara, root=root)

0 commit comments

Comments
 (0)