Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing dependency with simplejson #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
177 changes: 92 additions & 85 deletions peframe/modules/fileurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,99 +25,106 @@

import re
import json
import string
import stringstat


def valid_ip(address):
try:
host_bytes = address.split('.')
valid = [int(b) for b in host_bytes]
valid = [b for b in valid if b >= 0 and b<=255]
valid = [b for b in valid if b >= 0 and b <= 255]
return len(host_bytes) == 4 and len(valid) == 4
except:
return False


def get(filename, strings_match):
strings_info = json.loads(stringstat.get(filename))
strings_list = strings_info['content']
ip_list = []
file_list = []
filetype_dict = {}
url_list = []
fuzzing_dict = {}
apialert_list = []
antidbg_list = []

# Get filetype and fuzzing
file_type = strings_match['filetype'].items()
fuzzing_list = strings_match['fuzzing'].items()

# Strings analysis
for string in strings_list:
# URL list
urllist = re.findall(r'((smb|srm|ssh|ftps?|file|https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)', string, re.MULTILINE)
if urllist:
for url in urllist:
url_list.append(url[0])

# IP list
iplist = re.findall(r'[0-9]+(?:\.[0-9]+){3}', string, re.MULTILINE)
if iplist:
for ip in iplist:
if valid_ip(str(ip)) and not re.findall(r'[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}\.0', str(ip)):
ip_list.append(str(ip))

# FILE list
fname = re.findall("(.+(\.([a-z]{2,3}$)|\/.+\/|\\\.+\\\))+", string, re.IGNORECASE | re.MULTILINE)
if fname:
for word in fname:
word = filter(None, word[0])
file_list.append(word)

# Purge list
ip_list = filter(None, list(set([item for item in ip_list])))
url_list = filter(None, list(set([item for item in url_list])))

# Initialize filetype
for key, value in file_type:
filetype_dict[key] = []

# Search for valid filename
array_tmp = []
for file in file_list:
for key, value in file_type:
match = re.findall("\\"+value+"$", file, re.IGNORECASE | re.MULTILINE)
if match and file.lower() not in array_tmp and len(file) > 4:
filetype_dict[key].append(file)
array_tmp.append(file.lower())

# Remove empty key filetype
for key, value in filetype_dict.items():
if not filetype_dict[key]:
del filetype_dict[key]

# Initialize fuzzing
for key, value in fuzzing_list:
fuzzing_dict[key] = []

# Strings analysis for fuzzing
array_tmp = []
for string in strings_list:
for key, value in fuzzing_list:
fuzz_match = re.findall(value, string, re.IGNORECASE | re.MULTILINE)
if fuzz_match and string.lower() not in array_tmp:
fuzzing_dict[key].append(string)
array_tmp.append(string.lower())

# Remove empty key filetype
for key, value in filetype_dict.items():
if not filetype_dict[key]:
del filetype_dict[key]

# Remove empty key fuzzing
for key, value in fuzzing_list:
if not fuzzing_dict[key]:
del fuzzing_dict[key]

return {"file": filetype_dict, "url": url_list, "ip": ip_list, "fuzzing": fuzzing_dict}

strings_info = json.loads(stringstat.get(filename))
strings_list = strings_info['content']
ip_list = []
file_list = []
filetype_dict = {}
url_list = []
fuzzing_dict = {}

# Get filetype and fuzzing
file_type = strings_match['filetype'].items()
fuzzing_list = strings_match['fuzzing'].items()

# Strings analysis
for string in strings_list:
# URL list
urllist = re.findall(r'((smb|srm|ssh|ftps?|file|https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)', string, re.MULTILINE)
if urllist:
for url in urllist:
url_list.append(url[0])

# IP list
iplist = re.findall(r'[0-9]+(?:\.[0-9]+){3}', string, re.MULTILINE)
if iplist:
for ip in iplist:
if valid_ip(str(ip)) and not re.findall(r'[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}\.0', str(ip)):
ip_list.append(str(ip))

# FILE list
fname = re.findall('(.+(\.([a-z]{2,3}$)|\/.+\/|\\\.+\\\))+', string, re.IGNORECASE | re.MULTILINE)
if fname:
for word in fname:
word = filter(None, word[0])
file_list.append(word)

# Purge list
ip_list = filter(None, list(set([item for item in ip_list])))
url_list = filter(None, list(set([item for item in url_list])))

# Initialize filetype
for key, value in file_type:
filetype_dict[key] = []

# Search for valid filename
array_tmp = []
for file in file_list:
for key, value in file_type:
match = re.findall(
'\\' + value + '$', file, re.IGNORECASE | re.MULTILINE
)
if match and file.lower() not in array_tmp and len(file) > 4:
filetype_dict[key].append(file)
array_tmp.append(file.lower())

# Remove empty key filetype
for key, value in filetype_dict.items():
if not filetype_dict[key]:
del filetype_dict[key]

# Initialize fuzzing
for key, value in fuzzing_list:
fuzzing_dict[key] = []

# Strings analysis for fuzzing
array_tmp = []
for string in strings_list:
for key, value in fuzzing_list:
fuzz_match = re.findall(
value, string, re.IGNORECASE | re.MULTILINE
)
if fuzz_match and string.lower() not in array_tmp:
fuzzing_dict[key].append(string)
array_tmp.append(string.lower())

# Remove empty key filetype
for key, value in filetype_dict.items():
if not filetype_dict[key]:
del filetype_dict[key]

# Remove empty key fuzzing
for key, value in fuzzing_list:
if not fuzzing_dict[key]:
del fuzzing_dict[key]

return {
'file': filetype_dict,
'url': url_list,
'ip': ip_list,
'fuzzing': fuzzing_dict
}
4 changes: 2 additions & 2 deletions peframe/modules/virustotal.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ----------------------------------------------------------------------

import simplejson
import json
import urllib
import urllib2

Expand All @@ -41,7 +41,7 @@ def get(tosearch, strings_match):
apikey = strings_match['virustotal']['apikey']
if apikey:
response = vtcheck(apikey, tosearch)
response = simplejson.loads(response)
response = json.loads(response)
if response['response_code'] == 1:
scan_date = response['scan_date']
permalink = response['permalink']
Expand Down
Loading